Commit 64ff375e authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

DB Init Skript wird jetzt mit GET auf .../webapi/dbinit ausgeführt

parent 3a680332
DROP TABLE subject CASCADE CONSTRAINTS;
DROP TABLE student CASCADE CONSTRAINTS;
DROP TABLE study CASCADE CONSTRAINTS;
DROP TABLE inscription CASCADE CONSTRAINTS;
-- Create Tables
CREATE TABLE student (matrnr INTEGER PRIMARY KEY, firstname VARCHAR2(150), lastname VARCHAR2(150));
CREATE TABLE study (studyid INTEGER PRIMARY KEY, name VARCHAR(150));
CREATE TABLE subject (
studyId INTEGER,
subjectid INTEGER,
name VARCHAR2(150),
ects INTEGER,
PRIMARY KEY (studyid, subjectid),
FOREIGN KEY (studyId) REFERENCES study(studyId) ON DELETE CASCADE
);
CREATE TABLE inscription(
matrnr INTEGER,
studyid INTEGER,
ins_date DATE,
FOREIGN KEY (studyid) REFERENCES study(studyid),
FOREIGN KEY (matrnr) REFERENCES student(matrnr),
PRIMARY KEY (matrnr, studyid)
);
-- Insert Data sutdent
INSERT INTO student(matrnr, firstname,lastname) values(1, 'Andres', 'Iniesta');
INSERT INTO student(matrnr, firstname,lastname) values(2, 'Lionel', 'Messi');
INSERT INTO student(matrnr, firstname,lastname) values(3, 'David', 'Silva');
INSERT INTO student(matrnr, firstname,lastname) values(4, 'Luka', 'Modric');
INSERT INTO student(matrnr, firstname,lastname) values(5, 'Marcel', 'Hirscher');
INSERT INTO student(matrnr, firstname,lastname) values(6, 'Henrik', 'Kristoffersen');
INSERT INTO student(matrnr, firstname,lastname) values(7, 'Aksel Lund', 'Svindal');
INSERT INTO student(matrnr, firstname,lastname) values(8, 'Hermann', 'Maier');
-- Insert Data study
INSERT INTO study(studyId, name) values(1, 'Wirtschftsinformatik');
INSERT INTO study(studyId, name) values(2, 'Informatik');
INSERT INTO study(studyId, name) values(3, 'Mathematik');
INSERT INTO study(studyId, name) values(4, 'Physiotherapie');
INSERT INTO study(studyId, name) values(5, 'Gesundheits- und Krankenpflege');
INSERT INTO study(studyId, name) values(6, 'Sportwissenschaften');
-- Insert Data subject
INSERT INTO subject(studyid, subjectid, name, ects) values(1,1,'Communications Engineering',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(1,2,'Grundlagen Softwareentwicklung',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(1,3,'Vertiefung Softwareentwicklung',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(2,1,'Softwareentwicklung 1',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(2,2,'Softwareentwicklung 2',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(2,3,'Elektronik',4);
INSERT INTO subject(studyid, subjectid, name, ects) values(3,1,'Algebra',3);
INSERT INTO subject(studyid, subjectid, name, ects) values(3,2,'Mathematik 1',3);
INSERT INTO subject(studyid, subjectid, name, ects) values(4,1,'Bewegungslehre',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(4,2,'Trainingslehre',2);
INSERT INTO subject(studyid, subjectid, name, ects) values(4,3,'Spezifische Anatomie',2);
INSERT INTO subject(studyid, subjectid, name, ects) values(5,1,'Allgemeine Anatomie',3);
INSERT INTO subject(studyid, subjectid, name, ects) values(5,2,'Allgemeine Physiologie',3);
INSERT INTO subject(studyid, subjectid, name, ects) values(6,1,'Trainingslehre',6);
INSERT INTO subject(studyid, subjectid, name, ects) values(6,2,'Bewegungslehre',3);
-- Insert Data inscription
INSERT INTO inscription(matrnr, studyid, ins_date) values (1,1,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (1,2,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (1,3,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (2,1,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (2,2,sysdate);
INSERT INTO inscription(matrnr, studyid, ins_date) values (3,3,sysdate);
\ No newline at end of file
package university.at.jku.ce.dao;
public interface DBInitializerDao {
public void initDatabase();
}
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();
}
}
package university.at.jku.ce.resource;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import university.at.jku.ce.dao.DBInitializerDao;
import university.at.jku.ce.dao.h2dao.H2DBInitializerDao;
import university.at.jku.ce.model.Student;
@Path("/dbinit")
public class DBInitializerResource {
DBInitializerDao initDao=new H2DBInitializerDao();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response dbinit() {
initDao.initDatabase();
return Response.ok().build();
}
}
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