连接数据库操作时,其实会有SQL注入问题
使用Statement执行对象容易出问题,导致数据泄露
所以,可以使用PreparedStatement执行对象,ta的存在就是为防止SQL注入产生的
用法和Statement有点点不一样
Connection con = null;
PreparedStatement st= null;
ResultSet rs = null;
try {
con= JdbcUtils.getConnection();
String sql = "select * from user where id =?"; //?是占位符
st=con.prepareStatement(sql); //这里需要进行预编译
st.setInt(1,2); //前一个数字是第几个占位符,第二个位置是传入的值
rs = st.executeQuery(); //由于是查询操作还是使用 executeQuery()
if(rs.next()) {
System.out.println(rs.getString("name"));
System.out.println(rs.getString("birthday"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
就这样 可以防止SQL注入了