JDBC连接数据库工具类

/*
   1、注册驱动(DRIVER)
   2、建立连接(URL,USER,PASSWORD)
   3、编译sql
   4、执行sql
   5、获得结果集
   6、返回
 */

/**
 * 1.常量定义 final,最终、不变,常量标识符全大写
 * 2.定义常量类尽量不要用public
 * 3.需要经常调用的使用static修饰
 */
private static final String URL = "jdbc:mysql://127.0.0.1:3306/ylws?
                        serverTimezone=UTC&characterEncoding=utf-8";
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String USER = "root";
private static final String PASSWORD = "123456";

JDBC工具类

  1 public class JDBCUtils {
  2     /*
  3        1、注册驱动(DRIVER)
  4        2、建立连接(URL,USER,PASSWORD)
  5        3、编译sql
  6        4、执行sql
  7        5、获得结果集
  8        6、返回
  9      */
 10 
 11     /**
 12      * 1.常量定义 final,最终、不变,常量标识符全大写
 13      * 2.定义常量类尽量不要用public
 14      * 3.需要经常调用的使用static修饰
 15      */
 16     private static final String URL = "jdbc:mysql://127.0.0.1:3306/ylws?serverTimezone=UTC&characterEncoding=utf-8";
 17     private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
 18     private static final String USER = "root";
 19     private static final String PASSWORD = "123456";
 20 
 21     /*
 22         1、建立连接
 23         2、执行语句
 24         3、返回结果
 25      */
 26     private static Connection connection = null;
 27     //PreparedStatement比Statement访问速度更快。能防止sql注入
 28     private static PreparedStatement preparedStatement = null;
 29     private static ResultSet resultSet;
 30 
 31     /**
 32      * 建立连接
 33      * 只执行一次
 34      */
 35     private static Connection getConnection() {
 36         try {
 37             //利用反射,加载驱动
 38             Class.forName(DRIVER);
 39             //建立连接
 40             connection = DriverManager.getConnection(URL, USER, PASSWORD);
 41              /*
 42                 位运算符:&、|、!
 43                 逻辑运算符:&&、||、^
 44                 单目运算符:~
 45                 一、&&和&的区别
 46                    1、相同点
 47                       1)&和&&都可以用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,
 48                        整个运算结果才为true,否则,只要有一方为false,则结果为false。
 49                    2、不同点
 50                       1)&&具有短路的功能,即如果第一个表达式为false,则不再计算第二个表达式
 51                       2)&作为为运算时,&是按位与操作,参加运算的两个数据按照二进制位进行“与”运算。
 52                 二、|| 和 | 的区别
 53                    1、相同点
 54                       1)当二者表示或的时候,二者都false则false,只有一个为true的时则true。
 55                    2、不同点
 56                       1)|| 也存在短路的问题,当前者为true时,则不会判断后面的表达(与上面的&&类似)
 57                       2)| 是按位或操作,参加运算的两个数据按照二进制位进行“或”运算。
 58                 三、~与!的区别
 59                    1、~是一个单目运算符,用来对一个二进制位按位取反,即0变成1,1变成0,比如~00001111=11110000
 60                    2、!是逻辑非运算符,!a的意思是:若a为真,则!a为假,若a为假则!a为真。
 61                 四、逻辑异或^
 62                    1、a^b,a 与 b 相异时,结果为 true ,相同为false,例如1^0=1,0^0=0,1^1=0,0^1=1,1表示true
 63              */
 64         } catch (SQLException | ClassNotFoundException e) {
 65             e.printStackTrace();
 66         }
 67         return connection;
 68     }
 69 
 70     /**
 71      * 执行增加、删除、修改操作
 72      *
 73      * @param sql sql语句
 74      * @return 影响行数
 75      */
 76     public static int excuteUpdate(String sql) {
 77         getConnection();
 78         int count = 0;
 79         try {
 80             preparedStatement = connection.prepareStatement(sql);
 81             //修改
 82             count = preparedStatement.executeUpdate();
 83         } catch (SQLException e) {
 84             e.printStackTrace();
 85         } finally {
 86             close();
 87         }
 88         return count;
 89     }
 90 
 91     /**
 92      * 执行查询操作
 93      *
 94      * @param sql sql语句
 95      * @return 查询结果
 96      */
 97     public static ResultSet getResultSet(String sql) {
 98         getConnection();
 99         try {
100             preparedStatement = connection.prepareStatement(sql);
101             //查询
102             resultSet = preparedStatement.executeQuery();
103         } catch (SQLException e) {
104             e.printStackTrace();
105         }
106         //在查询结束时关闭
107 //        finally {
108 //            close();
109 //        }
110         return resultSet;
111     }
112 
113     /**
114      * 关闭连接
115      */
116     private static void close() {
117         if (preparedStatement != null) {
118             try {
119                 preparedStatement.close();
120                 preparedStatement = null;
121             } catch (SQLException e) {
122                 e.printStackTrace();
123             }
124         }
125 
126         if (connection != null) {
127             try {
128                 connection.close();
129                 connection = null;
130             } catch (SQLException e) {
131                 e.printStackTrace();
132             }
133         }
134         if (resultSet != null) {
135             try {
136                 resultSet.close();
137                 resultSet = null;
138             } catch (SQLException e) {
139                 e.printStackTrace();
140             }
141         }
142     }
143 
144 }
作者:donleo123
本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
原文地址:https://www.cnblogs.com/donleo123/p/14070518.html