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

Wie besprochen: Schnittstelle student/{matrNr}/studies.... erweitert

parent 107977a8
This diff is collapsed.
......@@ -18,4 +18,10 @@ public interface StudentDao {
public void removeStudent(int matrNr);
public List<Study> getInscriptions(int matrNr);
public Study addInscription(int matrNr, Study study);
public Study getInscription(int matrNr, int studyId);
public void removeInscription(int matrNr, int studyId);
}
package university.at.jku.ce.dao.h2dao;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DaoParam {
protected static final String DRIVER="org.h2.Driver";
protected static final String USER="sa";
protected static final String PASSWORD="";
protected static final String JDBC_URL="jdbc:h2:C:\\Users\\Michael\\development\\workspace-ce5\\at.jku.ce\\data\\Database";
protected static final String JDBC_URL="jdbc:h2:"+getDbPath();
private static String getDbPath() {
// Create database directory, if not exists already
Path p2 = Paths.get(System.getProperty("catalina.base"), "db");
if(!Files.isDirectory(p2)) {
try {
Files.createDirectory(p2);
} catch (IOException e1) {
e1.printStackTrace();
}
}
// Return path as absolute path
return Paths.get(p2.toAbsolutePath().toString(),"Database").toAbsolutePath().toString();
}
}
package university.at.jku.ce.dao.h2dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
......@@ -175,6 +176,7 @@ public class H2StudentDao implements StudentDao {
@Override
public Student updateStudent(Student student) {
getStudent(student.getMatrNr());
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
......@@ -219,6 +221,7 @@ public class H2StudentDao implements StudentDao {
@Override
public void removeStudent(int matrNr){
getStudent(matrNr);
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
......@@ -303,4 +306,146 @@ public class H2StudentDao implements StudentDao {
if (inscriptions.size()==0) throw new NotFoundException();
return inscriptions;
}
@Override
public Study addInscription(int matrNr, Study study) {
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);
stmt = con.createStatement();
String sql = "INSERT INTO inscription values(?,?,sysdate)";
prStmt = con.prepareStatement(sql);
prStmt.setInt(1, matrNr);
prStmt.setInt(2, study.getStudyId());
prStmt.executeUpdate();
// STEP 5: Clean-up environment
stmt.close();
prStmt.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(prStmt!=null) prStmt.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
study=getInscription(matrNr,study.getStudyId());
return study;
}
@Override
public Study getInscription(int matrNr, int studyId) {
List<Study> studies=new ArrayList<Study>();
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 = "SELECT s.* FROM Study s INNER JOIN Inscription i ON s.studyId=i.studyId AND i.matrNr=? AND i.studyId=?";
prStmt = con.prepareStatement(sql);
prStmt.setInt(1, matrNr);
prStmt.setInt(2, studyId);
rs=prStmt.executeQuery();
while (rs.next()) {
studies.add(new Study(rs.getInt(1),rs.getString(2)));
}
// STEP 4: Clean-up environment
prStmt.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(prStmt!=null) prStmt.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
if (studies.size()==1) {return studies.get(0);}
else {
if (studies.size()<1) {throw new NotFoundException();}
else throw new RuntimeException();
}
}
@Override
public void removeInscription(int matrNr, int studyId) {
getInscription(matrNr,studyId);
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 delete
String sql = "DELETE FROM inscription where matrnr=? AND studyId=?";
prStmt = con.prepareStatement(sql);
prStmt.setInt(1, matrNr);
prStmt.setInt(2, studyId);
prStmt.executeUpdate();
// STEP 5: Clean-up environment
prStmt.close();
con.close();
} catch(SQLException se) {
se.printStackTrace();
throw new RuntimeException();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
throw new RuntimeException();
} finally {
//finally block used to close resources
try{
if(prStmt!=null) prStmt.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
}
}
......@@ -184,6 +184,7 @@ public class H2StudyDao implements StudyDao{
@Override
public Study updateStudy(Study study) {
getStudy(study.getStudyId());
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
......@@ -226,6 +227,7 @@ public class H2StudyDao implements StudyDao{
@Override
public void removeStudy(int studyId) {
getStudy(studyId);
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
......
......@@ -177,6 +177,7 @@ public class H2SubjectDao implements SubjectDao {
@Override
public Subject updateSubject(Subject subject) {
getSubject(subject.getStudyId(), subject.getSubjectId());
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
......@@ -221,6 +222,7 @@ public class H2SubjectDao implements SubjectDao {
@Override
public void removeSubject(int studyId, int subjectId) {
getSubject(studyId,subjectId);
try {
// STEP 1: Register JDBC driver
Class.forName(DaoParam.DRIVER);
......
......@@ -74,7 +74,7 @@ public class StudentResource {
}
@GET
@Path("/{studentMatrNr}/inscriptions")
@Path("/{studentMatrNr}/studies")
@Produces(MediaType.APPLICATION_JSON)
public Response getInscriptions(@PathParam("studentMatrNr")int matrNr) {
List<Study>list= dao.getInscriptions(matrNr);
......@@ -82,4 +82,30 @@ public class StudentResource {
return Response.ok(entity).build();
}
@POST
@Path("/{studentMatrNr}/studies")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addInscription(@PathParam("studentMatrNr")int matrNr, Study study, @Context UriInfo uriInfo) {
study= dao.addInscription(matrNr, study);
URI uri=uriInfo.getAbsolutePathBuilder().path(String.valueOf(study.getStudyId())).build();
return Response.created(uri).entity(study).build();
}
@GET
@Path("/{studentMatrNr}/studies/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getInscription(@PathParam("studentMatrNr")int matrNr, @PathParam("studyId")int studyId) {
Study study= dao.getInscription(matrNr, studyId);
return Response.ok().entity(study).build() ;
}
@DELETE
@Path("/{studentMatrNr}/studies/{studyId}")
@Produces(MediaType.APPLICATION_JSON)
public Response deleteInscription(@PathParam("studentMatrNr")int matrNr,@PathParam("studyId")int studyId ) {
dao.removeInscription(matrNr, studyId);
return Response.noContent().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