Derby on Intellij

2019-07-23 05:31发布

问题:

I want to setup a derby database on IntelliJ IDEA. I have searched a lot for this issue but I could not find a useful answer. Every time I try to connect to the derby database, I get the following error.

Can anyone help me fix this issue?

java.sql.SQLException: Failed to create database 'C:/Users/Omar/IdeaProjects/untitled3', see the next exception for details.
  at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
  at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
  at org.apache.derby.impl.jdbc.Util.seeNextException(Unknown Source)
  at org.apache.derby.impl.jdbc.EmbedConnection.createDatabase(Unknown Source)
  at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
  at org.apache.derby.jdbc.InternalDriver.getNewEmbedConnection(Unknown Source)
  at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
  at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
  at org.apache.derby.jdbc.EmbeddedDriver.connect(Unknown Source)
  at com.intellij.database.remote.jdbc.impl.RemoteDriverImpl.connect(RemoteDriverImpl.java:27)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
  at sun.rmi.transport.Transport$1.run(Transport.java:178)
  at sun.rmi.transport.Transport$1.run(Transport.java:175)
  at java.security.AccessController.doPrivileged(Native Method)
  at sun.rmi.transport.Transport.serviceCall(Transport.java:174)
  at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:557)
  at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:812)
  at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:671)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
  at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
  at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
  at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:162)
  at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
  at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
  at com.sun.proxy.$Proxy106.connect(Unknown Source)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at com.intellij.execution.rmi.RemoteUtil.invokeRemote(RemoteUtil.java:124)
  at com.intellij.execution.rmi.RemoteUtil.access$100(RemoteUtil.java:36)
  at com.intellij.execution.rmi.RemoteUtil$RemoteInvocationHandler.invoke(RemoteUtil.java:227)
  at com.sun.proxy.$Proxy108.connect(Unknown Source)
  at com.intellij.database.dataSource.LocalDataSource.getConnection(LocalDataSource.java:240)
  at com.intellij.database.dataSource.LocalDataSource.getConnection(LocalDataSource.java:208)
  at com.intellij.database.view.DatabaseRefactoringHandler$3.run(DatabaseRefactoringHandler.java:95)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl$TaskRunnable.run(ProgressManagerImpl.java:621)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl$3.run(ProgressManagerImpl.java:194)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl.a(ProgressManagerImpl.java:281)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:233)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:181)
  at com.intellij.openapi.progress.impl.ProgressManagerImpl$9.run(ProgressManagerImpl.java:530)
  at com.intellij.openapi.application.impl.ApplicationImpl$8.run(ApplicationImpl.java:406)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
  at org.jetbrains.ide.PooledThreadExecutor$1$1.run(PooledThreadExecutor.java:56)
Caused by: java.lang.RuntimeException: ERROR XJ041: Failed to create database 'C:/Users/Omar/IdeaProjects/untitled3', see the next exception for details.
  at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
  at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA(Unknown Source)
  at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
  at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
  at org.apache.derby.impl.jdbc.Util.seeNextException(Unknown Source)
  at org.apache.derby.impl.jdbc.EmbedConnection.createDatabase(Unknown Source)
  at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
  at org.apache.derby.jdbc.InternalDriver.getNewEmbedConnection(Unknown Source)
  at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
  at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
  at org.apache.derby.jdbc.EmbeddedDriver.connect(Unknown Source)
  at com.intellij.database.remote.jdbc.impl.RemoteDriverImpl.connect(RemoteDriverImpl.java:27)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
  at sun.rmi.transport.Transport$1.run(Transport.java:178)
  at sun.rmi.transport.Transport$1.run(Transport.java:175)
  at java.security.AccessController.doPrivileged(Native Method)
  at sun.rmi.transport.Transport.serviceCall(Transport.java:174)
  at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:557)
  at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:812)
  at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:671)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: ERROR XBM0J: Directory C:\Users\Omar\IdeaProjects\untitled3 already exists.
  at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
  at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
  at org.apache.derby.impl.services.monitor.StorageFactoryService$10.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at org.apache.derby.impl.services.monitor.StorageFactoryService.createServiceRoot(Unknown Source)
  at org.apache.derby.impl.services.monitor.BaseMonitor.bootService(Unknown Source)
  at org.apache.derby.impl.services.monitor.BaseMonitor.createPersistentService(Unknown Source)
  at org.apache.derby.impl.services.monitor.FileMonitor.createPersistentService(Unknown Source)
  at org.apache.derby.iapi.services.monitor.Monitor.createPersistentService(Unknown Source)
  ... 22 more

回答1:

I will assume that you want to create a IntelliJ Data Source.

These are the steps:

  1. Go to your Apache Derby installation bin directory (e.g. cd c:\derby\bin).
  2. Start Apache Derby (e.g. startNetworkServer)
  3. Open IntelliJ Idea Project
  4. Go to Database Tab
  5. Click on the + icon to a new data source.
  6. Choose Derby Remote
  7. Fill in the form. This is an example of how I did it:

    • Host: localhost
    • Port: 1527
    • Database: demo;create=true
    • User: admin
    • Password: admin

The URL looks as follows in my case:

jdbc:derby://localhost:1527/demo;create=true

Notice the use of the "create=true" parameter. This is important because otherwise Derby will not automatically create your database and that's probably what's causing your problem.

Hit the Apply button and after that the Test Connection button and that's it. Derby will create you database and connect to it.

The database will be placed in the DERBY_HOME/bin directory since this is the place where we started the Derby server.