∪∩deniable Design

          個人JAVA版GAE(google app engine),struts2+jpa+jQuery開發,互相交流 http://iunbug.appspot.com/
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          [簡單]Java連接MySQL及防SQL注入

          Posted on 2008-07-27 18:08 ∪∩BUG 閱讀(1784) 評論(0)  編輯  收藏 所屬分類: JSP學習筆記
          (1)SQL注入獲取用戶密碼:
          1select * from student where Sno='0605030320' or '1'='1' and password='abc'
          (2)設置參數在底層防止注入
           1/**
           2 * 
           3 */

           4package user.DAO;
           5
           6import java.sql.*;
           7
           8import user.entity.User;
           9
          10/**
          11 * 用戶數據訪問層
          12 * @author Administrator
          13 *@version 1.0
          14 *日期2008 07 24
          15 */

          16public class UserDAO {
          17    /**
          18     * 根據用戶名和密碼查找用戶,找到了返回 user 對象,否則返回 null
          19     * @param(參量) user
          20     */

          21    public findUserByUsernamePassword(User user){
          22        // 1.注冊驅動
          23        try {
          24            Class.forName("com.mysql.jdbc.Driver");
          25        }
           catch (ClassNotFoundException e) {
          26            // TODO Auto-generated catch block
          27            e.printStackTrace();
          28        }
          //mysql驅動
          29        
          30        java.sql.Connection conn = null;
          31        PreparedStatement pstmt = null;    
          32        ResultSet rs = null;
          33        
          34        try {
          35            //2.獲取數據庫的連接(接數據庫)
          36            conn = java.sql.DriverManager.getConnection(
          37                    "jdbc:mysql://localhost/stums?useUnicode=true&characterEncoding=GBK","root","admin");
          38            
          39            //執行SQL
          40            pstmt = conn.prepareStatement("select * from student where Sno =? and Password = ?");
          41            
          42            //設置參數防止SQL注入
          43            pstmt.setString(1, user.getUsername());
          44            pstmt.setString(2, user.getPassword());
          45            
          46            //獲取數據集
          47            rs = pstmt.executeQuery();
          48            
          49            if(rs != null && rs.next()){
          50                user.setId(rs.getInt("Sno"));
          51                return user;
          52            }

          53            
          54        }
           catch (SQLException e) {
          55            // TODO Auto-generated catch block
          56            e.printStackTrace();
          57        }
          finally{    //釋放資源
          58            try {
          59                rs.close();     //關閉數據集
          60            }
           catch (Exception e) {
          61                // TODO Auto-generated catch block
          62                e.printStackTrace();
          63            }

          64            try {
          65                pstmt.close();    //關閉表連接
          66            }
           catch (Exception e) {
          67                // TODO Auto-generated catch block
          68                e.printStackTrace();
          69            }

          70            try {
          71                conn.close();    //關閉數據庫連接
          72            }
           catch (Exception e) {
          73                // TODO Auto-generated catch block
          74                e.printStackTrace();
          75            }

          76        }

          77        
          78        return null;
          79        
          80        
          81    }

          82
          83}

          84
          主站蜘蛛池模板: 自贡市| 海口市| 寿阳县| 洪雅县| 龙泉市| 云阳县| 阿克苏市| 萍乡市| 靖远县| 江都市| 贵溪市| 莱州市| 吉安市| 叙永县| 蓬溪县| 安顺市| 元江| 砚山县| 宁陕县| 阿坝县| 胶南市| 万全县| 台南县| 绵竹市| 涡阳县| 乌鲁木齐市| 乳源| 张掖市| 普兰店市| 紫金县| 乌拉特后旗| 陵水| 淅川县| 昌都县| 益阳市| 万宁市| 平昌县| 巴中市| 蓬安县| 南宁市| 遵义市|