473,413 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

SQLException : No current connection? Why ?

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
9 10796
JosAH
11,448 Expert 8TB
@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
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
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
JosAH
11,448 Expert 8TB
System.out.println() is your friend: what is the value of 'con'?

kind regards,

Jos
Jun 14 '09 #5
@JosAH
con = (org.apache.derby.impl.jdbc.EmbedConnection40) #2003

Kr,
Jun 14 '09 #6
But I see a difference between con values, to comapre:

Jun 14 '09 #7
JosAH
11,448 Expert 8TB
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
@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
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

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

Similar topics

4
by: sumithradevi | last post by:
Hello Friends, I am getting the following error. java.sql.SQLException: ORA-00020: maximum number of processes (100) exceeded I am closing all my resultsets and all my connections in the try...
2
by: stanley J mroczek | last post by:
System.Data.SqlClient.SqlException: General network error. Check your network documentation. I know that this is not much to go on. But I don't know where to start. the program updates 186 records...
3
by: Rakesh | last post by:
Hi, I want to get connection to a DB2 database using the driver COM.ibm.db2.jdbc.DB2XADataSource. I have also included 'db2java.zip' in the classpath. However I am getting the exception ...
3
by: Mr.KisS | last post by:
Hello all, I'm working with : WinXP PRO SP1, MS SQL 2005 Express, Visual Web Dev 2005 Express. I have an aspx page which must execute a stored procedure : ______________ try {...
14
by: Marcus | last post by:
I have a function that simply returns TRUE if it can connect to a particular Sql Server 2005 express, or FALSE if it cannot. I am getting some strange error codes returned when the computer that...
7
by: bylum | last post by:
Servlet SQLException Communication link failure java howto i can't connect jsp and database(mysql). This is the exception: exception org.apache.jasper.JasperException:...
2
by: lunas | last post by:
hi i am trying to update a table selecting a value from another table and ve written the following codes for it.. import java.sql.*; import java.io.*; import java.util.*; public class...
8
by: ajos | last post by:
hi frnds, im trying to convert my servlets database configuration from ms access to mysql database.however im getting some error like no driver found exception. to verify this error ive...
1
by: R.A.M. | last post by:
Hello, I have started programming ASP.NET application using SQL Server 2005 Express Edition. The problem is that when C# code tries to access aspnetdb database, for example:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.