简单实现DButil工具类
DButil工具類的定義
定義一個工具類,類中實現(xiàn)連接方法,釋放資源方法,查詢方法,修改方法。
interface IRowMapper{void rowMapper(ResultSet rs); } public class DButil {static Connection con =null;static Statement sta =null;static String sql =null;static ResultSet result = null;public static Connection getConnection() {...}public static boolean upDate(String sql) {...}public static void sel(IRowMapper rowMapper,String sql) {...}private static void close(Statement statement,Connection connection) {...}private static void close(ResultSet result,Statement statement,Connection connection) {...} }1、連接方法
public static Connection getConnection() {try {Class.forName("com.mysql.jdbc.Driver");return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", ""); } catch (Exception e) {e.printStackTrace();}return null;}此方法返回一個Connection型數(shù)據(jù)使用時可以直接Connection con = DButil.getConnection();來獲取連接 。
2、釋放資源
此方法在完成查詢或者修改操作后,根據(jù)所使用的資源情況進行調(diào)用不同的資源釋放方法。
3、查詢
此查詢方法同樣使用了命令模式,方法參數(shù)列表中的IRowMapper為接口,使用該方法時需要創(chuàng)建該接口實現(xiàn)類,在實現(xiàn)類的rowMapper()方法中實現(xiàn)查詢。
4、修改
修改方法返回boolean型數(shù)據(jù),在使用該方法時,傳入sql語句,直接判斷upDate(sql)是否為真,為真則修改成功。
DButil工具類的使用
1、查詢
public class Test {public static void main(String[] args){String sql = "select * from user_info";class RowMapper implements IRowMapper{@Overridepublic void rowMapper(ResultSet res) {try {while (res.next()) {String student_id = res.getString("id");String userName = res.getString("user_name");String password = res.getString("password");System.out.println(student_id+" ,"+userName+" ,"+password);}} catch (SQLException e) {e.printStackTrace();}} }IRowMapper rowMapper = new RowMapper();DBUtil.select(sql, rowMapper);} }執(zhí)行結(jié)果:
但是查詢方法在實現(xiàn)登錄時會出現(xiàn)問題,如下
執(zhí)行用戶登錄結(jié)果如下:
前兩種情況沒什么問題,但最后一種情況登錄成功顯然出現(xiàn)問題,數(shù)據(jù)庫表中顯然沒有用戶名:123
密碼:1’ or ‘1’='1 的信息,這種情況就是SQL注入,此時實際上傳入的sql語句已經(jīng)變?yōu)?#xff1a;
select * from user_info where user_name=‘123’ and password=‘1’ or ‘1’=‘1’,顯然成立。
解決這個問題要重載查詢工具的代碼,如下:
在重載該方法時使用了PreparedStatement類,繼承于Statement。
public class Login {public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("請輸入用戶名:");String userName= sc.nextLine();System.out.println("請輸入密碼:");String password = sc.nextLine();String sql = "select * from user_info where user_name =? and password =?";class RowMapper implements IRowMapper{@Overridepublic void rowMapper(ResultSet res) {try {if (res.next()) {String userName = res.getString("user_name");System.out.println(sql);System.out.println(userName+"登錄成功");return;}} catch (SQLException e) {e.printStackTrace();}System.out.println("賬號密碼錯誤");} }IRowMapper rowMapper = new RowMapper();DButil.sel(rowMapper, sql,userName,password);//此處調(diào)用重載的sel方法} }再次進行SQL注入:
防止SQL注入成功!
2、修改
簡單測試下修改中的刪除:
執(zhí)行結(jié)果:YES
為了防止SQL注入導致不受控制的修改表中信息,所以也將upDate()方法進行了重載,重載后如下:
調(diào)用重載update()的方法如下
public class Test {public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("請輸入用戶名:");String userName= sc.nextLine();System.out.println("請輸入密碼:");String password = sc.nextLine();String sql = "UPDATE user_info SET password = ? WHERE user_name = ? ";if (DButil.upDate(sql,password,userName)) {System.out.println("設(shè)置密碼成功");}else {System.out.println("設(shè)置密碼失敗");}} }該代碼演示了用戶設(shè)置密碼的過程,只為了測試SQL注入,實際過程并不這樣設(shè)置密碼。
總結(jié)
以上是生活随笔為你收集整理的简单实现DButil工具类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux程序调试命令strace
- 下一篇: SharePoint 2013 List