3

I have created an embedded database using netbeans and added data to it. so now i want to query the database, the code runs smoothly but doesn't display data. Here is my code:

import java.sql.*;
public class EmbeddedDB 
{  

public static void main(String[] args)
{
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    try
    {
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        con = DriverManager.getConnection("jdbc:derby:CustDB;create=true", "app", "app");
        System.out.println("connected");
        st = con.createStatement();
        System.out.println("statement created");

        rs = st.executeQuery("select * from APP.TABLEX");
        System.out.println("retrieving ...");
        System.out.println(rs.getString(1));

    }
    catch(ClassNotFoundException | SQLException c)
    {
    }
}
}

so what could be the problem? the database was created in an embedded mode.

moskito-x
  • 11,832
  • 5
  • 47
  • 60
Green Onyeji
  • 259
  • 5
  • 18
  • Are you sure that the CustDB database you added data to is the same one that's on your classpath when this main method runs? – Tap May 26 '13 at 13:40
  • @Tap, yes. I had created a CustDB before writing the code to query it. – Green Onyeji May 26 '13 at 14:02
  • Still, I would search the file system for CustDB. If it turns up twice then your program created a new empty database. – Tap May 26 '13 at 15:00
  • "Are you sure that the CustDB database you added data to is the same one that's on your classpath when this main method runs? – Tap 4 hours ago". Can you elaborate more on this your question? – Green Onyeji May 26 '13 at 17:55
  • The file System for CustDB? – Green Onyeji May 26 '13 at 17:55
  • Since you said "create=true", Derby will create a new empty database if it doesn't find an existing one. Since your database path is simply "CustDB", Derby will look for a folder named CustDB in the current working directory of your program when you run it. So if the current working directory of your program changes from run to run, your program will be using a different database each time. In that case, your program should get a "no such table app.tablex" from the executeQuery call for "select * from app.tablex". – Bryan Pendleton May 26 '13 at 18:56

2 Answers2

4

You told us I have created an embedded database ... and added data to it.

Thus, this database must be visible in Netbeans Services.

enter image description here

NO ;create=true !
You should connect with the same URL you see in the properties. Nothing more.
Expand Database URL or look at the bottom.

enter image description here

 con = DriverManager.getConnection("jdbc:derby:C:/Dokumente und Einstellungen/Administrator/.netbeans-derby/sample","app","app");

In the embedded mode Derby runs within the JVM (Java Virtual Machine) of the application. In this mode only the application can access the database, e.g. another user / application will not be able to access the database.

Only one Application can acces the Database.
So disconnect in Netbeans Services, the Database you want to connect to in your Application.

moskito-x
  • 11,832
  • 5
  • 47
  • 60
2

I Would like to add an important clarification to the previous answer, because I found myself a bit lost during hours trying to make this thing functional. My point is for you to understand how Netbeans services tab works with embedded derby databases. When using:

 con = DriverManager.getConnection("jdbc:derby:CustDB;create=true", "app", "app");

It creates the database in the same directory as your netbeans project. You can change this behavior by adding a directory path:

jdbc:derby:directory_path/CustDB;create=true 

So when you execute your program it will create the database in the specified path.

Now, when you are creating a connection to an embedded database in the service tab, you are connecting to an existing database or creating a new one. I'm going to use Green's example:

Once you execute for the first time the code from the first post with

jdbc:derby:CustDB;create=true

You create a database in the project's directory path. To be able to use an edit this database from the service tab, you must create a new connection to an embedded database. You must use the name CustDB, same user app and same password app.

The very important thing here is to use the directory path of the project in the URL, so in the window when creating the connection in the URL field you must use:

jdbc:derby:directory_path_of_the_project/CustDB;

Now if yo press test connection button, everything should be alright.

If you use here create=true without using the directory path of the project, you will create another database with the same name, same user and password but in a different location. Later you will find yourself making changes to the database in your code and not seeing them in the database in the service tab.

BlueRat
  • 21
  • 1