+ Added prepared statements

This commit is contained in:
Matthew Cech
2019-05-18 00:13:56 -07:00
parent 03f6ffb3e8
commit e216e2e60b
5 changed files with 66 additions and 32 deletions
+9 -9
View File
@@ -35,7 +35,7 @@ public class DatabaseDriver
driver.Connect(); driver.Connect();
// Require a global table if it doesn't exist already // 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; return true;
} }
@@ -75,15 +75,15 @@ public class DatabaseDriver
// Prototype formatting for key updating // Prototype formatting for key updating
private void UpdateKey(String key, String value) private void UpdateKey(String key, String value)
{ {
String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = '" + value + "' WHERE " + globalKeyName + "= '" + key + "';"; String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = ? WHERE " + globalKeyName + " = ?;";
driver.ExecuteStatement(command); driver.ExecuteStatement(command, new String[] { key, value });
} }
// Protoype updating for seeing if a key exists // Protoype updating for seeing if a key exists
private boolean HasKey(String key) private boolean HasKey(String key)
{ {
String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = '" + key + "';"; String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = ?;";
ResultSet set = driver.ExecuteReturningStatement(command); ResultSet set = driver.ExecuteReturningStatement(command, new String[] { key });
String out = ResultAsString(set, "count"); String out = ResultAsString(set, "count");
return out.charAt(0) == '1'; return out.charAt(0) == '1';
} }
@@ -91,16 +91,16 @@ public class DatabaseDriver
// Prototype for getting a key // Prototype for getting a key
private String GetKey(String key) private String GetKey(String key)
{ {
String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + "= \'" + key + "\';"; String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + " = ?;";
ResultSet set = driver.ExecuteReturningStatement(command); ResultSet set = driver.ExecuteReturningStatement(command, new String[] { key });
return ResultAsString(set, "searchedKey"); return ResultAsString(set, "searchedKey");
} }
// Prototype for creating a key // Prototype for creating a key
private void CreateKey(String key, String value) private void CreateKey(String key, String value)
{ {
String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES ('" + key + "', '" + value + "');"; String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES (?, ?);";
driver.ExecuteStatement(command); driver.ExecuteStatement(command, new String[] { key, value });
} }
// Transforms a result into a string if possible. // Transforms a result into a string if possible.
+4 -3
View File
@@ -13,7 +13,8 @@ public abstract class JDBCDriver
public abstract boolean Disconnect(); public abstract boolean Disconnect();
// Executes a SQL command with the database. Returns if it was executed successfully, or in the // Executes a SQL command with the database. Returns if it was executed successfully, or in the
// case of the returning statement, returns the ResultSet. // case of the returning statement, returns the ResultSet. Args are placed into the prepared statement
public abstract boolean ExecuteStatement(String statement); // in place of each '?' places into it.
public abstract ResultSet ExecuteReturningStatement(String statement); public abstract boolean ExecuteStatement(String command, String[] args);
public abstract ResultSet ExecuteReturningStatement(String command, String[] args);
} }
+2 -2
View File
@@ -17,13 +17,13 @@ public class JDBCDriverMySQL extends JDBCDriver
} }
@Override @Override
public boolean ExecuteStatement(String statement) { public boolean ExecuteStatement(String statement, String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return false; return false;
} }
@Override @Override
public ResultSet ExecuteReturningStatement(String statement) { public ResultSet ExecuteReturningStatement(String statement, String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
+2 -2
View File
@@ -17,13 +17,13 @@ public class JDBCDriverPostgreSQL extends JDBCDriver
} }
@Override @Override
public boolean ExecuteStatement(String statement) { public boolean ExecuteStatement(String statement, String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return false; return false;
} }
@Override @Override
public ResultSet ExecuteReturningStatement(String statement) { public ResultSet ExecuteReturningStatement(String statement, String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
+47 -14
View File
@@ -3,6 +3,7 @@ package network;
import java.io.File; import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
@@ -63,54 +64,86 @@ public class JDBCDriverSQLite extends JDBCDriver
} }
@Override @Override
public ResultSet ExecuteReturningStatement(String sql) public ResultSet ExecuteReturningStatement(String command, String[] args)
{ {
if(connection == null) if(connection == null)
return null; return null;
if(sql == null || sql.length() == 0) if(command == null || command.length() == 0)
return null; return null;
try try
{ {
Statement statement = connection.createStatement(); if(args != null && args.length > 0)
ResultSet set = statement.executeQuery(sql); {
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; return set;
} }
else
{
Statement statement = connection.createStatement();
ResultSet set = statement.executeQuery(command);
return set;
}
}
catch (SQLException e) catch (SQLException e)
{ {
try { try
{
GlobalLog.Fatal(e.getMessage()); GlobalLog.Fatal(e.getMessage());
} catch (Exception e1) { }
// TODO Auto-generated catch block catch (Exception e1)
{
e1.printStackTrace(); e1.printStackTrace();
} }
return null; return null;
} }
} }
public boolean ExecuteStatement(String sql) public boolean ExecuteStatement(String command, String[] args)
{ {
if(connection == null) if(connection == null)
return false; return false;
if(sql == null || sql.length() == 0) if(command == null || command.length() == 0)
return false; return false;
try try
{ {
Statement statement = connection.createStatement(); if(args != null && args.length > 0)
boolean executed = statement.execute(sql); {
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; return executed;
} }
else
{
Statement statement = connection.createStatement();
boolean executed = statement.execute(command);
return executed;
}
}
catch (SQLException e) catch (SQLException e)
{ {
try { try
{
GlobalLog.Fatal(e.getMessage()); GlobalLog.Fatal(e.getMessage());
} catch (Exception e1) { }
// TODO Auto-generated catch block catch (Exception e1)
{
e1.printStackTrace(); e1.printStackTrace();
} }
return false; return false;
} }
} }