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();

}

}

[3663 byte] By [Valleriania] at [2007-10-2 9:13:03]
# 1
Hi,You should instead make the connection an attribute of the HawkBot class. The connect method should set that attribute, and the shutdown method should close connection, and set the attribute to null.Kaj
kajbja at 2007-7-16 23:20:06 > top of Java-index,Java Essentials,New To Java...
# 2
I don't really understand you fully, is there a example floating around? Thanks
Valleriania at 2007-7-16 23:20:06 > top of Java-index,Java Essentials,New To Java...
# 3

I changed some of the coding, now I get one error for changing the table.

HawkBot.java:113: cannot find symbol

symbol : variable conn

location: class HawkBot

PreparedStatement stmt = conn.prepareStatement("UPDATE NEXUS_PLA

YERS SET CREDITS = ? WHERE username = ?");

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.sql.PreparedStatement;

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 = "*******";

private String tableName = "Nexus_Players";

// jdbc Connection

String Admin = "NiteHawk";

public HawkBot() {

this.setName("HawkBot");

}

//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(username, userinput); //ALSO WRONG

sendMessage(channel, sender + ": Changed " + username + "'s score to " + userinput + ".");

shutdown();

}

else {

sendMessage(channel, sender + ": Connection failed.");

connecting = false;

}

}

//END OF CHANGE SCORE

}

}

}

}

//End of on message

//Shutdown Connection

private void shutdown()

{

accepted = false;

try

{

accepted = true;

Connection conn = DriverManager.getConnection(dbURL + ";shutdown=true");

conn.close();

conn = null;

}

catch (Exception except)

{

except.printStackTrace();

}

}

//End of shutdown

//Create a connection when needed

Connection connect()

{

try

{

Class.forName(driver);

//Get a connection

Connection conn = DriverManager.getConnection(dbURL);

accepted = true;

return conn;

}

catch (Exception except)

{

except.printStackTrace();

accepted = false;

}

}

//End of create connection

private void changescore(String username, String score)

{

try

{

PreparedStatement stmt = conn.prepareStatement("UPDATE NEXUS_PLAYERS SET CREDITS = ? WHERE username = ?");

stmt.setString(1, score);

stmt.setString(2, username);

stmt.executeUpdate();

stmt.close();

}

catch (Exception except)

{

except.printStackTrace();

}

}

}

Like for the shutdown Connection conn = DriverManager.getConnection(dbURL + ";shutdown=true"); is declared, what would it be fore updating and changing in the tables?

Valleriania at 2007-7-16 23:20:06 > top of Java-index,Java Essentials,New To Java...
# 4

An example:

public class HawkBot {

private Connection conn;

public void shutdown() {

if (conn == null) {

return;

}

try {

conn.close();

} catch (Exception ignore) {

//Do nothing

}

}

public void connect() {

try {

conn = DriverManager.getConnection(dbURL);

} catch (Exception e) {

e.printStackTrace();

}

}

}

Kaj

kajbja at 2007-7-16 23:20:06 > top of Java-index,Java Essentials,New To Java...
# 5
I heard though it's not proper to just declareprivate Connection conn; in the class, and should do it so it calls when needed?
Valleriania at 2007-7-16 23:20:06 > top of Java-index,Java Essentials,New To Java...
# 6

> I heard though it's not proper to just declare

> private Connection conn; in the class, and should

> uld do it so it calls when needed?

Hi,

I'm not sure that I understand your question. What do you mean?

It all depends on what your application looks like etc. More advanced applications usually have a database layer so that normal classes doesn't do database related stuff, and they do also use connection pools.

Kaj

kajbja at 2007-7-16 23:20:06 > top of Java-index,Java Essentials,New To Java...