|
|
|
@ -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("数据库连接已关闭。");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|