首页 > 数据库 >Util类 为了代码复用将一些连接数据库的代码

Util类 为了代码复用将一些连接数据库的代码

时间:2022-10-26 22:33:50浏览次数:48  
标签:代码 复用 Util printStackTrace state static close null conn

import java.sql.*;//导入包

public class Util1 {

//基本配置

static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/chaojikechengbiao?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//改自己的数据库(chaojikechengbiao(我的数据库))
static final String USER = "root";
static final String PASS = "123456";
public static Connection getConn () {
Connection conn = null;
try {
Class.forName(JDBC_DRIVER);
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//statement
public static void close (Statement state, Connection conn) {

if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

 



}

标签:代码,复用,Util,printStackTrace,state,static,close,null,conn
From: https://www.cnblogs.com/JIANGzihao0222/p/16830342.html

相关文章