13、preparedStatement的用法

mac2026-04-18  9

PreparedStatement

它是Statement接口的子接口;强大之处:防止sql攻击;提高代码的可读性、可维护性;提高效率学习PreparedStatement的用法: 1、如何得到PreparedStatement对象(pstmt):给出sql模板;调用connection的 PreparedStatement(String sql模板) 2、为参数赋值:调用pstmt的setXXX()系列方法,为sql模板中的?赋值 3、调用pstmt的executeUpdate()或executeQuery()的无参方法 string sql="select * from mydb1 where usename=? and password=?";//sql模板 PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1,username);//给第一个问好赋值,值为username pstmt.setString(2,password);//给第一个问好赋值,值为password pstmt.executeQuery();
最新回复(0)