1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Projection_Planning/src/main/java/DB/DB.java

134 lines
4.6 KiB
Java

package DB;
import Exceptions.NotFoundInTable;
import lombok.Data;
import lombok.Getter;
import java.sql.*;
public class DB {
static final private String host = "";
static final private String database = "";
static final private String user = "";
static final private String password = "";
@Getter static private Connection connection;
static public boolean connect() {
try {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection("jdbc:mariadb://"+host+"/"+database+"?user="+user+"&password="+password);
return true;
}
} catch (SQLException e) {
return false;
}
return false;
}
static public boolean disconnect() {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
connection = null;
}
} catch (SQLException e) {
return false;
}
return false;
}
static public boolean isConnected() {
try {
if (connection != null && !connection.isClosed())
return true;
} catch (SQLException e) {
return false;
}
return false;
}
static public <T> T get(Class<T> cl, String table, String value, String check, String checkValue) {
try {
PreparedStatement ps = DB.getConnection().prepareStatement("SELECT "+value+" FROM "+table+" WHERE "+check+" = ?");
ps.setString(1, checkValue);
if (ps.execute()) {
ResultSet rs = ps.getResultSet();
rs.next();
Object returnValue;
if (cl == String.class)
returnValue = rs.getString(value);
else if (cl == Integer.class)
returnValue = rs.getInt(value);
else if (cl == Time.class)
returnValue = rs.getTime(value);
else if (cl == Data.class)
returnValue = rs.getDate(value);
else if (cl == Competition.class)
returnValue = new Competition(rs.getString(value));
else if (cl == Movie.class)
returnValue = new Movie(rs.getString(value));
else if (cl == Projection.class)
returnValue = new Projection(rs.getInt(value));
else if (cl == Room.class)
returnValue = new Room(rs.getString(value));
else if (cl == Slot.class)
returnValue = new Slot(rs.getInt(value));
else if (cl == User.class)
returnValue = new Slot(rs.getInt(value));
else if (cl == UserType.class)
returnValue = new UserType(rs.getString(value));
else
returnValue = rs.getObject(value);
ps.close();
return (T)returnValue;
}
} catch (SQLException | NotFoundInTable e) {
return null;
}
return null;
}
static public boolean set(String table, String value, String newValue, String check, String checkValue) {
try {
PreparedStatement ps = DB.connection.prepareStatement("UPDATE "+table+" SET "+value+" = ? WHERE "+check+" = ?");
ps.setString(1, newValue);
ps.setString(2, checkValue);
int r = ps.executeUpdate();
ps.close();
if (r > 0)
return true;
} catch (SQLException e) {
return false;
}
return false;
}
static public boolean exist(String table, String check, String checkValue) {
try {
boolean r = false;
PreparedStatement ps = DB.connection.prepareStatement("SELECT * FROM "+table+" WHERE "+check+" = ?");
ps.setString(1, checkValue);
ps.execute();
if (ps.getResultSet().next())
r = true;
ps.close();
return r;
} catch (SQLException e) {
return false;
}
}
static public boolean delete(String table, String check, String checkValue) {
try {
PreparedStatement ps = DB.connection.prepareStatement("DELETE FROM "+table+" WHERE "+check+" = ?");
ps.setString(1, checkValue);
int r = ps.executeUpdate();
ps.close();
if (r > 0)
return true;
} catch (SQLException e) {
return false;
}
return false;
}
}