更多精彩文章歡迎關注公眾號“Java之康庄大道”
此篇是在上一篇的基礎上使用PreparedStatement對象來實現JDBC增刪改查的
具體工具類JDBCTools和實現類和配置文件在上一篇Statement對象實現的時候有寫。
上一篇地址http://www.cnblogs.com/yunqing/p/6136896.html
1、增加
/** * 新建一個使用PreparedStatement的方法 * PreparedStatement與Statement的區別 * 1.不需要sql語句拼接,防止sql注入,更加安全 * 2.用占位符的方式寫sql,便於后期維護,提高代碼可讀性,可以自動對類型進行轉換 * 3.有預編譯功能,可以大批量處理sql,(mysql不明顯,Oracle很明顯) * * 向數據庫中添加一條數據 * * PreparedStatement:用於執行sql語句的對象 * 用connection的PreparedStatement(sql)方法獲取 * 用executeUpdate(sql)執行sql語句 * 注意:只能執行 insert,update,delect,不能執行select * */ public void testPreparedStatement(){ Connection conn=null; PreparedStatement preStatement=null;//創建PreparedStatement對象 try { //1、准備Connection連接數據庫 conn=JDBCTools.getConnection2(); //2、准備sql語句 //sql語句不再采用拼接方式,應用占位符問號的方式寫sql語句。 String sql="insert into t_student(name,age,email) values(?,?,?)"; //3、准備prepareStatement //對占位符設置值,占位符順序從1開始,第一個參數是占位符的位置,第二個參數是占位符的值。 preStatement=conn.prepareStatement(sql); //4、占位符設置值 preStatement.setString(1, "klkl"); preStatement.setInt(2, 12); preStatement.setString(3, "kkk@jjj"); //5、執行sql preStatement.executeUpdate(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ //6、關閉數據庫等 JDBCTools.guanbi(null, preStatement, conn); } }
2.修改
public void testPreparedStatement(){ Connection conn=null; PreparedStatement preStatement=null;//創建PreparedStatement對象 try { conn=JDBCTools.getConnection2(); //sql語句不再采用拼接方式,應用占位符問號的方式寫sql語句。 String sql="update t_student set name=?,age=?,email=? where id=?"; //對占位符設置值,占位符順序從1開始,第一個參數是占位符的位置,第二個參數是占位符的值。 preStatement=conn.prepareStatement(sql); preStatement.setString(1, "asas"); preStatement.setInt(2, 12); preStatement.setString(3, "asa@jjj"); preStatement.setInt(4, 11); //執行sql preStatement.executeUpdate(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ JDBCTools.guanbi(null, preStatement, conn); } }
3、刪除
public void testPreparedStatement(){ Connection conn=null; PreparedStatement preStatement=null;//創建PreparedStatement對象 try { conn=JDBCTools.getConnection2(); //sql語句不再采用拼接方式,應用占位符問號的方式寫sql語句。 String sql="delete from t_student where id=?"; //對占位符設置值,占位符順序從1開始,第一個參數是占位符的位置,第二個參數是占位符的值。 preStatement=conn.prepareStatement(sql); preStatement.setInt(1, 12); //執行sql preStatement.executeUpdate(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ JDBCTools.guanbi(null, preStatement, conn); } }
4.查詢
public void testPreparedStatement(){ Connection conn=null; PreparedStatement preStatement=null;//創建PreparedStatement對象 ResultSet rs=null; try { //1.准備Connection連接數據庫 conn=JDBCTools.getConnection2(); //2.准備sql字符串 String sql="select * from t_student"; //3.准備prepareStatement preStatement=conn.prepareStatement(sql); //4.執行sql得到ResultSet rs=preStatement.executeQuery(); //5.處理ResultSet顯示查詢到的結果 while(rs.next()){ int id=rs.getInt(1); String name=rs.getString(2); int age=rs.getInt(3); String email=rs.getString(4); System.out.println(id); System.out.println(name); System.out.println(age); System.out.println(email); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ //6.關閉 JDBCTools.guanbi(rs, preStatement, conn); } }