prepareCall(String sql)
引數說明:Connection conn = …… //省略部分程式碼 String sql = "{call saveuser(?,?)}"; //呼叫儲存過程 conn.prepareCall(sql);
CallableStatement prepareCall(String sql,int resultSetType,int resultSetConcurrency) throws SQLException引數說明:
Connection conn = …… //省略部分程式碼 String sql = "{call saveuser(?,?)}"; CallableStatement cs = conn.prepareCall(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
CallableStatement prepareCall(String sql,int resultSetType,int resultSetConcurrency,int resultSetHoldability))throws SQLException
引數說明:Connection conn = …… //省略部分程式碼 String sql = "{call saveuser(?,?)}"; CallableStatement cs = conn.prepareCall(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
public String executeQuery(String userName,String passWord){ String message = "驗證失敗"; //定義儲存返回值的字串物件 con = getCon(); //獲取資料庫連線 CallableStatement cs = null; //定義CallableStatement物件 String sql = "{call validateSelect('"+userName+"','"+passWord+"')}"; //定義呼叫儲存過程語句 try{ cs = con.prepareCall(sql); //呼叫儲存過程 ResultSet rest = cs.executeQuery(); //獲取結果集 while(rest.next()){ //迴圈遍歷結果集物件 message = "通過儲存過程驗證成功"; //設定物件資訊 } System.out.println(message); }catch(SQLException e){ e.printStackTrace(); } return message; //返回String物件 }說明:本範例呼叫的名稱為 validateSelect,因此在實現本範例之前,資料庫中要建立名稱為 validateSelect 的儲存過程,儲存過程還要包含兩個 String 型別的字串引數。