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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

JAVA学习心得——DBUtil工具类

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA学习心得——DBUtil工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

接口:

package com.jd.util; import java.sql.ResultSet;public interface IRowMapper{void rowMapper(ResultSet result);}

工具類:
提供防止SQL注入和不防止SQL注入兩種方法:

package com.jd.util;import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;/*** 數據庫工具* * @author 馮申然*/ public class DBUtil {/*** 程序驅動* * @author 馮申然*/static {try {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {e.printStackTrace();}}/*** 連接mysql數據庫* * @author 馮申然*/public static Connection getconnection() {try {return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test",root, root);} catch (SQLException e) {e.printStackTrace();}return null; }/*** 數據的增刪改查* * @author 馮申然*/public static boolean updata(String sql) {Connection connection =null;Statement statement =null;try {connection=getconnection();statement = connection.createStatement();int result = statement.executeUpdate(sql);return result>0;} catch (Exception e) {e.printStackTrace();}finally {close(statement,connection);}return false;}/*** 數據增刪改查* @author 馮申然*/public static boolean updata(String sql,Object...params) {Connection connection =null;PreparedStatement prepareStatement=null;try {connection=getconnection();prepareStatement = connection.prepareStatement(sql);for(int i=1;i<=params.length;i++) {prepareStatement.setObject(i, params[i-1]);}int result = prepareStatement.executeUpdate();return result>0;} catch (Exception e) {e.printStackTrace();}finally {close(prepareStatement,connection);}return false;}/*** 登錄設置* * @author 馮申然*/public static void select(String sql,IRowMapper rowMapper,Object...params) {//登錄設置ResultSet result =null;Statement statement =null;Connection connection =null;PreparedStatement preparedStatement =null;try {connection=getconnection();preparedStatement =connection.prepareStatement(sql);for(int i=1;i<=params.length;i++) {preparedStatement.setObject(i, params[i-1]);}result =preparedStatement.executeQuery();rowMapper.rowMapper(result);} catch (Exception e) {e.printStackTrace();}finally {close(result,statement,connection);}}/*** 檢驗數據是否存在* * @author 馮申然*/public static boolean exist (String sql) {//是否存在class RowMapper implements IRowMapper{boolean state;@Overridepublic void rowMapper(ResultSet result) {try {state=result.next();} catch (SQLException e) {e.printStackTrace();}} }RowMapper rowMapper =new RowMapper();select(sql,rowMapper);return rowMapper.state;}/*** 檢驗數據是否存在* * @author 馮申然*/public static boolean exist (String sql,Object...params) {//是否存在class RowMapper implements IRowMapper{boolean state;@Overridepublic void rowMapper(ResultSet result) {try {state=result.next();} catch (SQLException e) {e.printStackTrace();}} }RowMapper rowMapper =new RowMapper();select(sql,rowMapper,params);return rowMapper.state;}/*** 查詢數據* * @author 馮申然*/public static void select(String sql,IRowMapper rowMapper) {//查詢Connection connection =null;Statement statement =null;ResultSet result =null;try {connection=getconnection();statement = connection.createStatement();result = statement.executeQuery(sql);rowMapper.rowMapper(result);} catch (Exception e) {e.printStackTrace();}finally {close(result,statement,connection);} }/*** 查詢數據* * @author 馮申然*/public static void Select(String sql,IRowMapper rowMapper,Object...params) {//查詢Connection connection =null;Statement statement =null;ResultSet result =null;PreparedStatement preparedStatement =null;try {connection=getconnection();preparedStatement =connection.prepareStatement(sql);for(int i=1;i<=params.length;i++) {preparedStatement.setObject(i, params[i-1]);}result =preparedStatement.executeQuery() ;rowMapper.rowMapper(result);} catch (Exception e) {e.printStackTrace();}finally {close(result,statement,connection);} }/*** 釋放資源* * @author 馮申然*/private static void close(Statement statement,Connection connection){if (statement!=null) {try {statement.close();} catch (SQLException e) {e.printStackTrace();} }if (connection!=null) {try {connection.close();} catch (SQLException e) {e.printStackTrace();} }}private static void close(ResultSet result,Statement statement,Connection connection){if (result!=null) {try {result.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();} }} }

總結

以上是生活随笔為你收集整理的JAVA学习心得——DBUtil工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

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