设为首页 加入收藏

TOP

JDBCMYSQL学习笔记(一)JDBC基本使用(三)
2015-11-21 01:37:29 来源: 作者: 【 】 浏览:4
Tags:JDBCMYSQL 学习 笔记 JDBC 基本 使用
nt.executeBatch(); preparedStatement.close(); //执行完后手动提交事务 connection.commit(); //打开自动提交 connection.setAutoCommit(true); connection.close(); }

11、SQL 注入的防范

SQL 注入是用户利用某些系统没有对输入数据进行充分的检查,从而进行恶意破坏的行为。
1、statement存在sql注入攻击问题,例如登陆用户名采用' or 1=1 or username=‘
2、对于防范 SQL 注入,可以采用PreparedStatement取代Statement。
备注:本例只是最基本的防止SQL注入方式,其他情况还请查阅资料。

12、PreparedStatement

PreperedStatement是Statement的孩子,它的实例对象可以通过调用Connection.preparedStatement()方法获得,相对于Statement对象而言的优势:
(1) 防止SQL注入:PreperedStatement可以避免SQL注入的问题。
(2) 预编译SQL语句:Statement会使数据库频繁编译SQL,可能造成数据库缓冲区溢出。PreparedStatement 可对SQL进行预编译,从而提高数据库的执行效率。
(3) 使用占位符简化语句:并且PreperedStatement对于sql中的参数,允许使用占位符的形式进行替换,简化sql语句的编写。 (例如多次循环插入数据)

?

?

public List
  
    getAll(){
 
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try{
              conn = JdbcUtils.getConnection();
              String sql = "select * from customer";
              st = conn.prepareStatement(sql);
              rs = st.executeQuery();
              List list = new ArrayList();
              while(rs.next()){
 
                    Customer c = new Customer();
 
                    c.setBirthday(rs.getDate("birthday"));
 
                    c.setCellphone(rs.getString("cellphone"));
 
                    c.setDescription(rs.getString("description"));
 
                    c.setEmail(rs.getString("email"));
 
                    c.setGender(rs.getString("gender"));
 
                    c.setId(rs.getString("id"));
 
                    c.setName(rs.getString("name"));
 
                    c.setPreference(rs.getString("preference"));
 
                    c.setType(rs.getString("type"));
 
                    list.add(c);
              }
              return list;
        }catch (Exception e) {
              throw new DaoException(e);
        }finally{
              JdbcUtils.release(conn, st, rs);
        }
  }
 
  
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Oracle GoldenGate 四、数据过滤.. 下一篇小贝_mysql主从复制作用以及案例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: