增加数据库备注

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

@ -11,78 +11,156 @@ import java.util.Properties;
public class JDBCUtil { public class JDBCUtil {
// Properties对象用于加载配置文件
private static Properties properties; private static Properties properties;
private static String url; private static String url;
private static String user; private static String user;
private static String password; private static String password;
// 静态代码块用于加载配置文件和数据库驱动
static { static {
// 获取配置文件输入流
InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
// 初始化Properties对象
properties = new Properties(); properties = new Properties();
try { try {
// 加载配置文件
properties.load(inputStream); properties.load(inputStream);
} catch (IOException e) { } catch (IOException e) {
// 如果加载配置文件失败,打印错误信息
e.printStackTrace(); e.printStackTrace();
}finally { } finally {
if(inputStream!=null){ // 关闭输入流
if (inputStream != null) {
try { try {
inputStream.close(); inputStream.close();
} catch (IOException e) { } catch (IOException e) {
// 关闭流失败,打印错误信息
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
//注册驱动 // 注册JDBC驱动
try { try {
//类加载会触发static里面代码 // 加载JDBC驱动类
Class.forName(properties.getProperty("driver")); Class.forName(properties.getProperty("driver"));
url= properties.getProperty("url"); // 获取数据库连接信息
user= properties.getProperty("user"); url = properties.getProperty("url");
password= properties.getProperty("password"); user = properties.getProperty("user");
password = properties.getProperty("password");
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
// 如果驱动类加载失败,打印错误信息
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* *
* @return *
* @return
*/ */
public static Connection getConn() { public static Connection getConn() {
try { try {
// 通过DriverManager获取数据库连接
return DriverManager.getConnection(url, user, password); return DriverManager.getConnection(url, user, password);
} catch (SQLException e) { } catch (SQLException e) {
// 连接失败时,打印错误信息
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
/** /**
* *
* @param resultSet *
* @param statement * @param resultSet
* @param connection * @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 { try {
if(resultSet!=null) { // 释放结果集资源
resultSet.close(); if (resultSet != null) {
} resultSet.close();
if(statement!=null) { }
statement.close();
} // 释放Statement资源
if(connection!=null) { if (statement != null) {
connection.close(); statement.close();
} }
// 释放Connection资源
if (connection != null) {
connection.close();
}
} catch (SQLException e) { } catch (SQLException e) {
// 如果关闭资源时发生错误,打印错误信息
e.printStackTrace(); 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