| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| On Tuesday 22 July 2008 05:29:15 am Shinkan wrote: > Hi there ! > > I'm really happy that I discovered Derby. It's a perfect embedded > lightweight data management system ! > As I really think that documentation is complete, it's sometimes hard to > find something precise. > > I would like to know if I can (and how), using Java JDBC Derby Embedded > Driver, choose the location my database files are stocked in. If you are not using the toplink, then you can set it either by hard coded path or app.path. You set the framework, driver, protocol and connection. Then - in a try/catch block - you connect and do your stuff. String databasename = System.getProperty("user.home") + java.io.File.separator + ".your_app_dir" + java.io.File.separator + "data" + java.io.File.separator; I put the database for a single-user embedded app in the user's home folder. In Wintendo is is either C:\Documents And Settings\UserName or C: \Users\UserName. In Linux or Mac it is /home/username. The dot in front of the your_app_dir makes it hidden in Linux/Unix/Mac and just separates it in Windows. public String framework = "embedded"; public String driver = "org.apache.derby.jdbc.EmbeddedDriver"; public String protocol = "jdbc:derby:"; public Connection conn = null; I then have an openDatabase() method that opens the database to allow me to work with it. public void openDatabase() { //open the database conneciton try { Class.forName(driver).newInstance(); Properties props = new Properties(); props.put("user", "dbuser"); props.put("password", "dbuser"); conn = DriverManager.getConnection(protocol + databasename, props); } catch (Throwable e) { System.out.println("exception thrown:"); if (e instanceof SQLException) { printSQLError((SQLException) e); } else { e.printStackTrace(); } } } You can, of course, request your username and password to be something else when you setup the database. One important thing - don't forget to close the database when done. Otherwise there's a lock file left in the database directory and you won't be able to open it. I check for this lock file when starting my applications and try to delete it if it exists. I actually close the database twice - once when I'm done with whatever I'm doing and again when I exit the applications. (This is more for insurance than anything else.) public void closeDatabase(){ boolean gotSQLExc = false; try { DriverManager.getConnection("jdbc:derby:;shutdown= true"); } catch (SQLException se) { gotSQLExc = true; } if (!gotSQLExc) { System.out.println("Database did not shut down normally"); } else { System.out.println("Database shut down normally"); } } -- kai www.filesite.org || www.4thedadz.com || www.perfectreign.com remember - a turn signal is a statement, not a request |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.