JDBCUtil 连接MYSQL数据库Java工具类

 1 package com.reliable.util;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 public class JDBCUtil {
 9     private static  Connection connection ;
10     private static  String url="jdbc:mysql://localhost:3306/file_insert?useUnicode=true&CharacterEncoding=utf-8";
11     private static  String user="root";
12     private static  String password="000429";
13     static {
14         try {
15             Class.forName("com.mysql.jdbc.Driver");
16         } catch (ClassNotFoundException e) {
17             // TODO 自动生成的 catch 块
18             e.printStackTrace();
19         }
20     }
21     public static Connection getConnection() {
22         try {
23             connection=(Connection) DriverManager.getConnection(url,user,password);
24         } catch (SQLException e) {
25             // TODO 自动生成的 catch 块
26             e.printStackTrace();
27         }
28 
29         return connection;
30     }
31     public static void release(Connection connection,PreparedStatement preparedStatement ,ResultSet resultSet)
32     {
33         if(connection!=null) {
34             try {
35                 connection.close();
36             } catch (SQLException e) {
37                 // TODO 自动生成的 catch 块
38                 e.printStackTrace();
39             }
40         }
41         if(preparedStatement!= null) {
42             try {
43                 preparedStatement.close();
44             } catch (SQLException e) {
45                 // TODO 自动生成的 catch 块
46                 e.printStackTrace();
47             }
48         }
49         if(resultSet != null) {
50             try {
51                 resultSet.close();
52             } catch (SQLException e) {
53                 // TODO 自动生成的 catch 块
54                 e.printStackTrace();
55             }
56         }
57     }
58     public static void release(Connection connection,PreparedStatement preparedStatement)
59     {
60         if(connection!=null) {
61             try {
62                 connection.close();
63             } catch (SQLException e) {
64                 // TODO 自动生成的 catch 块
65                 e.printStackTrace();
66             }
67         }
68         if(preparedStatement!= null) {
69             try {
70                 preparedStatement.close();
71             } catch (SQLException e) {
72                 // TODO 自动生成的 catch 块
73                 e.printStackTrace();
74             }
75         }
76     }
77 }

 

posted @ 2022-03-31 10:54  靠谱杨  阅读(93)  评论(0编辑  收藏  举报