「Java」PreparedStatementを使用する方法

書式
public interface PreparedStatement extends Statement
プリコンパイルされたSQL文を表すオブジェクトです。SQL文は、プリコンパイルされ、PreparedStatementオブジェクトに格納されます。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String strSQL =" select * from user_tbl" + "where user_id = ? " + " and dep_name = ? ";
PreparedStatement pstmt = null;
Result rslt = null;
try {
pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1,"1002");
pstmt.setString(2,"design");
rslt = pstmt.executeQuery();
//処理コード
}catch(SQLException e) {
throw e;
} finally {
rslt.close();
pstmt.close();
}
String strSQL =" select * from user_tbl" + "where user_id = ? " + " and dep_name = ? "; PreparedStatement pstmt = null; Result rslt = null; try { pstmt = conn.prepareStatement(strSQL); pstmt.setString(1,"1002"); pstmt.setString(2,"design"); rslt = pstmt.executeQuery(); //処理コード }catch(SQLException e) { throw e; } finally { rslt.close(); pstmt.close(); }
String strSQL =" select * from user_tbl" + "where user_id = ? " + " and dep_name = ? ";
PreparedStatement pstmt = null;
Result rslt = null;
try {
   
   pstmt = conn.prepareStatement(strSQL);
   pstmt.setString(1,"1002");
   pstmt.setString(2,"design");
   
   rslt = pstmt.executeQuery();
   //処理コード
   

}catch(SQLException e) {
 throw e;
} finally {
      rslt.close();
        pstmt.close();
}

 

Java

Posted by arkgame