1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程
2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。
3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得, preparedstatement支持批处理
4.
Code Fragment 1:
String updateString = "UPDATE COFFEES SET SALES = 75 " + "WHERE COF_NAME LIKE ′Colombian′";
stmt.executeUpdate(updateString);
Code Fragment 2:
PreparedStatement updateSales = con.prepareStatement("UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75);
updateSales.setString(2, "Colombian");
updateSales.executeUpdate();
片断2和片断1的区别在于,后者使用了PreparedStatement对象,而前者是普通的Statement对象。PreparedStatement对象不仅包含了SQL语句,而且大多数情况下这个语句已经被预编译过,因而当其执行时,只需DBMS运行SQL语句,而不必先编译。当你需要执行Statement对象多次的时候,PreparedStatement对象将会大大降低运行时间,当然也加快了访问数据库的速度。
这种转换也给你带来很大的便利,不必重复SQL语句的句法,而只需更改其中变量的值,便可重新执行SQL语句。选择PreparedStatement对象与否,在于相同句法的SQL语句是否执行了多次,而且两次之间的差别仅仅是变量的不同。如果仅仅执行了一次的话,它应该和普通的对象毫无差异,体现不出它预编译的优越性。
5.执行许多SQL语句的JDBC程序产生大量的Statement和PreparedStatement对象。通常认为PreparedStatement对象比Statement对象更有效,特别是如果带有不同参数的同一SQL语句被多次执行的时候。PreparedStatement对象允许数据库预编译SQL语句,这样在随后的运行中可以节省时间并增加代码的可读性。
然而,在Oracle环境中,开发人员实际上有更大的灵活性。当使用Statement或PreparedStatement对象时,Oracle数据库会缓存SQL语句以便以后使用。在一些情况下,由于驱动器自身需要额外的处理和在Java应用程序和Oracle服务器间增加的网络活动,执行PreparedStatement对象实际上会花更长的时间。
然而,除了缓冲的问题之外,至少还有一个更好的原因使我们在企业应用程序中更喜欢使用PreparedStatement对象,那就是安全性。传递给PreparedStatement对象的参数可以被强制进行类型转换,使开发人员可以确保在插入或查询数据时与底层的数据库格式匹配。
当处理公共Web站点上的用户传来的数据的时候,安全性的问题就变得极为重要。传递给PreparedStatement的字符串参数会自动被驱动器忽略。最简单的情况下,这就意味着当你的程序试着将字符串“D'Angelo”插入到VARCHAR2中时,该语句将不会识别第一个“,”,从而导致悲惨的失败。几乎很少有必要创建你自己的字符串忽略代码。
在Web环境中,有恶意的用户会利用那些设计不完善的、不能正确处理字符串的应用程序。特别是在公共Web站点上,在没有首先通过PreparedStatement对象处理的情况下,所有的用户输入都不应该传递给SQL语句。此外,在用户有机会修改SQL语句的地方,如HTML的隐藏区域或一个查询字符串上,SQL语句都不应该被显示出来。
然而,在Oracle环境中,开发人员实际上有更大的灵活性。当使用Statement或PreparedStatement对象时,Oracle数据库会缓存SQL语句以便以后使用。在一些情况下,由于驱动器自身需要额外的处理和在Java应用程序和Oracle服务器间增加的网络活动,执行PreparedStatement对象实际上会花更长的时间。
然而,除了缓冲的问题之外,至少还有一个更好的原因使我们在企业应用程序中更喜欢使用PreparedStatement对象,那就是安全性。传递给PreparedStatement对象的参数可以被强制进行类型转换,使开发人员可以确保在插入或查询数据时与底层的数据库格式匹配。
当处理公共Web站点上的用户传来的数据的时候,安全性的问题就变得极为重要。传递给PreparedStatement的字符串参数会自动被驱动器忽略。最简单的情况下,这就意味着当你的程序试着将字符串“D'Angelo”插入到VARCHAR2中时,该语句将不会识别第一个“,”,从而导致悲惨的失败。几乎很少有必要创建你自己的字符串忽略代码。
在Web环境中,有恶意的用户会利用那些设计不完善的、不能正确处理字符串的应用程序。特别是在公共Web站点上,在没有首先通过PreparedStatement对象处理的情况下,所有的用户输入都不应该传递给SQL语句。此外,在用户有机会修改SQL语句的地方,如HTML的隐藏区域或一个查询字符串上,SQL语句都不应该被显示出来。
我通过一段测试代码 完成防止SQL注入功能的验证
/*
* Copyright (c) 2013, 360buy Group and/or its affiliates. All rights reserved.
*/
package com.yh.java.sqlinjection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @version 2013-11-18
* @author dbyanghao
*/
public class SqlTest {
public static void main(String[] args) {
testSql("liming","123456","statement");
testSql("liming","12345","statement");
testSql("liming' or '1'='1","12345","statement");
testSql("liming","123456","preparedStatemet");
testSql("liming","12345","preparedStatemet");
testSql("liming' or '1'='1","12345","preparedStatemet");
}
public static void testSql(String name,String pwd,String type)
{
if(type.equals("statement"))
{
boolean statementTestResult = statementTest(name,pwd);
if(statementTestResult==true)
{
System.out.println("用户名密码正确");
}else
{
System.out.println("用户名密码错误");
}
}
if(type.equals("preparedStatemet"))
{
boolean preparedStatemetResult = preparedStatemetTest(name,pwd);
if(preparedStatemetResult==true)
{
System.out.println("用户名密码正确");
}else
{
System.out.println("用户名密码错误");
}
}
}
public static boolean statementTest(String name,String pwd)
{
boolean result = false;
//创建用于连接数据库的Connection对象
Connection conn = null;
//创建Statement对象
Statement st = null;
try {
// 加载Mysql数据驱动
Class.forName("com.mysql.jdbc.Driver");
// 创建数据连接
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3307/test", "root", "root");
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
try {
// 查询数据的sql语句
String sql = "select * from test where username='" +name+ "' and pwd='"+pwd +"'";
System.out.println(sql);
//创建用于执行静态sql语句的Statement对象,st属局部变量
st = (Statement) conn.createStatement();
//执行sql查询语句,返回查询数据的结果集
ResultSet rs = st.executeQuery(sql);
// 判断是否还有下一个数据
while (rs.next()) {
result = true;
String rname = rs.getString("username");
String rpwd = rs.getString("pwd");
System.out.println("用户名,密码为:"+rname+"--"+rpwd);
}
//关闭数据库连接
conn.close();
} catch (SQLException e) {
System.out.println("查询数据失败");
}
return result;
}
public static boolean preparedStatemetTest(String name,String pwd)
{
boolean result = false;
//创建PreparedStatement对象
PreparedStatement pstmt = null;
//创建连接对象
Connection conn = null;
try {
// 加载Mysql数据驱动
Class.forName("com.mysql.jdbc.Driver");
// 创建数据连接
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3307/test", "root", "root");
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
try {
// 查询数据的sql语句
String sql = "select * from test where username= ? and pwd= ?";
//使用PreparedStatement对象里来构建并执行SQL语句,2个问号代表2个字段预先要保留的值
pstmt = conn.prepareStatement(sql);
//通过PreparedStatement对象里的set方法去设置插入的具体数值
pstmt.setString(1, name);
pstmt.setString(2, pwd);
System.out.println(sql+"/1:"+name+"/2:"+pwd);
//执行sql查询语句,返回查询数据的结果集
ResultSet rs = pstmt.executeQuery();
// 判断是否还有下一个数据
while (rs.next()) {
result = true;
String rname = rs.getString("username");
String rpwd = rs.getString("pwd");
System.out.println("用户名,密码为:"+rname+"--"+rpwd);
}
//关闭数据库连接
conn.close();
} catch (SQLException e) {
System.out.println("查询数据失败");
}finally {
try {
if(pstmt != null) {
pstmt.close();
pstmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
//捕获SQL异常
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}
执行结果为:
select * from test where username='liming' and pwd='123456'
用户名,密码为:liming--123456
用户名密码正确
select * from test where username='liming' and pwd='12345'
用户名密码错误
select * from test where username='liming' or '1'='1' and pwd='12345'
用户名,密码为:liming--123456
用户名密码正确
select * from test where username= ? and pwd= ?/1:liming/2:123456
用户名,密码为:liming--123456
用户名密码正确
select * from test where username= ? and pwd= ?/1:liming/2:12345
用户名密码错误
select * from test where username= ? and pwd= ?/1:liming' or '1'='1/2:12345
用户名密码错误
用户名,密码为:liming--123456
用户名密码正确
select * from test where username='liming' and pwd='12345'
用户名密码错误
select * from test where username='liming' or '1'='1' and pwd='12345'
用户名,密码为:liming--123456
用户名密码正确
select * from test where username= ? and pwd= ?/1:liming/2:123456
用户名,密码为:liming--123456
用户名密码正确
select * from test where username= ? and pwd= ?/1:liming/2:12345
用户名密码错误
select * from test where username= ? and pwd= ?/1:liming' or '1'='1/2:12345
用户名密码错误
防止sql注入还要在先后台进行安全字符过滤。