图书管理系统——DBUtil层
DBUtil.java
1 import java.beans.Statement;
2 import java.sql.Connection;
3 import java.sql.DriverManager;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6
7
8 public class DButil {
9 private static String mysqlname = "library";
10 private static Connection con;
11 private static Statement sta;
12 private static ResultSet re;
13 private static String coursename = "com.mysql.cj.jdbc.Driver";
14 private static String url = "jdbc:mysql://localhost:3306/"+mysqlname+"?serverTimezone=UTC";
15
16 //注册驱动
17 public static Connection getCon() {
18 try {
19 Class.forName(coursename);
20 System.out.println("驱动加载成功");
21 }catch(ClassNotFoundException e) {
22 e.printStackTrace();
23 }
24 try {
25 con = DriverManager.getConnection(url,"root","123asd..00");
26 System.out.println("连接成功");
27 }catch(Exception e){
28 e.printStackTrace();
29 con = null;
30 }
31 return con;
32 }
33
34 public static void close(Statement sta,Connection connection) {
35 if(sta!=null) {
36 try {
37 ((Connection) sta).close();
38 }catch(SQLException e) {
39 e.printStackTrace();
40 }
41 }
42 if(connection!=null) {
43 try {
44 connection.close();
45 }catch(SQLException e) {
46 e.printStackTrace();
47 }
48 }
49 }
50
51 //关闭连接
52 public static void close(ResultSet re,Statement sta,Connection connection) {
53 if(re!=null) {
54 try {
55 re.close();
56 }catch(SQLException e) {
57 e.printStackTrace();
58 }
59 }
60 if(sta!=null) {
61 try {
62 ((Connection) sta).close();
63 }catch(SQLException e) {
64 e.printStackTrace();
65 }
66 }
67 if(connection!=null) {
68 try {
69 connection.close();
70 }catch(SQLException e) {
71 e.printStackTrace();
72 }
73 }
74 }
75
76
77 public static void main(String[] args)
78 {
79 getCon();
80 }
81 }