mybatis学习日记(一)1-预编译PreparedStatement以及jdbc缺点
来源:互联网 发布:苹果医药软件 编辑:程序博客网 时间:2024/05/19 01:09
预编译的优点
PreparedStatement是预编译的,对于批量处理可以大大提高效率,也叫JDBC存储过程。
使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。
statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得, * preparedstatement支持批处理*
PreparedStatement对象不仅包含了SQL语句,而且大多数情况下这个语句已经被预编译过,因而当其执行时,只需DBMS运行SQL语句,而不必先编译。当你需要执行Statement对象多次的时候,PreparedStatement对象将会大大降低运行时间,当然也加快了访问数据库的速度。这种转换也带来很大的便利,不必重复SQL语句的句法,而只需更改其中变量的值,便可重新执行SQL语句。选择PreparedStatement对象与否,在于相同句法的SQL语句是否执行了多次,而且两次之间的差别仅仅是变量的不同。如果仅仅执行了一次的话,它应该和普通的对象毫无差异,体现不出它预编译的优越性。
PreparedStatement对象比Statement对象更有效,特别是如果带有不同参数的同一SQL语句被多次执行的时候。PreparedStatement对象允许数据库预编译SQL语句,这样在随后的运行中可以节省时间并增加代码的可读性。
-PreparedStatement还能有效防止sql注入。比如
/** * SQL 注入. */ @Test public void testSQLInjection() { String username = "a' 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); } } /** * 使用 PreparedStatement 将有效的解决 SQL 注入问题. */ @Test public void testSQLInjection2() { String username = "a' OR PASSWORD = "; String password = " OR '1'='1"; String sql = "SELECT * FROM users WHERE username = ? " + "AND password = ?"; 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); } }
可以参考另一篇文章:http://www.cnblogs.com/shellway/p/3933403.html
注意:
1、在执行这一语句ps = conn.prepareStatement(sql);的时候,如果提示要强制转换成prepareStatement类型,
则不要转换,只需要把包导入就行:import java.sql.PreparedStatement;
2、遇到插入日期语句的时候,比如:ps.setDate(3, new Date(new java.util.Date().getTime()));
前面的new Date()是要声明为java.sql包中的,里面的new Date()则要声明为java.util包中的。
预编译的核心代码
//预编译方式构建SQL查询:String sql = "select * from student where year(birthday) between ? and ?";PreparedStatement ps = conn.prepareStatement(sql);ps.setString(1, "1987");ps.setString(2, "1990");rs = ps.executeQuery();
预编译的简单范例
/** * 释放数据库资源的方法 * * @param resultSet * @param statement * @param connection */ public static void releaseDB(ResultSet resultSet, Statement statement, Connection connection) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }应用于查询结束finally{//关闭数据库releaseDB(resultSet, preparedStatement, connection);}
但是时常地(有需要时)打开和关闭数据库会造成数据库资源浪费,影响数据库性能,这个时候就会想到用数据库连接池(c3p0)。
/**PreparedStatement 预编译之查询栏目的范围 */ public ResultSet StartQuery(String sql,String s1, String s2) { getConnection(); try { pStatement = connection.prepareStatement(sql); pStatement.setString(1, s1); pStatement.setString(2, s2); rSet = pStatement.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rSet; }应用:System.out.println("生日范围查询:");ResultSet resultSet = jDemo1.StartQuery("select * from t_userr where year(birthdate) between ? and ?","1992","1992");//将数据库sql语句硬编译到java语句中,不能灵活应变,不利于系统维护,这个时候就会想到可以将sql语句以及参数写到xml文件中jDemo1.AllResult(resultSet);
对以上范例的拓展
/** PreparedStatement 预编译之查询拓展版第一个参数是SQL语句,第二个参数是查询的一个参数列表。(一数组形式存放) */ public ResultSet StartQueryLook(String sql,Object[] s) { getConnection(); try { pStatement = connection.prepareStatement(sql); for (int i = 0; i < s.length; i++) { pStatement.setObject(i+1, s[i]); } rSet = pStatement.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rSet; }应用://预编译之查询拓展版应用1 System.out.println("生日范围查询:"); String[] s1 = {"1992","1992"}; ResultSet resultSet = jDemo1.StartQueryLook("select * from t_userr where year(birthdate) between ? and ?",s1); jDemo1.AllResult(resultSet); //预编译之查询拓展版应用2 System.out.println("ID范围查询:"); String[] s2 = {"100","200"}; ResultSet resultSet2 = jDemo1.StartQueryLook("select * from t_userr where id between ? and ?",s2); jDemo1.AllResult(resultSet2);
预编译的拓展范例二(SQL增添、删除、修改)
/** PreparedStatement 预编译之增删改拓展版 */ public int StartQueryAll(String sql,Object[] objArr) { int count = 0; getConnection(); try { pStatement = connection.prepareStatement(sql); if(objArr!=null && objArr.length>0) { for (int i = 0; i < objArr.length; i++) { pStatement.setObject(i+1, objArr[i]); } } count = pStatement.executeUpdate();//insert, update 或 delete。 而不包含 select } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { close(); } return count; } 应用: //预编译之增删改拓展版:批量增加 for (int i = 0; i < 10; i++) { Object[] s3 = {10,10}; jDemo1.StartQueryAll("insert into jdbctest(username,password) values(?,?)",s3); } //预编译之增删改拓展版:批量删除 System.out.println("删除多条:"); jDemo1.StartQueryAll("delete from t_userr where id between ? and ?",new Object[]{"1010","1030"});
jdbc读取数据库从resultSet中遍历结果集,存在硬编码(写死的),不利于系统维护,所以最好能将结果集自动映射成java对象
由此产生了mybatis。
- mybatis学习日记(一)1-预编译PreparedStatement以及jdbc缺点
- 预编译PreparedStatement以及jdbc缺点
- JDBC:PreparedStatement预编译执行SQL语句
- mybatis(1)--对于jdbc的回顾以及其缺点总结
- 预编译语句(PreparedStatement)
- JAVA通过JDBC链接数据库获取数据(三)PreparedStatement预编译SQL
- JDBC 开发步骤及PreparedStatement预编译处理对象
- [疯狂Java]JDBC:PreparedStatement预编译执行SQL语句
- JDBC中的——PreparedStatement 预编译原理
- [疯狂Java]JDBC:PreparedStatement预编译执行SQL语句
- 从零开始学JDBC--1.7 PreparedStatement-- 预编译sql
- JDBC那些事(三)——PreparedStatement预编译对象
- jdbc中预编译语句PreparedStatement的深层分析
- JDBC的预编译语言对象PreparedStatement为什么优于Statement
- jdbc+sql+preparedStatement使用预编译实现增添数据
- Mybatis学习日记(一)
- Mybatis学习日记(一)
- PreparedStatement like 预编译
- <LeetCode OJ> 226. Invert Binary Tree
- Python知识点与应用
- CMake cache
- [IOS]NULL与nil的区别
- 编程练习 15.12.07~15.12.14
- mybatis学习日记(一)1-预编译PreparedStatement以及jdbc缺点
- Android实战之 自定义GitHub开源项目ViewPagerIndicator
- .NET学习(九)Timer控件的使用
- 最短的基本字符串
- 音乐播放器
- 编程练习 15.12.15~15.12.22
- 数据库入门学习总结1
- 编程练习 15.12.23~15.12.29
- 深入分析JavaWeb Item36 -- 过滤器Filter高级应用