Hi,
I have a database class I use to get and drop db connections:
All other methodes like updatePlayer, insertPlayer, getAllPlayers .. work very good. Only in this selectPlayer I get an error message:
SQLException : No current connection.
Can u help? thx in advance
Kr,
I have a database class I use to get and drop db connections:
Code:
public class Database {
protected static Connection c;
//....
/// .. c = DriverManager.getConnection ....
public Connection getConnection() {
return c;
}
public void dropConnection() {
try {
c.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
All other methodes like updatePlayer, insertPlayer, getAllPlayers .. work very good. Only in this selectPlayer I get an error message:
SQLException : No current connection.
Code:
///...
private Statement stmt;
private Database db;
private Connection con;
public Player selectPlayer(int id) {
if (this.db == null) {
db = new Database();
}
Player p = new Player();
String sql =
"SELECT playerID, name, team FROM tblPlayer WHERE " +
"playerID= " + id + "";
try {
con = db.getConnection();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
p.setPlayerID(rs.getInt("playerID"));
p.setName(rs.getString("name"));
p.setTeam(rs.getString("team"));
}
} catch (SQLException ex) {
System.err.println("SQLException : " + ex.getMessage());
}
try {
stmt.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
} finally {
db.dropConnection();
}
return p;
}
Kr,
Comment