【问题标题】:Export project with derby database in Netbeans在 Netbeans 中使用 derby 数据库导出项目
【发布时间】:2023-03-27 03:55:01
【问题描述】:

如何在 Netbeans 中导出带有 derby 数据库的 java 项目?我想在另一台计算机上导入项目后无需任何配置即可工作。

【问题讨论】:

    标签: java netbeans export javadb


    【解决方案1】:

    将 derby 最基本的安装到您的应用程序中非常轻松。这实际上只是将库/jar 放在类路径上的问题。 Netbeans 已经带有这个库,所以您不必下载它。如果由于某种原因没有,您可以从here 下载。解压缩后,只需将derby.jar(以及其他必要的)添加到类路径。

    基本上来自 Netbeans,在您的项目中,右键单击 [Library] 并选择 [Add Library]。

    然后只需选择 [Java DB] 库

    如果您下载了库,则不要选择 [添加库],而是选择 [添加 Jar] 并搜索您将其下载到的 jar。

    这些是 Netbeans 库附带的 jars

    然后你可以在你的应用程序中使用数据库。嵌入式版本与您的应用程序在同一个 JVM 上运行,因此您可能需要自己负责启动和关闭数据库。这是一个启动-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();
        }
    }
    

    当我们构建它时,Netbeans 默认构建应该将 jars 放入 dist\lib 并将这些 jars 放在 MANIFEST.MF 的类路径中。你可以从命令行运行jar来测试它

    如果您在 Netbeans 中打开文件视图,您可以看到数据的实际存储位置。


    有关德比和德比教程的更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多