Blog / Create and Build the Web Operation InsertCourse() 2 – Develop Java Web Services to Access Databases

Create and Build the Web Operation InsertCourse() 2 – Develop Java Web Services to Access Databases

Create and Build the Web Operation InsertCourse() 2 – Develop Java Web Services to Access Databases


D. A PreparedStatement object, pstfid, is generated with a connection object, con, and query statement to get the desired faculty _ id based on the selected faculty _ name, and the latter is a positional dynamic input parameter.

E. A system method, setString(), is executed to set up the input positional dynamic param-eter that is stored in the input ArrayList instance, cdata; that is, it is located at the first position on that list, thus another system method get(0).toString() is used to pick it up.

F. The first query is executed by calling the system method executeQuery(), and the returned query result is stored in the instance of ResultSet class, rs.

G. A while() loop with its condition, rs.next(), is executed to pick up the selected fac-ulty _ id by calling another system method, getString(). The argument of that method is the column name of the faculty _ id column in the Faculty Table.
H. The second query statement is created with seven input positional dynamic parameters.

One point to be noted is that the order of those input positional parameters must be pre-defined with the input faculty _ name as the first one, followed by six pieces of new inserted course information.

I. Another PreparedStatement object, pstmt, is declared with the second query statement. J. A sequence of setString() methods is executed to set up and initialize all seven input positional parameters, which include a new inserted course record and the selected faculty _ name.

K. The seventh positional dynamic parameter in the second query is the faculty _ id, which is obtained from the first query, not from the input ArrayList variable cdata.

L. The second query is executed by calling another executeUpdate() method, and the run result is returned and assigned to the integer variable numInsert.
M. A sequence of cleaning jobs is performed to close all objects we used for these queries.
N. If this data insertion is successful, a non-zero integer value would be returned, and a true is also returned to the consume project to indicate this situation.

O. Otherwise, if a zero is returned, which means that this data insertion fails and no record has been inserted into our database, a false is returned to indicate this error.

P. The catch block is used to monitor and track these query operations. A false would be returned if any exception occurs.

Now one can click on the Clean and Build Project button to build our Web Service project to compile and update this new operation. Next let’s build our Web operation, UpdateCourse().

Angela Gaffney

  • 0

Leave a Reply

Your email address will not be published. Required fields are marked *


                Copyright © 2024 Saucierlight.com. All Rights Reserved.