Basically what I want to do is tap into a database when the program starts and keep the connection open to run queries on it with said program. A simple tool, really. Here are the important functions:
public class MySQLConnection implements Serializable
{
private Connection _con;
GUI gui;
public void open()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
gui.cli("Driver loaded. . .");
String pass = new String(_pass);
String url = "jdbc:mysql://" + _host + ":3306/" + _name;
_con = DriverManager.getConnection(url, _user, pass);
gui.cli("Connection established to " + url + " . . .");
save();
}
catch(ClassNotFoundException e)
{
gui.cli(e.toString());
}
catch(SQLException e)
{
gui.cli(e.toString());
}
}
public void close()
{
if(_con != null)
{
try
{
_con.close();
gui.cli("Abolished connection to " + _host);
}
catch(SQLException e)
{
gui.cli(e.toString());
}
catch(Exception e)
{
gui.cli(e.toString());
}
}
}
}
There's obviously more to the class, like save, load, constructors, etc - again this is just the part that works with the Connection
It connects just fine, no problem, but when I call close(even if I call it immediately) the Connection is already null. If I take out the check I even get a Null Pointer exception, so it looks like _con is closing itself as soon as the function finishes. I didn't think this would happen since the variable's scope is at the class level.
Anyone familiar with this conundrum, know a way around this?

New Topic/Question
Reply




MultiQuote





|