解决 Derby 数据库初始化依赖问题
当引入依赖后依然报错时,通常是由于依赖配置不正确、版本冲突或环境问题导致的。下面我将提供一个完整的解决方案,包括详细的依赖配置和增强的数据库连接代码。
完整的解决方案
1. 添加正确的 Maven 依赖
在 pom.xml
中添加以下依赖:
<dependencies>
<!-- Derby 数据库驱动 -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyshared</artifactId>
<version>10.15.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.15.2.0</version>
</dependency>
<!-- Swing 相关依赖 -->
<dependency>
<groupId>com.formdev</groupId>
<artifactId>flatlaf</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
2. 增强的数据库连接工具类
package com.example.studentdb;
import java.sql.*;
import java.util.Properties;
public class DerbyDBUtil {
private static final String DB_NAME = "studentDB";
private static final String DB_DIR = System.getProperty("user.dir") + "/db";
private static final String DB_URL = "jdbc:derby:" + DB_DIR + "/" + DB_NAME + ";create=true";
private static Connection connection;
public static Connection getConnection() throws SQLException {
if (connection == null || connection.isClosed()) {
// 确保数据库目录存在
java.io.File dbDir = new java.io.File(DB_DIR);
if (!dbDir.exists()) {
dbDir.mkdirs();
}
// 设置 Derby 系统目录
System.setProperty("derby.system.home", DB_DIR);
try {
// 尝试加载驱动
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
} catch (ClassNotFoundException e) {
throw new SQLException("Derby JDBC Driver not found", e);
}
// 设置连接属性
Properties props = new Properties();
props.put("user", "admin");
props.put("password", "admin123");
props.put("create", "true");
// 创建连接
connection = DriverManager.getConnection(DB_URL, props);
}
return connection;
}
public static void initializeDatabase() throws SQLException {
try (Connection conn = getConnection();
Statement stmt = conn.createStatement()) {
// 检查表是否存在
if (!tableExists(conn, "STUDENTS")) {
// 创建学生表
String createTableSQL = "CREATE TABLE STUDENTS (" +
"ID VARCHAR(10) PRIMARY KEY, " +
"NAME VARCHAR(50) NOT NULL, " +
"GENDER VARCHAR(10) NOT NULL, " +
"AGE INTEGER NOT NULL)";
stmt.executeUpdate(createTableSQL);
// 插入示例数据
insertSampleData(conn);
}
}
}
private static boolean tableExists(Connection conn, String tableName) throws SQLException {
DatabaseMetaData dbmd = conn.getMetaData();
try (ResultSet rs = dbmd.getTables(null, null, tableName.toUpperCase(), new String[]{
"TABLE"})) {
return rs.next();
}
}
private static void insertSampleData(Connection conn) throws SQLException {
String sql = "INSERT INTO STUDENTS (ID, NAME, GENDER, AGE) VALUES (?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
insertStudent(pstmt, "S001", "张三", "男", 20);
insertStudent(pstmt, "S002", "李四", "女", 21);
insertStudent(pstmt, "S003", "王五", "男", 22);
insertStudent(pstmt, "S004", "赵六", "男", 19);
insertStudent(pstmt, "S005", "钱七", "女", 20);
}
}
private static void insertStudent(PreparedStatement pstmt, String id, String name, String gender, int age)
throws SQLException {
pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setString(3, gender);
pstmt.setInt(4, age);
pstmt.executeUpdate();
}
public static void shutdownDatabase() {
try {
if