在Spring中使用JDBC连接到derby数据库

时间:2014-01-26 01:22:57

标签: java database derby spring-jdbc

我正在尝试连接到德比数据库。

运行应用后,我在控制台中出现以下错误:

    01:07:31.698 [main] INFO  o.s.j.d.DriverManagerDataSource - Loaded JDBC driver: org.apache.derby.jdbc.EmbeddedDriver
Creating records
01:07:31.826 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL update
01:07:31.828 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [INSERT INTO `students` (name,age) VALUES (?,?)]
01:07:31.845 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
01:07:31.845 [main] DEBUG o.s.j.d.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:derby:c:\temp\database\test01;create=true]
Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Failed to start database 'c:\temp\database\test01' with class loader sun.misc.Launcher$AppClassLoader@6d9bf996, see the next exception for details.
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:628)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:907)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:968)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:978)
    at Student.dao.StudentImplementationOfDao.insert(StudentImplementationOfDao.java:22)
    at main.Application.main(Application.java:31)
Caused by: java.sql.SQLException: Failed to start database 'c:\temp\database\test01' with class loader sun.misc.Launcher$AppClassLoader@6d9bf996, see the next exception for details.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.seeNextException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.bootDatabase(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(Unknown Source)
    at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Unknown Source)
    at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
    at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    ... 6 more
Caused by: java.sql.SQLException: Failed to start database 'c:\temp\database\test01' with class loader sun.misc.Launcher$AppClassLoader@6d9bf996, see the next exception for details.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 24 more
Caused by: java.sql.SQLException: Another instance of Derby may have already booted the database C:\temp\database\test01.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    ... 21 more
Caused by: ERROR XSDB6: Another instance of Derby may have already booted the database C:\temp\database\test01.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.privGetJBMSLockOnDB(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.getJBMSLockOnDB(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.store.raw.RawStore.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.store.access.RAMAccessManager.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.db.BasicDatabase.bootStore(Unknown Source)
    at org.apache.derby.impl.db.BasicDatabase.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.bootService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startProviderService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.findProviderAndStartService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startPersistentService(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.startPersistentService(Unknown Source)
    ... 21 more

我为连接数据库而构建的代码是:

        DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
    ds.setUrl("jdbc:derby:c:\\temp\\database\\test01;create=true");
    ds.setUsername("");
    ds.setPassword("");
    siod.setDataSource(ds);

从我在上面显示的错误中可以理解的是与试图插入的PreparedStatement有关。

sql语句是:

"INSERT INTO `students` (name,age) VALUES (?,?)

我将这个sql解析为实例化的JdbcTemplate,它具有上面提到的dataSource。 这是确切的代码:

public void insert(Student student)
{
//ds is simply the dataSource that gets created in the code example above
JdbcTemplate create = new JdbcTemplate(ds);
create.update(sql,new Object[]{student.name,student.age});

}

我确定这个问题有一个简单的答案,但我只是不明白究竟是什么导致了这个错误。是声明吗?是否导致连接失败?如何解决使我的代码失败的原因?

谢谢

1 个答案:

答案 0 :(得分:4)

  

Derby的另一个实例可能已经启动了数据库   C:\ TEMP \数据库\ TEST01

对于嵌入式数据库,只有嵌入的应用程序才能访问它。检查以确保先前的应用程序已完全停止。最简单的解决方案是重新启动操作系统,但如果您熟悉查杀方法,则可以查找所有Java进程并将其终止。

我有时看到的另一件事是有人将文件从Derby数据库复制到另一个位置,而正在使用该Derby数据库。这使数据库的副本处于锁定状态。这可能不是你的问题。