PreparedStatement vs Statement
1、代码的可读性和可维护性
2、PreparedStatement能最大可能提高性能
- DBServer会对预编译语句提供优化,因为预编译语句有可能被重复调用,所以在语句被DBServer的编译器编译后的代码被缓存下来,那么下次调用时只要是相同的预编译语句就不再需要再次编译,只需要将参数直接传入编译过的语句执行代码中就会得到执行
- 在statement语句中,即使是相同的操作但数据内容不一样,所以整个语句本身不能匹配,没有缓存的意义,事实是没有数据库会对普通的语句编译后的执行代码保存,这样每执行一次都要对传入的语句编译一次
- 语法检查,语义检查,翻译成二进制命令,缓存
3、PreparedStatement 可以防止sql注入
方式一,使用statement类,但是不建议使用,这里只讨论使用预编译的statement类
方式二:
//方式二
public class PreparedStatement_Insert1 {
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
//获取数据库连接
Connection connection = DatabaseConnectivity.getConnection("jdbc.properties");
String sql="INSERT INTO goods(`name`) VALUES(?) ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
long start = System.currentTimeMillis();
for(int i=1;i<=20000;i++){
preparedStatement.setObject(1,"nama_"+i);
preparedStatement.execute();
System.out.println(i);
}
long end=System.currentTimeMillis();
System.out.println("共花费的时间为:"+(end-start));
//释放资源
DatabaseConnectivity.releaseResources(connection,preparedStatement);
}
}
方式三:使用缓冲技术
class PreparedStatement_Insert2{
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
//获取数据库连接
Connection connection = DatabaseConnectivity.getConnection("jdbc.properties");
String sql="INSERT INTO goods(`name`) VALUES(?) ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
long start = System.currentTimeMillis();
for(int i=1;i<=200000;i++){
//填充占位符
preparedStatement.setObject(1,"nama_"+i);
preparedStatement.addBatch();
if(i%1000==0){
//缓冲sql
preparedStatement.executeBatch();
//清空缓冲
preparedStatement.clearBatch();
}
}
long end=System.currentTimeMillis();
System.out.println("共花费的时间为:"+(end-start)/1000.0+"秒");
//释放资源
DatabaseConnectivity.releaseResources(connection,preparedStatement);
}
}
方式四:减少提交次数,等数据都插入完成后再提交
//方式四:减少提交次数
class PreparedStatement_Insert3{
public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
//获取数据库连接
Connection connection = DatabaseConnectivity.getConnection("jdbc.properties");
//设置不可提交
connection.setAutoCommit(false);
String sql="INSERT INTO goods(`name`) VALUES(?) ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
long start = System.currentTimeMillis();
for(int i=1;i<=200000;i++){
//填充占位符
preparedStatement.setObject(1,"nama_"+i);
preparedStatement.addBatch();
if(i%1000==0){
//缓冲sql
preparedStatement.executeBatch();
//清空缓冲
preparedStatement.clearBatch();
}
}
//提交数据
connection.commit();
long end=System.currentTimeMillis();
System.out.println("共花费的时间为:"+(end-start)/1000.0+"秒");
//释放资源
DatabaseConnectivity.releaseResources(connection,preparedStatement);
}
}
可以看出,方式四效率最高 ,在使用事务时,应该使用同一个连接,因为断开连接之后,就已经自动提交了
另外在这里复制一下如何开启数据据库的批处理数据功能
allowMultiQueries=true
可以在sql语句后携带分号,实现多语句执行。
可以执行批处理,同时发出多个SQL语句。
rewriteBatchedStatements=true
5.1.13以上版本的驱动,添加此参数可实现高性能的批量插入。
MySQL JDBC驱动在默认情况下会无视executeBatch()语句,把我们期望批量执行的一组sql语句拆散,一条一条地发给MySQL数据库,批量插入实际上是单条插入,直接造成较低的性能。
只有把rewriteBatchedStatements参数置为true, 驱动才会帮你批量执行SQL
另外这个选项对INSERT/UPDATE/DELETE都有效