134 lines
4.6 KiB
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;
|
|
}
|
|
}
|