Java Reference
In-Depth Information
In cases where a CallableStatement object returns multiple ResultSet objects, all of
the results should be retrieved using the method getMoreResults before OUT
parameters are retrieved.
Listing 4-5 illustrates how you would retrieve an output parameter from a stored
procedure in a JDBC application.
Stored procedures can contain more than one SQL statement, in which
case they produce multiple results, in which case the execute method
should be used.
Note
Listing 4-5: Getting an output parameter from a stored procedure
package java_databases.ch04;
import java.sql.*;
public class CheckPassword{
public static void main(String args[]){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection ("jdbc:odbc:Customers","user","pwd");
CallableStatement cs =
con.prepareCall("{call CHECK_USER_NAME(?,?,?)}");
cs.setString(1,"Corleone");
cs.setString(2,"Vito");
cs.registerOutParameter(3, java.sql.Types.VARCHAR);
cs.executeUpdate();
System.out.println(cs.getString(3));
con.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
There are many situations in which it is important that a group of SQL statements be
executed in its entirety. A classic example is a series of statements that debit one
bank account and credit another. In this situation, it is highly undesirable from the
bank's viewpoint to credit the second account if, for some reason, the system fails to
Search WWH ::




Custom Search