PreparedStatement vs Statement

来源:互联网 发布:震爆雷升级数据 编辑:程序博客网 时间:2024/06/16 23:42

二者异同:

代码的可读性和可维护性. 

PreparedStatement 能最大可能提高性能:
  •      DBServer会对预编译语句提供性能优化。因为预编译语句有可能被重复调用,所以语句在被DBServer的编译器编译后的执行代码被缓存下来,那么下次调用时只要是相同的预编译语句就不需要编译,只要将参数直接传入编译过的语句执行代码中就会得到执行。
  •    在statement语句中,即使是相同操作但因为数据内容不一样,所以整个语句本身不能匹配,没有缓存语句的意义.事实是没有数据库会对普通语句编译后的执行代码缓存.这样每执行一次都要对传入的语句编译一次.  
  •    (语法检查,语义检查,翻译成二进制命令,缓存)

PreparedStatement 可以防止 SQL 注入 。


代码:

package com.atguigu.java;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.Statement;import org.junit.Test;//批量操作:主要指的是批量插入//分别使用PreparedStatement和Statement向Oracle数据库的某个表中插入100000条数据,//对比二者的效率public class TestJDBC1 {// 使用PreparedStatement执行插入操作2,结合addBatch() executeBatch() clearBatch()//结果显示:插入10万条数据花费时间不到1秒 ,推荐使用此方法,@Test public void test4() {Connection conn = null;PreparedStatement ps = null;try {conn = JDBCUtils.getConnection();long start = System.currentTimeMillis();String sql = "insert into dept values(?,?)";ps = conn.prepareStatement(sql);for (int i = 0; i < 100000; i++) {ps.setInt(1, i + 1);ps.setString(2, "dept_" + (i + 1));//1."攒sql"ps.addBatch();//2.执行if((i + 1) % 250 == 0){//利用统计学可以找到一个效率最高的峰值,这里我们选用250能被100000整除ps.executeBatch();//3.清空ps.clearBatch();}}long end = System.currentTimeMillis();System.out.println("花费的时间为:" + (end - start));// 47052-801} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {JDBCUtils.close(null, ps, conn);}}// 使用Statement执行插入操作2,结合addBatch() executeBatch() clearBatch()//结果发现效率并没有提升@Testpublic void test3() {Connection conn = null;Statement st = null;try {conn = JDBCUtils.getConnection();long start = System.currentTimeMillis();st = conn.createStatement();for (int i = 0; i < 100000; i++) {String sql = "insert into dept values(" + (i + 1) + ",'dept_"+ (i + 1) + "')";// 1.“攒”sqlst.addBatch(sql);if ((i + 1) % 250 == 0) {//2.st.executeBatch();//3.st.clearBatch();}}long end = System.currentTimeMillis();System.out.println("花费的时间为:" + (end - start));// 98278-98957} catch (Exception e) {e.printStackTrace();} finally {JDBCUtils.close(null, st, conn);}}// 使用PreparedStatement执行插入操作1:明显能提高效率,但是还能改进@Testpublic void test2() {Connection conn = null;PreparedStatement ps = null;try {conn = JDBCUtils.getConnection();long start = System.currentTimeMillis();String sql = "insert into dept values(?,?)";ps = conn.prepareStatement(sql);for (int i = 0; i < 100000; i++) {ps.setInt(1, i + 1);ps.setString(2, "dept_" + (i + 1));ps.execute();}long end = System.currentTimeMillis();System.out.println("花费的时间为:" + (end - start));// 47052} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {JDBCUtils.close(null, ps, conn);}}// 使用Statement执行插入操作1//发现效率很低,原因是每次插入时都要对sql语句进行语法校验,然后执行每一条SQL语句@Testpublic void test1() {Connection conn = null;Statement st = null;try {conn = JDBCUtils.getConnection();long start = System.currentTimeMillis();st = conn.createStatement();for (int i = 0; i < 100000; i++) {String sql = "insert into dept values(" + (i + 1) + ",'dept_"+ (i + 1) + "')";st.execute(sql);}long end = System.currentTimeMillis();System.out.println("花费的时间为:" + (end - start));// 98278} catch (Exception e) {e.printStackTrace();} finally {JDBCUtils.close(null, st, conn);}}@Testpublic void test0() throws Exception {Connection conn = JDBCUtils.getConnection();System.out.println(conn);}}


0 0
原创粉丝点击