例子中用到的表t123及存儲過程test_proc的創建見博文:
package www.zjptcc.wxw.jdbctest.syb; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import Java.sql.Types; public class TestSp1 { /** * @param args */ public static void main(String[] args) { // TODO 自動生成的方法存根 try { Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance(); String url = "jdbc:Sybase:Tds:localhost:5000/testdb";// 數據庫名 Connection conn = DriverManager.getConnection(url, "mymotif","wxwpxh"); /* test_proc(@id_min int, @num_t123 int output))是將表t123中的id字段中大於等於第一個參數@id_min 的數目統計好,放到第二個參數@num_t123(輸出參數) */ String procedure = "{ call test_proc(?, ?)}"; CallableStatement statement = conn.prepareCall(procedure); //通過 setXXX 方法將IN參數傳給@id_min,其中第一個1是參數的索引值,第二個1是傳給@id_min的值 statement.setInt(1, 1); //registerOutParameter注冊輸出參數@num_t123,輸出參數的索引值是2 //參數的索引值是根據占位符?出現的次序從左到右由1開始計,不管其是輸入還是輸出參數 statement.registerOutParameter(2,Types.INTEGER); statement.execute(); //打印輸出參數@num_t123的值 System.out.print(statement.getInt(2)); conn.close(); } catch (SQLException sqe) { System.out.println("Unexpected exception : " + sqe.toString() + ", sqlstate = " + sqe.getSQLState()); System.exit(1); } catch (Exception e) { System.out.println(e.getMessage()); } } }