+ Added prepared statements
This commit is contained in:
@@ -35,7 +35,7 @@ public class DatabaseDriver
|
||||
driver.Connect();
|
||||
|
||||
// Require a global table if it doesn't exist already
|
||||
driver.ExecuteStatement("CREATE TABLE IF NOT EXISTS " + globalTableName + " (" + globalKeyName + " text PRIMARY KEY, " + globalValueName + " text);");
|
||||
driver.ExecuteStatement("CREATE TABLE IF NOT EXISTS " + globalTableName + " (" + globalKeyName + " text PRIMARY KEY, " + globalValueName + " text);", null);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -75,15 +75,15 @@ public class DatabaseDriver
|
||||
// Prototype formatting for key updating
|
||||
private void UpdateKey(String key, String value)
|
||||
{
|
||||
String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = '" + value + "' WHERE " + globalKeyName + "= '" + key + "';";
|
||||
driver.ExecuteStatement(command);
|
||||
String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = ? WHERE " + globalKeyName + " = ?;";
|
||||
driver.ExecuteStatement(command, new String[] { key, value });
|
||||
}
|
||||
|
||||
// Protoype updating for seeing if a key exists
|
||||
private boolean HasKey(String key)
|
||||
{
|
||||
String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = '" + key + "';";
|
||||
ResultSet set = driver.ExecuteReturningStatement(command);
|
||||
String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = ?;";
|
||||
ResultSet set = driver.ExecuteReturningStatement(command, new String[] { key });
|
||||
String out = ResultAsString(set, "count");
|
||||
return out.charAt(0) == '1';
|
||||
}
|
||||
@@ -91,16 +91,16 @@ public class DatabaseDriver
|
||||
// Prototype for getting a key
|
||||
private String GetKey(String key)
|
||||
{
|
||||
String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + "= \'" + key + "\';";
|
||||
ResultSet set = driver.ExecuteReturningStatement(command);
|
||||
String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + " = ?;";
|
||||
ResultSet set = driver.ExecuteReturningStatement(command, new String[] { key });
|
||||
return ResultAsString(set, "searchedKey");
|
||||
}
|
||||
|
||||
// Prototype for creating a key
|
||||
private void CreateKey(String key, String value)
|
||||
{
|
||||
String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES ('" + key + "', '" + value + "');";
|
||||
driver.ExecuteStatement(command);
|
||||
String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES (?, ?);";
|
||||
driver.ExecuteStatement(command, new String[] { key, value });
|
||||
}
|
||||
|
||||
// Transforms a result into a string if possible.
|
||||
|
||||
@@ -13,7 +13,8 @@ public abstract class JDBCDriver
|
||||
public abstract boolean Disconnect();
|
||||
|
||||
// Executes a SQL command with the database. Returns if it was executed successfully, or in the
|
||||
// case of the returning statement, returns the ResultSet.
|
||||
public abstract boolean ExecuteStatement(String statement);
|
||||
public abstract ResultSet ExecuteReturningStatement(String statement);
|
||||
// case of the returning statement, returns the ResultSet. Args are placed into the prepared statement
|
||||
// in place of each '?' places into it.
|
||||
public abstract boolean ExecuteStatement(String command, String[] args);
|
||||
public abstract ResultSet ExecuteReturningStatement(String command, String[] args);
|
||||
}
|
||||
|
||||
@@ -17,13 +17,13 @@ public class JDBCDriverMySQL extends JDBCDriver
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ExecuteStatement(String statement) {
|
||||
public boolean ExecuteStatement(String statement, String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet ExecuteReturningStatement(String statement) {
|
||||
public ResultSet ExecuteReturningStatement(String statement, String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -17,13 +17,13 @@ public class JDBCDriverPostgreSQL extends JDBCDriver
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ExecuteStatement(String statement) {
|
||||
public boolean ExecuteStatement(String statement, String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet ExecuteReturningStatement(String statement) {
|
||||
public ResultSet ExecuteReturningStatement(String statement, String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package network;
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
@@ -63,54 +64,86 @@ public class JDBCDriverSQLite extends JDBCDriver
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet ExecuteReturningStatement(String sql)
|
||||
public ResultSet ExecuteReturningStatement(String command, String[] args)
|
||||
{
|
||||
if(connection == null)
|
||||
return null;
|
||||
|
||||
if(sql == null || sql.length() == 0)
|
||||
if(command == null || command.length() == 0)
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
return set;
|
||||
if(args != null && args.length > 0)
|
||||
{
|
||||
PreparedStatement statement = connection.prepareStatement(command);
|
||||
|
||||
for(int i = 0; i < args.length; ++i)
|
||||
statement.setString(i + 1, args[i]);
|
||||
|
||||
ResultSet set = statement.executeQuery();
|
||||
return set;
|
||||
}
|
||||
else
|
||||
{
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet set = statement.executeQuery(command);
|
||||
return set;
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
try {
|
||||
try
|
||||
{
|
||||
GlobalLog.Fatal(e.getMessage());
|
||||
} catch (Exception e1) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
catch (Exception e1)
|
||||
{
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean ExecuteStatement(String sql)
|
||||
public boolean ExecuteStatement(String command, String[] args)
|
||||
{
|
||||
if(connection == null)
|
||||
return false;
|
||||
|
||||
if(sql == null || sql.length() == 0)
|
||||
if(command == null || command.length() == 0)
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
Statement statement = connection.createStatement();
|
||||
boolean executed = statement.execute(sql);
|
||||
return executed;
|
||||
if(args != null && args.length > 0)
|
||||
{
|
||||
PreparedStatement statement = connection.prepareStatement(command);
|
||||
|
||||
for(int i = 0; i < args.length; ++i)
|
||||
statement.setString(i + 1, args[i]);
|
||||
|
||||
boolean executed = statement.execute();
|
||||
return executed;
|
||||
}
|
||||
else
|
||||
{
|
||||
Statement statement = connection.createStatement();
|
||||
boolean executed = statement.execute(command);
|
||||
return executed;
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
try {
|
||||
try
|
||||
{
|
||||
GlobalLog.Fatal(e.getMessage());
|
||||
} catch (Exception e1) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
catch (Exception e1)
|
||||
{
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user