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
Monday, September 14, 2015
Monday, September 7, 2015
[Java] IBM AS400 JDBC Call
Sample AS400 JDBC class.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class AS400Query {
public static void main(String[] args) {
ArrayList<String> arl = new ArrayList<String>();
try {
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
Connection con =
DriverManager.getConnection("jdbc:as400://<server>",
"<user>", "<password>");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT <col> FROM <table>");
while (rs.next()){
arl.add(rs.getString(1));
}
rs.close();
for(String s : arl){
String sql = " UPDATE <table> SET COL= " + s;
System.out.println(sql);
stmt.execute(sql);
}
stmt.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class AS400Query {
public static void main(String[] args) {
ArrayList<String> arl = new ArrayList<String>();
try {
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
Connection con =
DriverManager.getConnection("jdbc:as400://<server>",
"<user>", "<password>");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT <col> FROM <table>");
while (rs.next()){
arl.add(rs.getString(1));
}
rs.close();
for(String s : arl){
String sql = " UPDATE <table> SET COL= " + s;
System.out.println(sql);
stmt.execute(sql);
}
stmt.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Subscribe to:
Posts (Atom)