Java Reference
In-Depth Information
tds.setUser( dbUserName );
tds.setPassword( dbPassword );
DataSource ds = tds;
Connection con = ds.getConnection(dbUserName,dbPassword);
Statement stmt = con.createStatement();
stmt.executeUpdate(createProc);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
Calling a Stored Procedure
Stored procedures are called using a simple escape syntax defined by enclosing the call in curly braces.
When the driver encounters the curly braces, it will translate the command they enclose into the native
SQL used by the database to call the stored procedure. This syntax has two forms: one that has a result
parameter and one that does not. Here's an example:
{?= call <procedure-name>[<arg1>,<arg2>, ...]}
{call <procedure-name>[<arg1>,<arg2>, ...]}
Question marks (?) serve as placeholders for parameters defined in the stored procedure using the
@Name convention as shown in the example. IN parameter values are set using the set methods
inherited from PreparedStatement .
When calling a stored procedure, a CallableStatement object is created using the Connection
method prepareCall() . The argument of the prepareCall() method is the escape String, with
question marks as place holders for each of the input parameters. Values for each of these parameters
are assigned using the setter methods of the CallableStatement object; then the
CallableStatement is executed. The following code fragment shows how the stored procedure
created in Listing 13-3 can be called:
String[] newMember = {"Fred","A","Tagliatelle","123 Ziti",
"Penne","PA","12345","123-456-7890","fat@pasta.com"};
CallableStatement cs =
con.prepareCall("{call
INSERT_CONTACT_INFO(?,?,?,?,?,?,?,?,?,?)}");
cs.setInt(1,2);
for(int i=0;i<newMember.length;i++){
cs.setString(i+2,newMember[i]);
}
System.out.println(cs.executeUpdate()+" row updated");
Search WWH ::




Custom Search