How to connect to database without opening NetBeans?

1.4k views Asked by At

I have created a Java application that uses a database, and I now want to build it into a jar file and make it so that the database works without NetBeans. I thought NetBeans would automatically include the database, but it doesn't.

The files that NetBeans include in the 'dist' folder when I build it look like this:

  • dist
    • lib
      • derby.jar (Executable Jar File)
      • derbyclient.jar (Executable Jar File)
    • README
    • WordFinder.jar (Executable Jar File)

Everything except the database-related code works when I launch the WordFinder.jar file after closing NetBeans.

I noticed that the database is using 'org.apache.derby.jdbc.ClientDriver', and I'm wondering, does that need to be 'org.apache.derby.jdbc.EmbeddedDriver' in order for it to include the database?

Are there any files that I need to include? I'm new to Java databases, so try to keep the instructions simple.

2

There are 2 answers

3
Yumarx Polanco On

You need to start the database server in you computer, for doing that you need to go to your derby path (C:\Program Files\Java\jdk1.8.0_45\db\bin) and run the startNetworkServer.bat file

1
joppiealiw On

I may be a year late but seeing the comments, I'm assuming the issue hasn't been resolved yet. I had the same problem and would like to show how I worked around it. Note I'm not using an embedded derby database.

1) I changed my connection string. Instead of the usual "jdbc:derby://localhost:1527/myDB;create=true;...." , I put the exact location of the database which can be found usually under C:\Users\myPC\.netbeans-derby\myDB

2) Clean and build the project which should give you the "dist" folder which would include the jar file of the project together with the "lib" folder.

3) Start the StartNetworkServer.bat which is located in the bin folder under derby folder. A cmd would show that will say something "Apache Derby Network Server blah blah blah started and ready to accept connections on port 1527"

4) After doing those, run the jar file and it should run even with the netbeans IDE closed.

That's about it. That's how I got mine to work without opening Netbeans IDE. Hope it helps.