增加数据库备注

pull/52/head
沈国良 10 months ago
parent 849c0cd35a
commit c554d51aa3

@ -8,81 +8,159 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
// Properties对象用于加载配置文件
private static Properties properties;
private static String url;
private static String user;
private static String password;
// 静态代码块用于加载配置文件和数据库驱动
static {
// 获取配置文件输入流
InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
// 初始化Properties对象
properties = new Properties();
try {
// 加载配置文件
properties.load(inputStream);
} catch (IOException e) {
// 如果加载配置文件失败,打印错误信息
e.printStackTrace();
}finally {
if(inputStream!=null){
} finally {
// 关闭输入流
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// 关闭流失败,打印错误信息
e.printStackTrace();
}
}
}
//注册驱动
// 注册JDBC驱动
try {
//类加载会触发static里面代码
// 加载JDBC驱动类
Class.forName(properties.getProperty("driver"));
url= properties.getProperty("url");
user= properties.getProperty("user");
password= properties.getProperty("password");
// 获取数据库连接信息
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
} catch (ClassNotFoundException e) {
// 如果驱动类加载失败,打印错误信息
e.printStackTrace();
}
}
/**
*
* @return
*
*
* @return
*/
public static Connection getConn() {
try {
// 通过DriverManager获取数据库连接
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// 连接失败时,打印错误信息
e.printStackTrace();
return null;
}
}
/**
*
* @param resultSet
* @param statement
* @param connection
*
*
* @param resultSet
* @param statement SQL
* @param connection
*/
public static void close(ResultSet resultSet,Statement statement,Connection connection) {
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
try {
if(resultSet!=null) {
resultSet.close();
}
if(statement!=null) {
statement.close();
}
if(connection!=null) {
connection.close();
}
// 释放结果集资源
if (resultSet != null) {
resultSet.close();
}
// 释放Statement资源
if (statement != null) {
statement.close();
}
// 释放Connection资源
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// 如果关闭资源时发生错误,打印错误信息
e.printStackTrace();
}
}
/**
*
*/
private static void printDbConfig() {
System.out.println("数据库连接信息:");
System.out.println("JDBC驱动" + properties.getProperty("driver"));
System.out.println("数据库URL" + properties.getProperty("url"));
System.out.println("数据库用户名:" + properties.getProperty("user"));
System.out.println("数据库密码:" + properties.getProperty("password"));
}
/**
*
*
* @return
*/
private static String getDbConnectionString() {
return "jdbc connection info: " + url + " as user " + user;
}
/**
*
*
* @return true if connection is successful, otherwise false
*/
public static boolean testConnection() {
Connection connection = null;
try {
connection = getConn();
if (connection != null) {
System.out.println("数据库连接成功!");
return true;
} else {
System.out.println("数据库连接失败!");
return false;
}
} catch (Exception e) {
System.out.println("测试数据库连接时发生异常:" + e.getMessage());
return false;
} finally {
// 关闭连接
close(null, null, connection);
}
}
/**
*
*/
private static void logConnectionStatus() {
System.out.println("正在尝试连接数据库...");
}
/**
*
*/
private static void logCloseConnection() {
System.out.println("数据库连接已关闭。");
}
}

Loading…
Cancel
Save