Export project with derby database in Netbeans

前端 未结 1 1919
轻奢々
轻奢々 2021-01-16 16:37

How can I export a java project with derby database in Netbeans? I would like to after import project on another computer it works without any configuration.

相关标签:
1条回答
  • 2021-01-16 17:24

    The most basic installation of derby into your application is pretty effortless. It's really just a matter of put the library/jar on the class path. Netbeans already comes with this library, so you don't have to download it. If for any reason, it doesn't, you can download it from here. After you unzip it, just add the derby.jar (and others if necessary) to the classpath.

    Basically from Netbeans, from your project, right click on the [Library] and select [Add Library].

    enter image description here

    Then just select the [Java DB] library

    enter image description here

    If you downloaded the library, then instead of [Add Library], select [Add Jar] and search for the jar where you downloaded it to.

    These are the jars that come with the Netbeans library

    enter image description here

    Then you can use the database in your application. The embedded version runs on the same JVM as your application, so you may want to take care of starting and shutting down the database yourself. Here's a sample application that starts-createsdb-inserts-selects-shutsdown.

    import java.sql.*;
    
    public class DerbyProject {
        public static void main(String[] args) throws Exception {
            /* ------- Start DB ----------- */
            final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
            Class.forName(driver).newInstance();
    
            final String protocol = "jdbc:derby:";
            final String dbName = "derbyDB";
            Connection connection = DriverManager.getConnection(
                    protocol + dbName + ";create=true");
            System.out.println("=====    Started/Connected DB    =====");
    
            /*
             *    Drop table for testing. If we don't drop, running the
             *    same program will fail, if we start our application over
             *    as the new table has been persisted
             */
            final String dropSql = "drop table users";
            Statement statement = connection.createStatement();
            try {
                statement.execute(dropSql);
                System.out.println("=====    Dropped Table 'users'   =====");
            } catch (SQLException e) {
                if (!e.getSQLState().equals("42Y55")) {
                    throw e;
                }
            }
    
            /* ----- Creeate 'users' table  ----- */
            final String createSql = "create table users ( id int, name varchar(32) )";
            statement.execute(createSql);
            System.out.println("=====    Created Table 'users'   =====");
    
            /* ----- Insert 'peeskillet' into 'users' ----*/
            final String insertSql = "insert into users values ( 1 , 'peeskillet' )";
            statement.execute(insertSql);
            System.out.println("=====    inserted 'peeskillet into 'users'   =====");
    
            /* ----- Select from 'users' table  ----- */
            final String selectSql = "select name from users where id = 1";
            ResultSet rs = statement.executeQuery(selectSql);
            if (rs.next()) {
                System.out.println("=====    Selected from 'users' with id 1 \n"
                        + "\t\t\t result: " + rs.getString("name") + "  =====");
            }
    
            /*  ------ Shut Down DB ------- */
            try {
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (SQLException se) {
                if (((se.getErrorCode() == 50000)
                        && ("XJ015".equals(se.getSQLState())))) {
                    System.out.println("Derby shut down normally");
                } else {
                    System.err.println("Derby did not shut down normally");
                    throw se;
                }
            }
    
            statement.close();
            rs.close();
            connection.close();
        }
    }
    

    When we build it, the Netbeans default build should put the jars into the dist\lib and put those jars on the classpath in the MANIFEST.MF. You can run the jar from the command line to test it

    enter image description here

    If you open up the files view in Netbeans, you can see where the data is actually being stored.

    enter image description here


    For more information on Derby and Derby tutorials:

    • Derby Home Page
    • Derby Tutorial
    • Embedded Derby
    0 讨论(0)
提交回复
热议问题