Commit e504c084 authored by Ondra's avatar Ondra
Browse files

Edit Repozitaru

parent 7275a0fa
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.entities;
/**
......
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.entities;
import java.util.Objects;
......
package cz.cvut.fel.skoumond.exceptions;
/**
*
* @author Ondra
*/
public class DataEntryException extends RuntimeException {
public DataEntryException() {
super();
}
public DataEntryException(String string) {
super(string);
}
public DataEntryException(String string, Throwable thrwbl) {
super(string, thrwbl);
}
public DataEntryException(Throwable thrwbl) {
super(thrwbl);
}
protected DataEntryException(String string, Throwable thrwbl, boolean bln, boolean bln1) {
super(string, thrwbl, bln, bln1);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.exceptions;
/**
......
......@@ -4,14 +4,13 @@
*/
package cz.cvut.fel.skoumond.sql;
import cz.cvut.fel.skoumond.entities.BaseEntity;
import java.util.List;
/**
*
* @author Ondra
*/
public interface IRepository<T extends BaseEntity> {
public interface IRepository<T> {
public T findById(int index);
......
......@@ -4,6 +4,7 @@
*/
package cz.cvut.fel.skoumond.sql;
import cz.cvut.fel.skoumond.entities.BaseEntity;
import cz.cvut.fel.skoumond.exceptions.NoConnectionException;
import java.sql.Connection;
import java.sql.DriverManager;
......@@ -13,28 +14,22 @@ import java.sql.SQLException;
*
* @author Ondra
*/
public class SQLManager {
public abstract class Repository<T extends BaseEntity> implements IRepository<T> {
protected static Connection getConnection() {
private SQLManager() {
}
public static Connection getConnection() {
try {
// JDBC driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
}
catch (java.lang.ClassNotFoundException e) {
} catch (java.lang.ClassNotFoundException e) {
System.err.println("Chyba při načtení databázového ovladače: " + e.getMessage());
}
try (Connection connection = DriverManager.getConnection("jdbc:derby:database;create=true", "skoumond", "heslo")) {
return connection;
}
catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází" + e.getMessage());
try {
return DriverManager.getConnection("jdbc:derby:database;create=true", "skoumond", "heslo");
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.sql;
import cz.cvut.fel.skoumond.entities.RunEntity;
import java.util.List;
/**
*
* @author Ondra
*/
public class RunRepository extends Repository<RunEntity> {
@Override
public RunEntity findById(int index) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List<RunEntity> all() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void save(RunEntity entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void update(RunEntity entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void delete(RunEntity entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.sql;
import cz.cvut.fel.skoumond.entities.TypeEntity;
import cz.cvut.fel.skoumond.exceptions.DataEntryException;
import cz.cvut.fel.skoumond.exceptions.NoConnectionException;
import static cz.cvut.fel.skoumond.sql.Repository.getConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Ondra
*/
public class TypeRepository extends Repository<TypeEntity> {
private final String FINDBYID = "SELECT * FROM APP.TYPE WHERE T_ID = ?";
private final String ALL = "SELECT * APP.TYPE";
private final String INSERT = "INSERT INTO APP.TYPE VALUES (?, ?)";
private final String UPDATE = "UPDATE APP.TYPE SET T_TYPE = ? WHERE T_ID = ?";
private final String DELETE = "DELETE FROM APP.TYPE WHERE T_ID = ?";
@Override
public TypeEntity findById(int index) throws NoConnectionException {
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(FINDBYID)) {
ps.setInt(1, index);
ResultSet rs = ps.executeQuery();
rs.next();
return new TypeEntity(rs.getInt(1), rs.getString(2));
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
@Override
public List<TypeEntity> all() throws NoConnectionException {
List<TypeEntity> arrayList = new ArrayList();
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(ALL)) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
arrayList.add(new TypeEntity(rs.getInt(1), rs.getString(2)));
}
return arrayList;
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
@Override
public void save(TypeEntity entity) throws NoConnectionException {
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(INSERT)) {
ps.setInt(1, entity.getId());
ps.setString(2, entity.getType());
ps.executeUpdate();
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
@Override
public void update(TypeEntity entity) throws NoConnectionException, DataEntryException{
try (Connection con = getConnection();) {
con.setAutoCommit(false);
try (PreparedStatement ps = con.prepareStatement(UPDATE)) {
ps.setString(1, entity.getType());
ps.setInt(2, entity.getId());
ps.executeUpdate();
con.commit();
con.setAutoCommit(true);
} catch (SQLException e) {
con.rollback();
con.setAutoCommit(true);
System.err.println("Chyba při práci s databází: " + e.getMessage());
throw new DataEntryException();
}
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
@Override
public void delete(TypeEntity entity) throws NoConnectionException {
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(DELETE)) {
ps.setInt(1, entity.getId());
ps.executeUpdate();
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
}
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