vitaly
vitaly

Reputation: 11

how to set up wanted db in wanted location with derby in java

I want to build wanted db in wanted location programly in java.

    public class DerbyCreateTable {
 public static void main(String [] args) {
Connection con = null;
try {
  con = DriverManager.getConnection(
    "jdbc:derby://localhost/TestDB");

 // Creating a database table
  Statement sta = con.createStatement(); 
  int count = sta.executeUpdate(
    "CREATE TABLE HY_Address (ID INT, StreetName VARCHAR(20),"
    + " City VARCHAR(20))");
  System.out.println("Table created.");
  sta.close();        

  con.close();        
} catch (Exception e) {
  System.err.println("Exception: "+e.getMessage());
}
}
}

but how can I set up where to create it and how to creat the new location that I want ? thanks

Upvotes: 1

Views: 2151

Answers (1)

Qwerky
Qwerky

Reputation: 18435

You are almost there. First you need to load the driver, for example;

private static String embeddedDriver = "org.apache.derby.jdbc.EmbeddedDriver";

then in your main;

Class.forName(embeddedDriver).newInstance();

Second you need to use a URL like this to specify a location on the file system;

jdbc:derby:/dir/to/create/database;create=true

Upvotes: 1

Related Questions