简体   繁体   中英

querying embedded database in netbeans using derby

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.

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

Thus, this database must be visible in Netbeans Services.

在此输入图像描述

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.

在此输入图像描述

 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, eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM