开发者

How do I integrate a JavaDB database into my main java package

I am working on a desktop application which uses JavaDB. I am using NetBeans 6.8 and JDK 6 Update 20

I created the database I need and connected to it through my application using ClientDriver:

    String driver = "org.apache.derby.jdbc.ClientDriver";
    String connectionURL = "jdbc:derby://localhost:1527/myDB;create=true;user=user;password=pass";

    try {
        Class.forName(driver开发者_开发百科);
    } catch (java.lang.ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        schedoDBConnection = DriverManager.getConnection(connectionURL);
    } catch (Exception e) {
        e.printStackTrace();
    }

This works fine. But in that case the service of the database comes from NetBeans. If I move my application to another PC I won't be able to access my database. How can I integrate my JavaDB into my application?


If I move my application to another PC I won't be able to access my database. How can I integrate my JavaDB into my application?

NetBeans starts Derby in Network Server mode and Derby is running in another JVM. If you want to embed your database inside an application, you'll need to start Derby in embedded mode from within your application. To do so, use the EmbeddedDriver provided by derby.jar.

/*
    If you are running on JDK 6 or higher, you do not
    need to invoke Class.forName(). In that environment, the
    EmbeddedDriver loads automatically.
*/
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = DriverManager.getConnection("jdbc:derby:sample");

By default, the database will be created/loaded from the working directory. If you want more control, the recommended way is to set the derby.system.home system property. Have a look at Using Java DB in Desktop Applications.

See also

  • Apache Derby: Documentation


The database isn't coming from Netbeans; it's built into the JDK itself.

Clients need to have the JDBC driver JAR available to them. If you're running the database as as server, your users will have to start the server. Maybe that's the piece that Netbeans is doing for you that will have to be replaced.


after you create db derby and connect with localhost you need add line in file derby.properties locate in you directory database

derby.drda.host=10.0.0.40  //example IPAddress

save and go to connection in netbeans and change localhost to you ipaddress

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜