Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Franz Reischl
tomcat-miniREST
Commits
4bcbc73e
Commit
4bcbc73e
authored
Apr 23, 2019
by
Michael Schimpelsberger
Browse files
DB-init on Tomcat startup if db does not exist
parent
64ff375e
Changes
5
Hide whitespace changes
Inline
Side-by-side
.classpath
View file @
4bcbc73e
...
...
@@ -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>
src/main/java/university/at/jku/ce/dao/DBInitializerDao.java
View file @
4bcbc73e
...
...
@@ -3,4 +3,6 @@ package university.at.jku.ce.dao;
public
interface
DBInitializerDao
{
public
void
initDatabase
();
public
boolean
isValidDatabase
();
}
src/main/java/university/at/jku/ce/dao/h2dao/H2DBInitializerDao.java
View file @
4bcbc73e
...
...
@@ -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
}
}
src/main/java/university/at/jku/ce/init/MyAppServletContextListener.java
0 → 100644
View file @
4bcbc73e
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"
);}
}
}
src/main/webapp/WEB-INF/web.xml
View file @
4bcbc73e
...
...
@@ -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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment