SQLException : No current connection? Why ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • freethinker
    New Member
    • May 2009
    • 17

    SQLException : No current connection? Why ?

    Hi,

    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;
        }
    Can u help? thx in advance
    Kr,
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by freethinker
    Code:
    public class Database {
        protected static Connection c;    
        //....
       /// .. c = DriverManager.getConnection ....
    All other methodes like updatePlayer, insertPlayer, getAllPlayers .. work very good. Only in this selectPlayer I get an error message:
    SQLException : No current connection.
    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

    Comment

    • freethinker
      New Member
      • May 2009
      • 17

      #3
      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,

      Comment

      • freethinker
        New Member
        • May 2009
        • 17

        #4
        It is really bizarre, because when I debug, and get into these lines:
        Code:
        con = db.getConnection(); 
        stmt = con.createStatement(); 
        ResultSet rs = stmt.executeQuery(sql);
        con = db.getConnectio n(); => This works, I get into my database class, and the methode getConnection returns variable c without any problem.

        And here:
        stmt = con.createState ment(); ==> It throws exception wich i catch with System.out.prin tln(), on the scree I read: SQLException : No current connection.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          System.out.prin tln() is your friend: what is the value of 'con'?

          kind regards,

          Jos

          Comment

          • freethinker
            New Member
            • May 2009
            • 17

            #6
            Originally posted by JosAH
            what is the value of 'con'?
            con = (org.apache.der by.impl.jdbc.Em bedConnection40 ) #2003

            Kr,

            Comment

            • freethinker
              New Member
              • May 2009
              • 17

              #7
              But I see a difference between con values, to comapre:

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                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

                Comment

                • freethinker
                  New Member
                  • May 2009
                  • 17

                  #9
                  Originally posted by JosAH
                  Let's dissect this nasty little problem:

                  1) were you able to load your data from the database?
                  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.

                  Comment

                  • freethinker
                    New Member
                    • May 2009
                    • 17

                    #10
                    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)

                    Code:
                    public class PlayerPanel extends JPanel { 
                    
                        private ArrayList<Player> pList;
                        private DataPlayer dp;
                        private Player p;
                        private int identifier = 1;
                    
                        public PlayerPanel () {
                            setLayout(null);
                            dp = new DataPlayer();
                            p = new Player();
                    
                            viewPlayers();
                            selectPlayer();
                        }
                    
                        public void viewPlayers() {
                            [B]dp = new DataPlayer();[/B] // [U]this is what I have to add[/U]
                            p = new Player();
                    
                            list.removeAll();
                            pList= new ArrayList<Player>();
                            pList = dp.selectAllPlayers();
                            for (Player player : pList) {
                                list.add(p.toString());
                            }
                    
                    
                        }
                    
                        public void selectPlayer() {
                           [B]dp = new DataPlayer();[/B] // [U]this is what I have to add[/U]
                            /....
                    
                            p = dp.selectPlayer(identifier);
                    
                           /..
                        }
                    
                          }
                    }

                    Comment

                    Working...