简体   繁体   English

在Netbeans中使用derby数据库导出项目

[英]Export project with derby database in Netbeans

How can I export a java project with derby database in Netbeans? 如何在Netbeans中使用derby数据库导出Java项目? I would like to after import project on another computer it works without any configuration. 我想在另一台计算机上导入项目后,无需进行任何配置。

The most basic installation of derby into your application is pretty effortless. derby最基本的安装到您的应用程序是很容易的。 It's really just a matter of put the library/jar on the class path. 实际上,只需将库/ jar放在类路径上即可。 Netbeans already comes with this library, so you don't have to download it. Netbeans已经随该库一起提供,因此您不必下载它。 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. 解压缩后,只需将derby.jar (以及其他必要的文件)添加到类路径中。

Basically from Netbeans, from your project, right click on the [Library] and select [Add Library]. 基本上是从Netbeans,在您的项目中,右键单击[Library],然后选择[Add Library]。

在此处输入图片说明

Then just select the [Java DB] library 然后只需选择[Java DB]库

在此处输入图片说明

If you downloaded the library, then instead of [Add Library], select [Add Jar] and search for the jar where you downloaded it to. 如果下载了库,则选择[添加Jar]而不是[添加库],然后搜索将其下载到的jar。

These are the jars that come with the Netbeans library 这些是Netbeans库随附的jar

在此处输入图片说明

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. 嵌入式版本与您的应用程序在同一JVM上运行,因此您可能需要自己启动和关闭数据库。 Here's a sample application that starts-createsdb-inserts-selects-shutsdown. 这是一个示例应用程序,它开始-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 . 在构建它时,Netbeans的默认构建应将jar放入dist\\lib并将这些jar放在MANIFEST.MF的类路径上。 You can run the jar from the command line to test it 您可以从命令行运行jar进行测试

在此处输入图片说明

If you open up the files view in Netbeans, you can see where the data is actually being stored. 如果在Netbeans中打开文件视图,则可以看到实际存储数据的位置。

在此处输入图片说明


For more information on Derby and Derby tutorials: 有关Derby和Derby教程的更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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