Embedding JavaDB in your desktop application

JavaDB is very lightweight database that might be used for storing data in your desktop applications. I would like to show how to use embedded JavaDB in your applications. Let's to create desktop application that uses embedded database. In his mode the Derby database engine runs inside the same Java Virtual Machine (JVM) as the application. At first you need to download Derby database from here. Then, create new J2SE project and add derby.jar from Derby DB to project's classpath. We should load EmbeddedDriver, connect, add SQL code for inserting, selecting application data and we are done. See code snapshot:
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
    Connectionconn= 
     DriverManager.getConnection("jdbc:derby:myDB;create=true");
    conn.createStatement().execute("CREATE TABLE ...");
    ..... 
  
Now, run your applications and myDB directory with JavaDB files should be created in working directory. If you want to change a location when thies directory is created use derby.system.home Java system property. Persisting application data with embedded JavaDB is easy, isn't it?
Comments:

Post a Comment:
Comments are closed for this entry.
About

pblaha

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks