User Guide > Procedures > Executing a Procedure or Parameterized Query > Executing a Procedure through JDBC
 
Executing a Procedure through JDBC
The name of the sample stored procedure is SP_D1. The username and password are guest and password. This section contains sample Java code to execute a stored procedure that returns two cursors.
To execute a stored procedure through JDBC
1. Publish the stored procedure as a TDV data service in the following directory:
Data Services/Databases/ds
 
2. Write and compile the Java code, and save the class file in a directory.
See Sample Code Using MultipleCursors.
3. From the directory where you saved the class file, run the following command:
java -classpath <path_to_csjdbc.jar_file>/csjdbc.jar\;. <MultipleCursors>
 
MultipleCursors is the name of the class file, and csjdbc.jar is the name of the JAR file containing the class file.
Sample Code Using MultipleCursors
import java.sql.*;
 
public class MultipleCursors
{
private static final String COMPOSITE_URL =
"jdbc:compositesw:dbapi@localhost:9401?domain=composite&dataSource=ds";
 
private static final String COMPOSITE_DRIVER =
"cs.jdbc.driver.CompositeDriver";
 
private static final String COMPOSITE_USER = "guest";
private static final String COMPOSITE_PASSWORD = "password";
 
public static void main(String[] args) {
try {
Class.forName(COMPOSITE_DRIVER);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return;
}
 
try {
executeProcedure();
} catch (SQLException ex) {
ex.printStackTrace();
return;
}
}
 
private static void executeProcedure()
throws SQLException
{
Connection conn = DriverManager.getConnection(
COMPOSITE_URL, COMPOSITE_USER, COMPOSITE_PASSWORD);
 
CallableStatement stmt = conn.prepareCall("{call SP_D1(?,?)}");
stmt.registerOutParameter(1, Types.OTHER);
stmt.registerOutParameter(2, Types.OTHER);
stmt.execute();
 
printResultSet((ResultSet)stmt.getObject(1));
printResultSet((ResultSet)stmt.getObject(2));
 
stmt.close();
conn.close();
}
 
private static void printResultSet(ResultSet rs)
throws SQLException
{
ResultSetMetaData metaData = rs.getMetaData();
int rowIndex = 0;
while (rs.next()) {
System.out.println("Row " + rowIndex++);
for (int i=1; i<=metaData.getColumnCount(); i++) {
System.out.println(" Column " + i + " " + metaData.getColumnName(i) +
" " + rs.getString(i));
}
}
}
}