51 lines
1.2 KiB
Java
51 lines
1.2 KiB
Java
package com.legacy.order.util;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
|
|
public class DbUtil {
|
|
|
|
private static final String URL = "jdbc:h2:mem:legacydb;DB_CLOSE_DELAY=-1";
|
|
private static final String USER = "sa";
|
|
private static final String PASSWORD = "password123";
|
|
|
|
private static DbUtil instance;
|
|
private static Connection conn;
|
|
|
|
private DbUtil() {
|
|
System.out.println("DbUtil初始化了");
|
|
}
|
|
|
|
public static DbUtil getInstance() {
|
|
if (instance == null) {
|
|
instance = new DbUtil();
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public Connection getConnection() {
|
|
try {
|
|
Class.forName("org.h2.Driver");
|
|
conn = DriverManager.getConnection(URL, USER, PASSWORD);
|
|
System.out.println("创建了新数据库连接");
|
|
return conn;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void closeConnection() {
|
|
try {
|
|
if (conn != null && !conn.isClosed()) {
|
|
conn.close();
|
|
}
|
|
} catch (Exception e) {
|
|
}
|
|
}
|
|
|
|
public static Connection getConn() {
|
|
return DbUtil.getInstance().getConnection();
|
|
}
|
|
}
|