Sample code to call SQL stored procedure in IBM iDB2 for i.
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
public class TestProcedureCall{
public static void main(String[] args) {
String storedProcedure = "{ CALL MYPROC (?, ?, ?) }";
byte[] blankByte = " ".getBytes();
try{
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
Connection con = DriverManager.getConnection(
"jdbc:as400://LOCALHOST", "USER1", "PASSWORD1");
CallableStatement stmt =
con.prepareCall(storedProcedure);
stmt.setString(1,"ROBIN"); //Name
stmt.setBytes(2,blankByte); //Nothing
stmt.setInt(3,30); //Age
stmt.registerOutParameter(1,Types.CHAR);
stmt.registerOutParameter(2,Types.CHAR);
stmt.registerOutParameter(5,Types.DECIMAL);
ResultSet rs = callableStatement.executeQuery();
ResultSetMetaData rsMetaData = rs.getMetaData();
int colCount = rsMetaData.getColumnCount();
for (int i = 1 ; i <= colCount ; i++){
System.out.println(rsMetaData.getColumnName(i));
}
rs.close();
callableStatement.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}//End main
}//End class
No comments:
Post a Comment