String sql = "INSERT INTO examstudent VALUES("
+ student.getFlowId()
+ ","
+ student.getType()
+ ",'"
+ student.getIdCard()
+ "','"
+ student.getExamCard()
+ "','"
+ student.getStudentName()
+ "','"
+ student.getLocation()
+ "',"
+ student.getGrade()
+ ");";
②. 可以有效的禁止SQL注入.
SQL 注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入数据中注入非法的 SQL 语句段或命令,从而利用系统的 SQL 引擎完成恶意行为的做法
对于 Java 而言,要防范 SQL 注入,只要用 PreparedStatement 取代 Statement 就可以了
③ 可以尽可能提高效率。
注意:PreparedStatement: 是Statement 的子接口,可以传入带占位符的SQL 语句.
并且提供了补充占位符变量的方法。它表示一条预编译过的 SQL 语句。
①. 创建PreparedStatement:
String sql = “INSERT INTO examstudent VALUES(?,?,?,?,?,?,?)”;
PreparedStatement ps = conn.preparedStatement(sql);
②. 调用PreparedStatement 的setXxx(int index, Object val)方法 ,设置占位符的值
index 值从1开始,第一个参数是要设置的 SQL 语句中的参数的索引(从 1 开始),第二个是设置的 SQL 语句中的参数的值
③. 执行 SQL 语句: executeQuery() 或 executeUpdate(). 注意:执行时不再需要
传入SQL 语句.
/**
* 使用PreparedStatement 将有效解决 SQL注入的问题
*/
@Test
public void testSQLInjection2() {
String username = "Tom' OR PASSWORD = ";
String password = "OR '1'='1";
String sql = "SELECT * FROM users WHERE username = ?"
+ " AND password = ?";
System.out.println(sql);
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
System.out.println("登录成功!");
}else {
System.out.println("登录失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCTools.releaseDB(resultSet, preparedStatement, connection);
}
}
/**
* SQL 注入的问题
*/
@Test
public void testSQLInjection() {
String username = "Tom' OR PASSWORD = ";
String password = "OR '1'='1";
String sql = "SELECT * FROM users WHERE username = '" + username + "'"
+ " AND "+
" password = '" + password + " ' ";
System.out.println(sql);
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
System.out.println("登录成功!");
}else {
System.out.println("登录失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCTools.releaseDB(resultSet, statement, connection);
}
}
@Test
public void testPreparedStatement() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JDBCTools.getConnection();
String sql = "INSERT INTO customers(name,email,birth) "+
"VALUES(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "小红");
preparedStatement.setString(2, "[email protected]");
preparedStatement.setDate(3, new Date(new java.util.Date().getTime()));
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCTools.releaseDB(null, preparedStatement, connection);
}
}
工具类方法
/**
* 执行SQL语句,使用PrepareStatement
* @param sql
* @param args : 填写占位符的可变参数
*/
public static void update(String sql,Object ... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i+1, args[i]);
}
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCTools.releaseDB(null, preparedStatement, connection);
}
}