Commit 4bcbc73e authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

DB-init on Tomcat startup if db does not exist

parent 64ff375e
......@@ -21,9 +21,11 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/h2-1.4.197.jar"/>
<classpathentry kind="lib" path="C:/Program Files/Apache Software Foundation/Tomcat 8.0/lib/servlet-api.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
......@@ -3,4 +3,6 @@ package university.at.jku.ce.dao;
public interface DBInitializerDao {
public void initDatabase();
public boolean isValidDatabase();
}
......@@ -211,4 +211,56 @@ public class H2DBInitializerDao implements DBInitializerDao {
return p2.toAbsolutePath().toString();
}
@Override
public boolean isValidDatabase() {
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
// STEP 2: Open a connection
con = DriverManager.getConnection(DaoParam.JDBC_URL, DaoParam.USER, DaoParam.PASSWORD);
try {
String sql="SELECT * from student where rownum<2";
stmt = con.createStatement();
stmt.executeQuery(sql);
sql="SELECT * from study where rownum<2";
stmt = con.createStatement();
stmt.executeQuery(sql);
sql="SELECT * from subject where rownum<2";
stmt = con.createStatement();
stmt.executeQuery(sql);
sql="SELECT * from inscription where rownum<2";
stmt = con.createStatement();
stmt.executeQuery(sql);
} catch (SQLException e) {return false;}
// STEP 4: Clean-up environment
stmt.close();
con.close();
return true;
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
return false;
} catch (Exception e) {
// Handle errors for Class.forName
return false;
} finally {
// finally block used to close resources
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
} // nothing we can do
try {
if (con != null)
con.close();
} catch (SQLException se) {
se.printStackTrace();
} // end finally try
rs = null;
} // end try
}
}
package university.at.jku.ce.init;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import university.at.jku.ce.dao.DBInitializerDao;
import university.at.jku.ce.dao.h2dao.H2DBInitializerDao;
public class MyAppServletContextListener implements ServletContextListener{
DBInitializerDao dao=new H2DBInitializerDao();
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// Do Nothing
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
if (!dao.isValidDatabase()) {dao.initDatabase();
System.out.println("Initializing Database");
}
else {System.out.println("No Database initialization required");}
}
}
......@@ -15,4 +15,9 @@
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
university.at.jku.ce.init.MyAppServletContextListener
</listener-class>
</listener>
</web-app>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment