java---jdbc preparedStatement (防止SQl注入)

写的不是太好  包涵包涵
public class Dome3 { /** * 测试PreparedStatement的插入 * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Connection con=null; PreparedStatement ps=null; try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //连接数据库 获取connection对象 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456"); //欲准备执行SQL语句 ps=con.prepareStatement("insert into user(userName,passWord) values(?,?)"); ps.setString(1, "lideng"); ps.setString(2, "12365155"); //执行更新 成功后会返回 影响了几行数据 System.out.println(ps.executeUpdate()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

  

猜你喜欢

转载自www.cnblogs.com/qurui1997/p/10520756.html