Search code examples
javanetbeansderby

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.


Solution

  • 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.