/ Published in: Java

A simple utility class that uses the H2 SQL Db to maintain a database connection. It also gives a convenience method to run SQL scripts.
_Note:_The init.sql value is hardcoded, and should be replaced by a static variable if needed
_Note:_The init.sql value is hardcoded, and should be replaced by a static variable if needed
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; import org.h2.tools.DeleteDbFiles; public class DbUtil { static { try { Class.forName("org.h2.Driver"); Logger.getLogger(DbUtil.class.getName()).log(Level.SEVERE, null, ex); } } if(null == myConnection || myConnection.isClosed()) { } } if(null==myConnection || myConnection.isClosed()) { Logger.getLogger(DbUtil.class.getName()).log(Level.SEVERE, null, ex); throw ex; } return myConnection.createStatement(); } myConnection.close(); } setupConn(theDbPath); runScript("init.sql"); } stat.execute("runscript from '"+thePath+"'"); stat.close(); } // to separate the dbname from the path int lastSlash = theDbPath.lastIndexOf('/'); DeleteDbFiles.execute(theDbPath.substring(0,lastSlash), theDbPath.substring(lastSlash), true); setupDB(theDbPath); } }
Comments
