Commit f2e4104e authored by Michael Schimpelsberger's avatar Michael Schimpelsberger
Browse files

removed redundant methods

parent 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(
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) {
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) {
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
}
@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
}
}
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, 'Luis', 'Suarez');
INSERT INTO student(matrnr, firstname,lastname) values(6, 'Cristiano', 'Ronaldo');
INSERT INTO student(matrnr, firstname,lastname) values(7, 'Andrea', 'Pirlo');
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, 'Sportwissenschaften');
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 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);
INSERT INTO inscription(matrnr, studyid, ins_date) values (4,3,sysdate);
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, 'Luis', 'Suarez');
INSERT INTO student(matrnr, firstname,lastname) values(6, 'Cristiano', 'Ronaldo');
INSERT INTO student(matrnr, firstname,lastname) values(7, 'Andrea', 'Pirlo');
INSERT INTO student(matrnr, firstname,lastname) values(8, 'Francesco', 'Totti');
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, 'Sportwissenschaften');
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);