如何从我的java应用程序中启动derby服务器

时间:2014-03-25 11:51:35

标签: java sql eclipse jpa

我一直在使用最近的EclipseLink,在创建我的java应用程序后,没有我从netbeans手动启动derby服务器(服务>数据库> JavaDB>启动服务器)我的应用程序将无法连接到数据库,我不知道出了什么问题或怎么解决这个问题。我使用了来自新线程的代码,这就是我得到的。

public static class DBServerStarter extends Thread {
  NetworkServerControl serverControl;

  public DBServerStarter(){
    try {
      serverControl = new NetworkServerControl();
    } catch (Exception ex) {
      Logger.getLogger(ContactManager.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  @Override
  public void run(){
    try {
      serverControl.start(new PrintWriter(System.out));
    } catch (Exception ex) {
      Logger.getLogger(ContactManager.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

我的日志看起来像

Tue Mar 25 12:50:33 WAT 2014 : Apache Derby Network Server - 10.9.1.0 - (1344872) started and ready to accept connections on port 1527
[EL Info]: 2014-03-25 12:50:36.098--ServerSession(21798543)--EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5
[EL Severe]: ejb: 2014-03-25 12:50:36.583--ServerSession(21798543)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLNonTransientConnectionException: The connection was refused because the database Contact was not found.
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.prodigy4440.contactmanager.ContactManager.main(ContactManager.java:28)
Error Code: 40000
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLNonTransientConnectionException: The connection was refused because the database Contact was not found.
Error Code: 40000
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:766)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:182)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getDatabaseSession(EntityManagerFactoryImpl.java:527)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:140)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at com.prodigy4440.contactmanager.MainJFrame.<clinit>(MainJFrame.java:38)
    ... 1 more
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLNonTransientConnectionException: The connection was refused because the database Contact was not found.
Error Code: 40000
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:326)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:138)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:204)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:741)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
    ... 9 more
Caused by: java.sql.SQLNonTransientConnectionException: The connection was refused because the database Contact was not found.
    at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:187)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:98)
    ... 14 more
Caused by: org.apache.derby.client.am.DisconnectException: The connection was refused because the database Contact was not found.
    at org.apache.derby.client.net.NetConnectionReply.parseRDBNFNRM(Unknown Source)
    at org.apache.derby.client.net.NetConnectionReply.parseAccessRdbError(Unknown Source)
    at org.apache.derby.client.net.NetConnectionReply.parseACCRDBreply(Unknown Source)
    at org.apache.derby.client.net.NetConnectionReply.readAccessDatabase(Unknown Source)
    at org.apache.derby.client.net.NetConnection.readSecurityCheckAndAccessRdb(Unknown Source)
    at org.apache.derby.client.net.NetConnection.flowSecurityCheckAndAccessRdb(Unknown Source)
    at org.apache.derby.client.net.NetConnection.flowUSRIDPWDconnect(Unknown Source)
    at org.apache.derby.client.net.NetConnection.flowConnect(Unknown Source)
    at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
    at org.apache.derby.client.net.NetConnection40.<init>(Unknown Source)
    at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40.newNetConnection(Unknown Source)
    ... 18 more
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

1 个答案:

答案 0 :(得分:1)

答案在日志消息中:

  

java.sql.SQLNonTransientConnectionException:连接是   拒绝,因为找不到数据库联系人。

因此,您需要手动创建Contact数据库或将create=true添加到persistence.xml中的javax.persistence.jdbc.url属性:

<property name="javax.persistence.jdbc.url" value="jdbc:derby:/path/to/db/Contact;create=true" />

如果数据库不存在,它允许derby创建数据库。