简体   繁体   English

Netbeans:如何以编程方式启动Derby数据库

[英]Netbeans: How to start a Derby Database programmatically

I have developed a Java project in Netbeans. 我在Netbeans中开发了一个Java项目。 Now I have the problem that the following code is only executeable if i start the specified database first: 现在,我的问题是,只有首先启动指定的数据库,以下代码才可执行:

private boolean DriverIsLoaded()
{
    try
    {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        return true;
    }
    catch(ClassNotFoundException e)
    {
        e.printStackTrace();
        return false;
    }
}
private boolean openConnectionAndCheck()
{
    try
    {
         connection = DriverManager.getConnection("jdbc:derby://localhost:1527/myDatabase;create=true", "Administrator", "Administrator");
         return true;
    }
    catch(SQLException e)
    {
        e.printStackTrace();
        return false;
    }
}

The I have created the database inside of Netbeans. 我已经在Netbeans内部创建了数据库。 My question is, how can I start the jdbc Database programatically? 我的问题是,如何以编程方式启动jdbc数据库?

The whole point of the Derby client-server configuration (ie, using the Derby ClientDriver and specifying a JDBC Connection URL of the form jdbc:derby://host:port/path/to/database ) is that the client and server are operated independently, typically on different computers, and typically there are multiple clients, but only a single server. Derby客户端-服务器配置(即,使用Derby ClientDriver并指定jdbc:derby://host:port/path/to/database形式的JDBC连接URL)的全部要点是,客户端和服务器均已操作独立地,通常在不同的计算机上,并且通常有多个客户端,但是只有一个服务器。

That is, the notion that the client will "start the jdbc Database programmatically" is not the expected behavior for this sort of configuration; 也就是说,客户机将“以编程方式启动jdbc数据库”的想法不是这种配置的预期行为。 the client and server aren't even on the same computer! 客户端和服务器甚至不在同一台计算机上!

Perhaps what you want is the Derby "embedded" configuration. 也许您想要的是Derby“嵌入式”配置。

Read more about the types of Derby deployment configurations here: http://db.apache.org/derby/docs/10.14/getstart/cgstutorialintro.html 在此处阅读有关Derby部署配置类型的更多信息: http : //db.apache.org/derby/docs/10.14/getstart/cgstutorialintro.html

And perhaps what you are really looking for is information about how to deploy, configure, and operate the Derby Network Server, for which start here: http://db.apache.org/derby/docs/10.14/adminguide/index.html 也许您真正想要的是有关如何部署,配置和操作Derby Network Server的信息,其信息从此处开始: http : //db.apache.org/derby/docs/10.14/adminguide/index.html

You are looking for programming Derby in embedded mode . 您正在寻找以嵌入式模式编写Derby的程序。 Here is a code snippet: 这是一个代码片段:

package com.zetcode;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CreateCars {

    public static void main(String[] args) {

        Connection con = null;
        Statement st = null;

        String url = "jdbc:derby:testdb;user=USER12";

        try {

            System.setProperty("derby.system.home", "/home/janbodnar/.derby");

            con = DriverManager.getConnection(url);
            st = con.createStatement();
            st.executeUpdate("CREATE TABLE CARS(ID INT PRIMARY KEY,"
                    + "NAME VARCHAR(30), PRICE INT)");
            st.executeUpdate("INSERT INTO CARS VALUES(1, 'Audi', 52642)");
            st.executeUpdate("INSERT INTO CARS VALUES(2, 'Mercedes', 57127)");
            st.executeUpdate("INSERT INTO CARS VALUES(3, 'Skoda', 9000)");
            st.executeUpdate("INSERT INTO CARS VALUES(4, 'Volvo', 29000)");
            st.executeUpdate("INSERT INTO CARS VALUES(5, 'Bentley', 350000)");
            st.executeUpdate("INSERT INTO CARS VALUES(6, 'Citroen', 21000)");
            st.executeUpdate("INSERT INTO CARS VALUES(7, 'Hummer', 41400)");
            st.executeUpdate("INSERT INTO CARS VALUES(8, 'Volkswagen', 21600)");
            DriverManager.getConnection("jdbc:derby:;shutdown=true");

        } catch (SQLException ex) {

            Logger lgr = Logger.getLogger(CreateCars.class.getName());

            if (((ex.getErrorCode() == 50000)
                    && ("XJ015".equals(ex.getSQLState())))) {

                lgr.log(Level.INFO, "Derby shut down normally", ex);

            } else {

                lgr.log(Level.SEVERE, ex.getMessage(), ex);
            }

        } finally {

            try {

                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(CreateCars.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
}

For a full tutorial, have a look at my Programming Derby with JDBC tutorial. 有关完整的教程,请查看我的《 带JDBC编程Derby》

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

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