1什么是sql的注入:这个是指输入不的内容被当做sql语句的条件,而不是作为用户名
2如何防止sql;对sql语句进行预编译,就是在sql语句中用?进行站位.
步骤:
第一步,加载驱动,创建数据库的连接
DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb2", "root", "root");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb2", "root", "root");
第二步,编写sql
String sql="delete from user where id = ?";
第三步,需要对sql进行预编译
PreparedStatement pstm = conn.prepareStatement(sql);
第四步,向sql里面设置参数
pstm.setInt(1, id);
第五步,执行sql
pstm.executeUpdate();
第六步,释放资源
if(pstm != null){
try {
stmt.close();
} catch (Exception e2) {
e2.printStackTrace();
}
pstm=null;
}
if(conn != null){
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
conn=null;
}
}