日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

简单实现DButil工具类

發布時間:2023/12/9 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单实现DButil工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

DButil工具類的定義

定義一個工具類,類中實現連接方法,釋放資源方法,查詢方法,修改方法。

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型數據使用時可以直接Connection con = DButil.getConnection();來獲取連接 。
2、釋放資源

private static void close(Statement statement,Connection connection) {try {if (statement!=null) {statement.close();}} catch (SQLException e) {e.printStackTrace();}try {if (connection!=null) {connection.close();}} catch (SQLException e) {e.printStackTrace();}}private static void close(ResultSet result,Statement statement,Connection connection) {try {if (result!=null) {result.close();}} catch (SQLException e) {e.printStackTrace();}close(statement,connection);}

此方法在完成查詢或者修改操作后,根據所使用的資源情況進行調用不同的資源釋放方法。
3、查詢

public static void sel(IRowMapper rowMapper,String sql) {try {con = getConnection();sta = con.createStatement();result = sta.executeQuery(sql);rowMapper.rowMapper(result);} catch (Exception e) {e.printStackTrace();}finally {close(result, sta, con);}}

此查詢方法同樣使用了命令模式,方法參數列表中的IRowMapper為接口,使用該方法時需要創建該接口實現類,在實現類的rowMapper()方法中實現查詢。
4、修改

public static boolean upDate(String sql) {con = getConnection(); try {sta = con.createStatement();return sta.executeUpdate(sql)>0;} catch (SQLException e) {e.printStackTrace();}finally {close(result, sta, con);}return false;}

修改方法返回boolean型數據,在使用該方法時,傳入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);} }

執行結果:

但是查詢方法在實現登錄時會出現問題,如下

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='"+userName+"' and password='"+password+"'";class RowMapper implements IRowMapper{@Overridepublic void rowMapper(ResultSet res) {try {if (res.next()) {String userName = res.getString("user_name");System.out.println(userName+"登錄成功");return;}} catch (SQLException e) {e.printStackTrace();}System.out.println("賬號密碼錯誤");} }IRowMapper rowMapper = new RowMapper();DButil.sel(rowMapper, sql);} }

執行用戶登錄結果如下:



前兩種情況沒什么問題,但最后一種情況登錄成功顯然出現問題,數據庫表中顯然沒有用戶名:123
密碼:1’ or ‘1’='1 的信息,這種情況就是SQL注入,此時實際上傳入的sql語句已經變為:
select * from user_info where user_name=‘123’ and password=‘1’ or ‘1’=‘1’,顯然成立。
解決這個問題要重載查詢工具的代碼,如下:

public static boolean upDate(String sql,Object...arrays) {con = getConnection();PreparedStatement preparedStatement =null;try {preparedStatement = con.prepareStatement(sql);for (int i = 1; i <=arrays.length; i++) {preparedStatement.setObject(i, array[i-1]);//根據傳入參數個數進行逐次拼接。}return preparedStatement.executeUpdate()>0;//此時executeUpdate()不用傳入sql,因為sql語句已經執行。}catch(SQLException e) {e.printStackTrace();}finally {close(result, preparedStatement, con);}return false;}

在重載該方法時使用了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);//此處調用重載的sel方法} }

再次進行SQL注入:

防止SQL注入成功!
2、修改
簡單測試下修改中的刪除:

public class Update {public static void main(String[] args) {String sql = "delete from user_info";if (DBUtil.update(sql)) {System.out.println("YSE");}else {System.out.println("NO");}} }

執行結果:YES
為了防止SQL注入導致不受控制的修改表中信息,所以也將upDate()方法進行了重載,重載后如下:

//重載upDate public static boolean upDate(String sql,Object...arrays) {con = getConnection();PreparedStatement preparedStatement =null;try {preparedStatement = con.prepareStatement(sql);for (int i = 1; i <=arrays.length; i++) {preparedStatement.setObject(i, array[i-1]);}return preparedStatement.executeUpdate()>0;}catch(SQLException e) {e.printStackTrace();}finally {close(result, preparedStatement, con);}return false; }

調用重載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("設置密碼成功");}else {System.out.println("設置密碼失敗");}} }

該代碼演示了用戶設置密碼的過程,只為了測試SQL注入,實際過程并不這樣設置密碼。

總結

以上是生活随笔為你收集整理的简单实现DButil工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。