开发者

Java: Embedded Data Base

I want t开发者_JAVA技巧o create a desctop application with an embedded data base. Data base is JavaDB(Derby). I have connected a jar file derby.jar to my project. The problem is I don't understand how to register a driver to use this data base. It is said that I should use Class.forName("org.apache.derby.jdbc.EmbeddedDriver") But what if that was another data base and its driver was not in a java standart package? As you can see I'm confused with this. I want to know, how to use my connected derby.jar, how to work with its jdbc driver and how to create tables in a specified directory.

Please, give as detailed answer, as you can. (I'm a dummy in this =)) )


You use a statement like:

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

to load and register the JDBC driver class for Derby, so that the JDBC java.sql.DriverManager can find the driver when you want to connect to the database. If you want to connect to a different database or use a different driver implementation, you'll have to change the name for the driver you're using. Ofcourse you can put the information in a configuration file instead of hard-coding it in your program, so that you can change the driver without re-compiling your program.

For example, put the necessary information in a configuration file database.properties:

jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver
jdbc.url=jdbc:derby:derbyDB;create=true
jdbc.username=dbusername
jdbc.password=dbpassword

Then load those settings in your program and use them to open a database connection:

InputStream in = new FileInputStream("database.properties");
Properties props = new Properties();
props.load(in);
in.close();

String driver = props.getProperty("jdbc.driver");
Class.forName(driver);

String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");

Connection conn = DriverManager.getConnection(url, username, password);

See the Apache Derby Tutorial and Sun's JDBC Tutorial.


To create a database in another directory with Derby, specify the directory in the JDBC connection URL, for example:

Connection conn = DriverManager.getConnection("jdbc:derby:c:/otherDirectory/myDB");

See the Apache Derby documentation; this example comes from the section Connecting to databases of the Derby Developer's Guide.


Actually Derby isn't part of the "standard" Java package, it is included in Sun's distribution for Windows, but it's not included in OS X by default for example.

To answer what appears to be your question, you would add the jar file for the required database to your class path, and use Class.forName("") with the appropriate driver for that database.


The driver registration is not needed at all if you're using Java 6, which comes with JDBC 4. JDBC 4 infers the driver required from the connection URL. The driver, of course, should be somewhere on your classpath... Otherwise I'd second Jesper's answer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜