By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

SQLException : No current connection? Why ?

P: 17
Hi,

I have a database class I use to get and drop db connections:

Expand|Select|Wrap|Line Numbers
  1. public class Database {
  2.     protected static Connection c;    
  3.     //....
  4.    /// .. c = DriverManager.getConnection ....
  5.  
  6.     public Connection getConnection() {
  7.            return c;        
  8.     }
  9.     public void dropConnection() {
  10.         try {
  11.             c.close();
  12.         } catch (SQLException ex) {
  13.             System.err.println("SQLException: " + ex.getMessage());
  14.         }
  15.     }
  16. }

All other methodes like updatePlayer, insertPlayer, getAllPlayers .. work very good. Only in this selectPlayer I get an error message:
SQLException : No current connection.
Expand|Select|Wrap|Line Numbers
  1.  
  2.     ///...
  3.     private Statement stmt;
  4.     private Database db;
  5.     private Connection con;
  6.  
  7.     public Player selectPlayer(int id) {
  8.  
  9.         if (this.db == null) {
  10.             db = new Database();
  11.         }
  12.  
  13.         Player p = new Player();
  14.  
  15.         String sql =
  16.                 "SELECT playerID, name, team FROM tblPlayer WHERE " +
  17.                 "playerID= " + id + "";
  18.  
  19.  
  20.         try {
  21.             con = db.getConnection();
  22.             stmt = con.createStatement();
  23.             ResultSet rs = stmt.executeQuery(sql);
  24.  
  25.             while (rs.next()) {
  26.                 p.setPlayerID(rs.getInt("playerID"));
  27.                 p.setName(rs.getString("name"));
  28.                 p.setTeam(rs.getString("team"));
  29.             }
  30.         } catch (SQLException ex) {
  31.             System.err.println("SQLException : " + ex.getMessage());
  32.         }
  33.         try {
  34.             stmt.close();
  35.         } catch (SQLException ex) {
  36.             System.err.println("SQLException: " + ex.getMessage());
  37.         } finally {
  38.             db.dropConnection();
  39.         }
  40.         return p;
  41.     }
Can u help? thx in advance
Kr,
Jun 14 '09 #1
Share this Question
Share on Google+
9 Replies


Expert 10K+
P: 11,448
@freethinker
Somewhere in that code fragment setting up a connection fails and you probably muffle away any thrown exception. Were you able to load the driver?

kind regards,

Jos
Jun 14 '09 #2

P: 17
Hi, thanks.
Yes I am able to run the hole application to manage players, teams, matches, records, .. all things, all classes and all methodes except that one (selectPlayer) and all methodes to save data in all classes I use the same structure, and they all work perfect except that method in the class DataPlayer.

Kr,
Jun 14 '09 #3

P: 17
It is really bizarre, because when I debug, and get into these lines:
Expand|Select|Wrap|Line Numbers
  1. con = db.getConnection(); 
  2. stmt = con.createStatement(); 
  3. ResultSet rs = stmt.executeQuery(sql); 
con = db.getConnection(); => This works, I get into my database class, and the methode getConnection returns variable c without any problem.

And here:
stmt = con.createStatement(); ==> It throws exception wich i catch with System.out.println(), on the scree I read: SQLException : No current connection.
Jun 14 '09 #4

Expert 10K+
P: 11,448
System.out.println() is your friend: what is the value of 'con'?

kind regards,

Jos
Jun 14 '09 #5

P: 17
@JosAH
con = (org.apache.derby.impl.jdbc.EmbedConnection40) #2003

Kr,
Jun 14 '09 #6

P: 17
But I see a difference between con values, to comapre:

Jun 14 '09 #7

Expert 10K+
P: 11,448
Let's dissect this nasty little problem:

1) were you able to load your data from the database?
2) did you close that connection somewhere? (you should reset that connection to null after closing).

kind regards,

Jos
Jun 14 '09 #8

P: 17
@JosAH
Yes
2) did you close that connection somewhere?
Ofcourse I close it after every SQL statement in the finally block.

(you should reset that connection to null after closing).
You mean con= null and then con.close() ?
I don't have to do that for other methodes which

Thanks.
Jun 14 '09 #9

P: 17
I solved it, but with a bizarre solution :)
I just have to recall the constructor of DataPlayer, even after doing this in the constructor of UI class.

Example: (see line 10, 18 and 32)

Expand|Select|Wrap|Line Numbers
  1. public class PlayerPanel extends JPanel { 
  2.  
  3.     private ArrayList<Player> pList;
  4.     private DataPlayer dp;
  5.     private Player p;
  6.     private int identifier = 1;
  7.  
  8.     public PlayerPanel () {
  9.         setLayout(null);
  10.         dp = new DataPlayer();
  11.         p = new Player();
  12.  
  13.         viewPlayers();
  14.         selectPlayer();
  15.     }
  16.  
  17.     public void viewPlayers() {
  18.         dp = new DataPlayer(); // this is what I have to add
  19.         p = new Player();
  20.  
  21.         list.removeAll();
  22.         pList= new ArrayList<Player>();
  23.         pList = dp.selectAllPlayers();
  24.         for (Player player : pList) {
  25.             list.add(p.toString());
  26.         }
  27.  
  28.  
  29.     }
  30.  
  31.     public void selectPlayer() {
  32.        dp = new DataPlayer(); // this is what I have to add
  33.         /....
  34.  
  35.         p = dp.selectPlayer(identifier);
  36.  
  37.        /..
  38.     }
  39.  
  40.       }
Jun 14 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.