简体   繁体   中英

how can i use derby database with the build .jar file?

i'm using java with the derby database.

When i start the connection of the "jdbc:derby"-database in netbeans and run my code,the program shows up and everything works fine.

By opening the built ".jar"-file, the program just shows up and close immediately right after that.

So how do i have to configurate netbeans that the build .jar file work with the derby database?

(i thought derby was file based, so that the build process must generate something like a database file)

If your program crashes during the execution of your jar file, there is probably an exception. If it works in Netbeans, the exception is probably caused by the path to your derby.jar.

The first thing I can think of is that you have incluced the derby.jar in your application using a relative path and you have not moved the derby.jar inside your project library.

Try to do the following:

1: copy the derby.jar to a directory (for example /lib) in your project folder

2: include the jar using relative path from your /lib folder

EDIT:

Here is a sample code of how to create your database when it is newly created: Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

        Connection con = DriverManager.getConnection("jdbc:derby:C:\\Users\\TheReaver\\MyDB;create=true;user=thereaver;password=12345");
        PreparedStatement st = con.prepareStatement("SELECT ID, NAME FROM APP.TABLE1");
        try
        {
            ResultSet res = st.executeQuery();
        }
        catch(SQLException e) {
            //if table not found, then create all tables
            st.executeQuery("CREATE TABLE TABLE1(ID int, NAME varchar(20)))");
            //execute all statements that create your database
            //or execute an sql file that stores all your queries
        }

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