Connecting to a database using an embedded driver

View: New views
4 Messages — Rating Filter:   Alert me  

Connecting to a database using an embedded driver

by ComputechSys :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm trying to create a single user app that connects to a derby database. I have no problem creating connections using the client driver but cannot create a connection using the embedded driver. I get this error

Error: Unable to add connection. Cannot establish a connection to jdbc:derby:MaxxTrax using org.apache.derby.jdbc.EmbeddedDriver (Database 'MaxxTrax' not found).

And I dont know how the database isn't found cause the client driver can find it without a problem. Do i have to physically move the database or something?

And if no one can answer that, then using the client driver, how do I connect to the server using java code, and would i have to have the server installed seperatley or will it do it automatically in the clean and build?

Re: Connecting to a database using an embedded driver

by nbdev69 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't use derby. I use MySql so I am hoping some of this info applies to derby as well.

In mysql you simply add the driver to netbeans using the Database runtime or you can add it using code. Once the driver is added you input the url and it is done. I have this problem as well if I try and use the built-in MySql driver included with netbeans. I simply add a external driver which netbeans handles adding to my class path when run. If this does not help try and be more specific about what is a "client" driver and what is an "embedded" driver I don't really know what that means.



ComputechSys wrote:
I'm trying to create a single user app that connects to a derby database. I have no problem creating connections using the client driver but cannot create a connection using the embedded driver. I get this error

Error: Unable to add connection. Cannot establish a connection to jdbc:derby:MaxxTrax using org.apache.derby.jdbc.EmbeddedDriver (Database 'MaxxTrax' not found).

And I dont know how the database isn't found cause the client driver can find it without a problem. Do i have to physically move the database or something?

And if no one can answer that, then using the client driver, how do I connect to the server using java code, and would i have to have the server installed seperatley or will it do it automatically in the clean and build?

Re: Connecting to a database using an embedded driver

by ComputechSys :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Basically I can create a program that runs fine using the client driver. I can interact with it fine when I'm building it in netbeans. But when I clean and build the project it won't work if I physically stop the server in  Netbeans. This will be a singe user app and I need a way to get the jar file to run without first going into netbeans and starting the server.


I don't use derby. I use MySql so I am hoping some of this info applies to derby as well.

In mysql you simply add the driver to netbeans using the Database runtime or you can add it using code. Once the driver is added you input the url and it is done. I have this problem as well if I try and use the built-in MySql driver included with netbeans. I simply add a external driver which netbeans handles adding to my class path when run. If this does not help try and be more specific about what is a "client" driver and what is an "embedded" driver I don't really know what that means.



ComputechSys wrote:
I'm trying to create a single user app that connects to a derby database. I have no problem creating connections using the client driver but cannot create a connection using the embedded driver. I get this error

Error: Unable to add connection. Cannot establish a connection to jdbc:derby:MaxxTrax using org.apache.derby.jdbc.EmbeddedDriver (Database 'MaxxTrax' not found).

And I dont know how the database isn't found cause the client driver can find it without a problem. Do i have to physically move the database or something?

And if no one can answer that, then using the client driver, how do I connect to the server using java code, and would i have to have the server installed seperatley or will it do it automatically in the clean and build?


Re: Connecting to a database using an embedded driver

by John H. Embretsen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>  Error: Unable to add connection. Cannot establish a connection to
>  jdbc:derby:MaxxTrax using org.apache.derby.jdbc.EmbeddedDriver (Database
>  'MaxxTrax' not found).
>
>  And I dont know how the database isn't found cause the client driver can
>  find it without a problem. Do i have to physically move the database or
>  something?

By default, Derby looks for your databases in the working directory of
the JVM running the Derby system (i.e. network server or other
embedding application). Your issue is probably that the working
directory of the server instance created by Netbeans is not the same
as the working directory of your application.

When you are using the Network Server, it looks for databases relative
to the server's working directory (unless you specify an absolute URL
or set derby.system.home). When you start the server from NetBean's
menus, the server's working directory corresponds to the "Database
location" setting (I think the default is $HOME/.netbeans-derby/). You
can figure out this for sure by finding out the location of your
database in your file system. If your database is at
/home/user/.netbeans-derby/myDatabase, then the server's working
directory is /home/user/.netbeans-derby.

When you are using the embedded driver (e.g. from a Java app), Derby
looks for database directories in the app's working directory (in
NetBeans this is probably the "dist" directory or possibly a "run"
directory within the project directory).

You may want to read this blog post for some additional details:
http://davidvancouvering.blogspot.com/2007/10/where-are-my-database-tables-in.html

See also http://wiki.apache.org/db-derby/DatabaseNamesRelativeAbsolute

Possible solutions:

a) Adjust the JDBC connection URL for the embedded driver used by your
application, e.g. by using an absolute path.

b) Set the system property derby.system.home to the "Database
location" used by the server. Do this prior to loading the embedded
driver (Class.forName...) in your Java code. You can then use the same
JDBC URLs as before.

c) Move the database directory to the working directory used by your
project, and use the same embedded URL as before. If you still want to
access the database using the client driver + server from NetBeans'
menus, you will need to adjust the client URL and/or the "Database
location" server setting to the new location.

>
>  And if no one can answer that, then using the client driver, how do I
>  connect to the server using java code, and would i have to have the server
>  installed seperatley or will it do it automatically in the clean and build?

Connecting to the server using the client driver from Java code is as
simple as connecting to the embedded database. You just have to modify
three things:

a) You need to include derbyclient.jar in your app's classpath instead
of (or in addition to) derby.jar.

b) You need to load the client driver instead of the embedded driver:

Class.forName("org.apache.derby.jdbc.ClientDriver");

(with JDK 6 / JDBC 4.0 this is not needed)

c) You need to include a host name and possibly a port number in the
connection URL, e.g.

jdbc:derby://hostname:portnumber/databasePath

instead of

jdbc:derby:databasePath

See also the tutorial at http://db.apache.org/derby/papers/DerbyTut/index.html

Hope this helps!

--
John