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
f1c775b1
Commit
f1c775b1
authored
Apr 24, 2019
by
Franz Reischl
Browse files
Adding DML SQL script with demo data upon DB init.
parent
ad4e69ec
Changes
4
Hide whitespace changes
Inline
Side-by-side
pom.xml
View file @
f1c775b1
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
university
</groupId>
<artifactId>
at.jku.ce
</artifactId>
<packaging>
war
</packaging>
<version>
1.1.0
</version>
<name>
at.jku.ce
</name>
<groupId>
university
</groupId>
<artifactId>
at.jku.ce
</artifactId>
<packaging>
war
</packaging>
<version>
1.1.0
</version>
<name>
at.jku.ce
</name>
<build>
<finalName>
at.jku.ce
</finalName>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
2.5.1
</version>
<inherited>
true
</inherited>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
</configuration>
</plugin>
</plugins>
</build>
<build>
<finalName>
at.jku.ce
</finalName>
<resources>
<resource>
<directory>
src/main/resources
</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
2.5.1
</version>
<inherited>
true
</inherited>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.glassfish.jersey
</groupId>
<artifactId>
jersey-bom
</artifactId>
<version>
${jersey.version}
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.glassfish.jersey.containers
</groupId>
<artifactId>
jersey-container-servlet-core
</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>
org.glassfish.jersey.inject
</groupId>
<artifactId>
jersey-hk2
</artifactId>
</dependency>
<dependency>
<groupId>
org.glassfish.jersey.media
</groupId>
<artifactId>
jersey-media-json-binding
</artifactId>
</dependency>
<dependency>
<groupId>
javax.xml.bind
</groupId>
<artifactId>
jaxb-api
</artifactId>
<version>
2.3.0
</version>
</dependency>
<dependency>
<groupId>
javax.activation
</groupId>
<artifactId>
activation
</artifactId>
<version>
1.1
</version>
</dependency>
<dependency>
<groupId>
com.sun.xml.bind
</groupId>
<artifactId>
jaxb-core
</artifactId>
<version>
2.2.8-b01
</version>
</dependency>
<dependency>
<groupId>
com.sun.xml.bind
</groupId>
<artifactId>
jaxb-impl
</artifactId>
<version>
2.2-promoted-b65
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>
com.h2database
</groupId>
<artifactId>
h2
</artifactId>
<version>
1.4.197
</version>
<scope>
test
</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat -->
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
servlet-api
</artifactId>
<version>
2.5
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
org.glassfish.jersey
</groupId>
<artifactId>
jersey-bom
</artifactId>
<version>
${jersey.version}
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>
2.27
</jersey.version>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.glassfish
</groupId>
<artifactId>
javax.servlet
</artifactId>
<version>
10.0-b28
</version>
</dependency>
<dependency>
<groupId>
org.glassfish.jersey.containers
</groupId>
<artifactId>
jersey-container-servlet-core
</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>
org.glassfish.jersey.inject
</groupId>
<artifactId>
jersey-hk2
</artifactId>
</dependency>
<dependency>
<groupId>
org.glassfish.jersey.media
</groupId>
<artifactId>
jersey-media-json-binding
</artifactId>
</dependency>
<dependency>
<groupId>
javax.xml.bind
</groupId>
<artifactId>
jaxb-api
</artifactId>
<version>
2.3.0
</version>
</dependency>
<dependency>
<groupId>
javax.activation
</groupId>
<artifactId>
activation
</artifactId>
<version>
1.1
</version>
</dependency>
<dependency>
<groupId>
com.sun.xml.bind
</groupId>
<artifactId>
jaxb-core
</artifactId>
<version>
2.2.8-b01
</version>
</dependency>
<dependency>
<groupId>
com.sun.xml.bind
</groupId>
<artifactId>
jaxb-impl
</artifactId>
<version>
2.2-promoted-b65
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>
com.h2database
</groupId>
<artifactId>
h2
</artifactId>
<version>
1.4.197
</version>
<scope>
test
</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat -->
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
servlet-api
</artifactId>
<version>
2.5
</version>
<scope>
provided
</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>
2.27
</jersey.version>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
</project>
src/main/java/university/at/jku/ce/dao/h2dao/H2DBInitializerDao.java
View file @
f1c775b1
package
university.at.jku.ce.dao.h2dao
;
import
java.io.BufferedReader
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.ws.rs.NotFoundException
;
import
university.at.jku.ce.dao.DBInitializerDao
;
import
university.at.jku.ce.model.Student
;
public
class
H2DBInitializerDao
implements
DBInitializerDao
{
private
Statement
stmt
;
private
Connection
con
;
private
ResultSet
rs
;
@Override
public
void
initDatabase
()
{
deleteAllTables
();
createAllTables
();
insertData
();
}
public
void
insertData
()
{
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
);
// STEP 3: Execute a query
try
(
BufferedReader
br
=
new
BufferedReader
(
new
FileReader
(
getSQLPath
()+
"//DMLSkript.txt"
)))
{
String
line
;
while
((
line
=
br
.
readLine
())
!=
null
)
{
String
sql
=
line
.
substring
(
0
,
line
.
length
()-
1
);
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
}
}
// STEP 4: Clean-up environment
stmt
.
close
();
con
.
close
();
}
catch
(
SQLException
se
)
{
// Handle errors for JDBC
se
.
printStackTrace
();
throw
new
RuntimeException
();
}
catch
(
Exception
e
)
{
// Handle errors for Class.forName
e
.
printStackTrace
();
}
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
}
public
void
createAllTables
()
{
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
);
// STEP 3: Execute a query
String
sql
=
"CREATE TABLE student (matrnr INTEGER PRIMARY KEY, firstname VARCHAR2(150), lastname VARCHAR2(150))"
;
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
sql
=
"CREATE TABLE study (studyid INTEGER PRIMARY KEY, name VARCHAR(150))"
;
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
sql
=
"CREATE TABLE subject (\r\n"
+
" studyId INTEGER,\r\n"
+
" subjectid INTEGER, \r\n"
+
" name VARCHAR2(150), \r\n"
+
" ects INTEGER, \r\n"
+
" PRIMARY KEY (studyid, subjectid),\r\n"
+
" FOREIGN KEY (studyId) REFERENCES study(studyId) ON DELETE CASCADE\r\n"
+
")"
;
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
sql
=
"CREATE TABLE inscription(\r\n"
+
" matrnr INTEGER,\r\n"
+
" studyid INTEGER,\r\n"
+
" ins_date DATE,\r\n"
+
" FOREIGN KEY (studyid) REFERENCES study(studyid),\r\n"
+
" FOREIGN KEY (matrnr) REFERENCES student(matrnr),\r\n"
+
" PRIMARY KEY (matrnr, studyid)\r\n"
+
")"
;
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
// STEP 4: Clean-up environment
stmt
.
close
();
con
.
close
();
}
catch
(
SQLException
se
)
{
// Handle errors for JDBC
se
.
printStackTrace
();
throw
new
RuntimeException
();
}
catch
(
Exception
e
)
{
// Handle errors for Class.forName
e
.
printStackTrace
();
}
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
}
public
void
deleteAllTables
()
{
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
);
// STEP 3: Execute a query
String
sql
=
"DROP TABLE student CASCADE CONSTRAINTS"
;
try
{
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
}
catch
(
Throwable
t
)
{
System
.
out
.
println
(
"Table student did not exist"
);
}
sql
=
"DROP TABLE study CASCADE CONSTRAINTS"
;
try
{
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
}
catch
(
Throwable
t
)
{
System
.
out
.
println
(
"Table study did not exist"
);
}
sql
=
"DROP TABLE subject CASCADE CONSTRAINTS"
;
try
{
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
}
catch
(
Throwable
t
)
{
System
.
out
.
println
(
"Table subject did not exist"
);
}
sql
=
"DROP TABLE inscription CASCADE CONSTRAINTS"
;
try
{
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
}
catch
(
Throwable
t
)
{
System
.
out
.
println
(
"Table inscription did not exist"
);
}
// STEP 4: Clean-up environment
stmt
.
close
();
con
.
close
();
}
catch
(
SQLException
se
)
{
// Handle errors for JDBC
se
.
printStackTrace
();
throw
new
RuntimeException
();
}
catch
(
Exception
e
)
{
// Handle errors for Class.forName
e
.
printStackTrace
();
}
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
}
private
static
String
getSQLPath
()
{
// Create database directory, if not exists already
Path
p2
=
Paths
.
get
(
System
.
getProperty
(
"catalina.base"
),
"sql"
);
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.dao.h2dao
;
import
java.io.BufferedReader
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.ws.rs.NotFoundException
;
import
university.at.jku.ce.dao.DBInitializerDao
;
import
university.at.jku.ce.model.Student
;
public
class
H2DBInitializerDao
implements
DBInitializerDao
{
private
Statement
stmt
;
private
Connection
con
;
private
ResultSet
rs
;
@Override
public
void
initDatabase
()
{
deleteAllTables
();
createAllTables
();
insertData
();
}
public
void
insertData
()
{
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
);
// STEP 3: Execute a query
try
(
BufferedReader
br
=
new
BufferedReader
(
new
FileReader
(
getClass
().
getClassLoader
().
getResource
(
"DML_script.sql"
).
getFile
()
)))
{
String
line
;
while
((
line
=
br
.
readLine
())
!=
null
)
{
String
sql
=
line
.
substring
(
0
,
line
.
length
()-
1
);
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
}
}
// STEP 4: Clean-up environment
stmt
.
close
();
con
.
close
();
}
catch
(
SQLException
se
)
{
// Handle errors for JDBC
se
.
printStackTrace
();
throw
new
RuntimeException
();
}
catch
(
Exception
e
)
{
// Handle errors for Class.forName
e
.
printStackTrace
();
}
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
}
public
void
createAllTables
()
{
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
);
// STEP 3: Execute a query
String
sql
=
"CREATE TABLE student (matrnr INTEGER PRIMARY KEY, firstname VARCHAR2(150), lastname VARCHAR2(150))"
;
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
sql
=
"CREATE TABLE study (studyid INTEGER PRIMARY KEY, name VARCHAR(150))"
;
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
sql
=
"CREATE TABLE subject (\r\n"
+
" studyId INTEGER,\r\n"
+
" subjectid INTEGER, \r\n"
+
" name VARCHAR2(150), \r\n"
+
" ects INTEGER, \r\n"
+
" PRIMARY KEY (studyid, subjectid),\r\n"
+
" FOREIGN KEY (studyId) REFERENCES study(studyId) ON DELETE CASCADE\r\n"
+
")"
;
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
sql
=
"CREATE TABLE inscription(\r\n"
+
" matrnr INTEGER,\r\n"
+
" studyid INTEGER,\r\n"
+
" ins_date DATE,\r\n"
+
" FOREIGN KEY (studyid) REFERENCES study(studyid),\r\n"
+
" FOREIGN KEY (matrnr) REFERENCES student(matrnr),\r\n"
+
" PRIMARY KEY (matrnr, studyid)\r\n"
+
")"
;
stmt
=
con
.
createStatement
();
stmt
.
executeUpdate
(
sql
);
// STEP 4: Clean-up environment
stmt
.
close
();
con
.
close
();
}
catch
(
SQLException
se
)
{
// Handle errors for JDBC
se
.
printStackTrace
();
throw
new
RuntimeException
();
}
catch
(
Exception
e
)
{
// Handle errors for Class.forName
e
.
printStackTrace
();
}
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
)
{