java.sql.SQLException: 关闭的连接

原因:试图使用已经关闭的Connection,Statement,ResultSet。

如果在rs.next()之前关闭了Statement或PreparedStatement,会导致下面的异常:

java.sql.SQLException: 关闭的语句: next

如果在rs.next()之前关闭了Connection,会导致下面的异常:

java.sql.SQLException: 关闭的连接: next问题

如果在rs.next()之前关闭了ResultSet,会导致下面的异常:

java.sql.SQLException: 关闭的 Resultset: next

java.sql.SQLException: 关闭的连接。是因为我的web应用中用了别人写的获得数据库连接代码工具类,他而写成了单例模式。

package ceit.gis.web.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class JdbcConnector {

        private static Connection connection = null;

        private static JdbcConnector instance = new JdbcConnector();

        private JdbcConnector() {
                reconnect();
        }

        public Connection getConnection() {
                try {
                        if (connection == null || connection.isClosed()) {
                                reconnect();
                        }
                } catch (SQLException e) {
                        e.printStackTrace();
                }
                return connection;
        }

        public void reconnect() {
                try {
                        Context ctx = new InitialContext();
                        DataSource ds = (DataSource) ctx.lookup("gwm");
                        connection = ds.getConnection();
                } catch (SQLException e) {
                        e.printStackTrace();
                } catch (NamingException e) {
                        e.printStackTrace();
                }
                /*
                 * try { String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String
                 * username = "gwm"; String password = "gwm";
                 * Class.forName("oracle.jdbc.driver.OracleDriver"); connection =
                 * DriverManager.getConnection(url, username, password); } catch
                 * (SQLException e) { e.printStackTrace(); }catch
                 * (ClassNotFoundException e) { e.printStackTrace(); }
                 */
        }

        public static JdbcConnector getInstance() {
                return instance;
        }
}

去掉单例模式,就行了。