Need help with connections. SQL/Derby?
I need to beable to return the CONN string, so that the private void shutdown and changescore can actully do what they need. Someone told me about STRING URL, but I haven't a clue how to use it. Here is the coding. So yeah, it needs to beable to get the conn string from the connect() so that it can actully detect if its used and shut down the connection..
Oh and also is the updating statements in CHANGESCORE correct? THanks
import org.jibble.pircbot.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class HawkBot extends PircBot {
public Boolean accepted = false;
public Boolean connecting = false;
private String driver = "org.apache.derby.jdbc.ClientDriver";
private String dbURL = "******hidden";
private String tableName = "Nexus_Players";
// jdbc Connection
String Admin = "NiteHawk";
public HawkBot() {
this.setName("HawkBot");
Class.forName(driver);
}
//Begin on messages
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
if (message.equalsIgnoreCase(".time")) {
String time = new java.util.Date().toString();
sendMessage(channel, sender + ": The time is now " + time);
}
if (sender.equalsIgnoreCase(Admin)) {
if (message.equalsIgnoreCase(".quit")) {
quitServer("Quit command issued by: " + sender );
System.exit(0);
}
if (message.startsWith(".change")) {
if (connecting.equals(true)) {
sendMessage(channel, sender + ": You are already connecting, or your connected!");
}
else {
String[] splitUpString = message.split(" "); // split someString up by " "
String usercommand = splitUpString[1];
String username = splitUpString[2];
String userinput = splitUpString[3];
//Change Score
if (usercommand.equals("score"))
{
connecting = true;
connect();
if (accepted.equals(true)) {
changescore(conn, username, userinput);
sendMessage(channel, sender + ": Changed " + username + "'s score to " + userinput + ".");
shutdown(conn);
}
else {
sendMessage(channel, sender + ": Connection failed.");
connecting = false;
}
}
//END OF CHANGE SCORE
}
}
}
}
//End of on message
//Shutdown Connection
private void shutdown(String conn)
{
accepted = false;
try
{
if (conn != null)
{
accepted = true;
DriverManager.getConnection(dbURL + ";shutdown=true");
conn.close();
conn = null;
}
}
catch (Exception except)
{
except.printStackTrace();
}
}
//End of shutdown
//Create a connection when needed
private void connect()
{
try
{
//Get a connection
Connection conn = DriverManager.getConnection(dbURL);
accepted = true;
Connection conn(String url);
return conn;
}
catch (Exception except)
{
except.printStackTrace();
accepted = false;
}
}
//End of create connection
private void changescore(String conn, String username, String score)
{
PreparedStatement stmt = conn.prepareStatement("UPDATE NEXUS_PLAYERS SET CREDITS = ? WHERE username = ?");
stmt.setInt(1, score);
stmt.setString(2, username);
stmt.update();
stmt.close();
}
}

