📄 dboperator.java
字号:
package com.wczy.chatroom.server.database;
import java.sql.*;
import java.util.ResourceBundle;
public class DBOperator {
private static String USER,PASS,URL;
static{
ResourceBundle bundle=ResourceBundle.getBundle("db");//取得properties文件访问方式
URL=bundle.getString("URL");//从配置文件里读取内容
USER=bundle.getString("USER");
PASS=bundle.getString("PASS");
try {
Class.forName(bundle.getString("DRIVER"));//注册驱动
} catch (ClassNotFoundException e) {
throw new RuntimeException("驱动无法取得", e);
}
}
public static Connection getConnection() {
try {
Connection conn = DriverManager
.getConnection(URL,USER,PASS);// 取得连接
return conn;
} catch (SQLException e) {
throw new RuntimeException("数据库连接失败", e);
}
}
public static void close(Connection conn){
try{
if(conn != null) conn.close();//关闭连接
}catch(SQLException e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -