JAVA RUNTIME OBJECTS METHOD
9.8 BUILD JAVA WEB SERVICE TO INSERT DATA INTO THE SQL SERVER DATABASE
To perform a faculty record insertion action using our Web service, we need to add another operation or method called InsertFaculty() into our Web service project
WebServiceSQL .
Figure 9.39. The testing result for our Web client project.
9.8.1 Add a New Operation InsertFaculty() into Our Web Service Project
Perform the following operations to add this operation into our Web service:
1. Launch NetBeans IDE and open our Web service project WebServiceSQLApp , and select our Web service main class fi le WebServiceSQL.java from the Projects window.
2. Click on the Design button on the top of the window to open the Design View of our Web service project WebServiceSQL .
3. Click on the Add Operation button to open the Add Operation wizard.
4. Enter InsertFaculty into the Name fi eld and click on the Browse button that is next to the Return Type combo box. Type boolean into the Type Name fi eld and select the item
Boolean (java.lang) from the list, and click on the OK button.
5. Click on the Add button and enter fdata into the Name parameter fi eld. Then click on the drop - down arrow of the Type combo box, and select the Choose item to open the Find Type wizard. Type arraylist into the top fi eld, and select the ArrayList (java.util) data type, and click on the OK button to select an ArrayList as the data type for the input parameter.
Your fi nished Add Operation wizard should match the one that is shown in Figure 9.40 . Click on the OK button to complete this new operation creation process.
Click on the Source button on the top of this window to open the code window of our Web service project. Let ’ s perform the coding for this newly added operation.
On the opened code window, enter the codes that are shown in Figure 9.41 into this newly added operation InsertFaculty() .
Let ’ s have a closer look at this piece of codes to see how it works.
A. First, a local integer variable numInsert is created, and it is used to hold the running result of inserting a new faculty record into our sample database.
B. The SQL inserting query statement is created with seven positional parameters as the dynamic parameters for seven pieces of new faculty information to be inserted.
Figure 9.40. The complete Add Operation wizard.
c09.indd 809
c09.indd 809 7/20/2011 11:12:50 AM7/20/2011 11:12:50 AM
www.traintelco.com
C. The user - defi ned method DBConnection() is called to set up a connection between our Web service and our sample database. A connection instance con is returned after the execution of this method.
D. A new PreparedStatement instance pstmt is created to perform this insertion query.
E. Seven setString() methods are used to set up the actual values for seven positional dynamic parameters in the inserting query statement. One point to be noted is that the order of these setString() methods must be identical with the order of columns in our Faculty table.
F. The inserting action is performed by calling the executeUpdate() method, and the insert- ing result is returned and stored in the local integer variable numInsert .
G. The database connection is closed by executing the close() method, since we have com- pleted our data insertion action and need to disconnect with our database.
H. The executeUpdate() method will return an integer to indicate whether this data insertion is successful or not. If a nonzero value is returned, which means that at least one row has been inserted into our Faculty table, then this data inserting action is successful, and a true is returned to the client project.
I. Otherwise, no row has been inserted into our sample database, and this data insertion has failed. A false is returned for this situation.
J. The catch block is used to track and display any exception occurred during this data inser- tion process, and a false will be returned if this situation is really happened.
Figure 9.41. The codes for the new operation InsertFaculty().
@WebMethod(operationName = "InsertFaculty")
public Boolean InsertFaculty(@WebParam(name = "fdata") ArrayList fdata) {
//TODO write your implementation code here:
int numInsert = 0;
String query = "INSERT INTO Faculty (faculty_id, faculty_name, office, " + "phone, college, title, email) VALUES (?, ?, ?, ?, ?, ?, ?)";
try {
con = DBConnection(con);
PreparedStatement pstmt =con.prepareStatement(query);
pstmt.setString(1, fdata.get(0).toString());
pstmt.setString(2, fdata.get(1).toString());
pstmt.setString(3, fdata.get(2).toString());
pstmt.setString(4, fdata.get(3).toString());
pstmt.setString(5, fdata.get(4).toString());
pstmt.setString(6, fdata.get(5).toString());
pstmt.setString(7, fdata.get(6).toString());
numInsert = pstmt.executeUpdate();
con.close();
if (numInsert != 0) return true;
else
return false;
}
catch (Exception ex) {
msgDlg.setMessage("exception is: " + ex);
msgDlg.setVisible(true);
return false;
} } A B
C D E
F G H I
J
At this point, we have completed all coding development for the data insertion action.
Now let ’ s build and run our Web service project to test its function.
9.8.2 Deploy the Web Service Project
Perform the following operations to build and deploy our Web service project:
1. Click on the Clean and Build Main Project button to build our Web service.
2. Right click on our Web application WebServiceSQLApp and select the Deploy item to deploy our Web service. If everything is fi ne, a successful deployment result should be dis- played, as shown in Figure 9.42 .
A problem arises when testing this Web service project using the tester page, which is the input parameter array fdata . As we know, the fdata has a data type of ArrayList, and it needs to (1) create an ArrayList instance, and then (2) assign a group of faculty information to that ArrayList object to call this Web service operation InsertFaculty() to perform the faculty data insertion. However, it is diffi cult to do those two operations manually by using this tester page. Therefore, we need to create some Web client projects to consume and test this Web service project.
Next, we can develop some Web client projects to consume this Web service to perform data insertion to the Faculty table in our sample database. First, let ’ s discuss how to build a Windows - based client project to consume our Web service.