mirror of
https://github.com/TotalFreedomMC/TF-ProjectKorra.git
synced 2025-02-15 21:29:05 +00:00
File Cleanup and switch to PreparedStatements
This commit is contained in:
parent
91aa8dcdd2
commit
7980a92473
1 changed files with 13 additions and 16 deletions
|
@ -8,11 +8,11 @@ import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.PreparedStatement;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public abstract class Database {
|
public abstract class Database {
|
||||||
|
|
||||||
protected final Logger log;
|
protected final Logger log;
|
||||||
protected final String prefix;
|
protected final String prefix;
|
||||||
protected final String dbprefix;
|
protected final String dbprefix;
|
||||||
|
@ -50,7 +50,7 @@ public abstract class Database {
|
||||||
* @return Connection if exists, else null
|
* @return Connection if exists, else null
|
||||||
*/
|
*/
|
||||||
public Connection getConnection() {
|
public Connection getConnection() {
|
||||||
return this.connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,9 +64,9 @@ public abstract class Database {
|
||||||
* Close connection to Database.
|
* Close connection to Database.
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
if (!(this.connection == null)) {
|
if (connection != null) {
|
||||||
try {
|
try {
|
||||||
this.connection.close();
|
connection.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -85,9 +85,8 @@ public abstract class Database {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Statement stmt = connection.createStatement();
|
PreparedStatement stmt = connection.prepareStatement(query);
|
||||||
stmt.execute(query);
|
stmt.execute();
|
||||||
|
|
||||||
stmt.close();
|
stmt.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -104,8 +103,8 @@ public abstract class Database {
|
||||||
*/
|
*/
|
||||||
public ResultSet readQuery(String query) {
|
public ResultSet readQuery(String query) {
|
||||||
try {
|
try {
|
||||||
Statement stmt = this.connection.createStatement();
|
PreparedStatement stmt = connection.prepareStatement(query);
|
||||||
ResultSet rs = stmt.executeQuery(query);
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
return rs;
|
return rs;
|
||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
|
@ -122,15 +121,13 @@ public abstract class Database {
|
||||||
*/
|
*/
|
||||||
public boolean tableExists(String table) {
|
public boolean tableExists(String table) {
|
||||||
try {
|
try {
|
||||||
DatabaseMetaData dmd = this.connection.getMetaData();
|
DatabaseMetaData dmd = connection.getMetaData();
|
||||||
ResultSet rs = dmd.getTables(null, null, table, null);
|
ResultSet rs = dmd.getTables(null, null, table, null);
|
||||||
|
|
||||||
if(rs.next()) return true;
|
return rs.next();
|
||||||
else return false;
|
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue