Embedding JavaDB in your desktop application
By pblaha on Jun 05, 2006
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?