2

我有一个包含 54 个值的数据库列。我想为每个值创建一个表。但是我的代码只创建第一个值表,然后由于关闭而显示错误ResultSet

如何重新打开ResultSet使用循环?我需要所有 54 个有价值的表。

代码:

public void createTableStudent() {
    try {
        connection = (Connection) dbSource.getConnection();

        String qry = "SELECT studentName From batcha "; 
        System.out.println(qry);

        stmt = (PreparedStatement)  connection.prepareStatement(qry);
            rs =  stmt.executeQuery();

            while (rs.next()) {

                String check=new String(rs.getString(("studentName")));
                String student = check.replaceAll("\\s","");

                Class.forName("com.mysql.jdbc.Driver");
                connection = (Connection) DriverManager.getConnection(DB_URL_table, USER, PASS);

                String sql1 = "CREATE TABLE IF NOT EXISTS "+student+" " +
                    "(id INTEGER not NULL AUTO_INCREMENT, " +
                    " fcltyName VARCHAR(255), " + 

                    "CommunicationOral INTEGER, "+
                    "Communicationwritten INTEGER, "+
                    "Leadership INTEGER, "+
                    "AnalyticalAbilities INTEGER, "+
                    "Interpersonalskills INTEGER, "+
                    "DecisionMakingSkills INTEGER, "+
                    "SelfConfidence INTEGER, "+
                    "Creativity INTEGER, "+
                    "Punctualityregularity INTEGER, "+
                    "GeneralAwareness INTEGER, "+
                    "Commitment INTEGER, "+
                    "Hardwork INTEGER, "+

                    " PRIMARY KEY ( id ))"; 
            stmt.executeUpdate(sql1); 

                        }



    } catch (ClassNotFoundException ex) {
        Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
    }
}

它显示以下错误

 java.sql.SQLException: Operation not allowed after ResultSet closed
4

5 回答 5

5

您正在为两个查询重复使用相同的语句。不要那样做;相反,为每个查询创建一个新的 Statement 对象。

于 2013-08-14T05:36:39.997 回答
1

这违反了Statement类的期望。

默认情况下,每个 Statement 对象只能同时打开一个 ResultSet 对象。因此,如果一个 ResultSet 对象的读取与另一个 ResultSet 对象的读取交错,则每个对象都必须由不同的 Statement 对象生成。Statement 接口中的所有执行方法都会隐式关闭语句的当前 ResultSet 对象(如果存在打开的对象)。

因此,您需要为创建Statement表创建一个新的。

public void createTableStudent() 
{
    try {
        connection = (Connection) dbSource.getConnection();

        String qry = "SELECT studentName From batcha "; 
        System.out.println(qry);

        stmt = (PreparedStatement)  connection.prepareStatement(qry);
            rs =  stmt.executeQuery();

            while (rs.next()) 
            {

                Class.forName("com.mysql.jdbc.Driver");
                connection = (Connection) DriverManager.getConnection(DB_URL_table, USER, PASS);

                Statement newStmt = connection.createStatement();

                try {
                        String check=new String(rs.getString(("studentName")));
                        String student = check.replaceAll("\\s","");



                        String sql1 = "CREATE TABLE IF NOT EXISTS "+student+" " +
                            "(id INTEGER not NULL AUTO_INCREMENT, " +
                            " fcltyName VARCHAR(255), " + 

                            "CommunicationOral INTEGER, "+
                            "Communicationwritten INTEGER, "+
                            "Leadership INTEGER, "+
                            "AnalyticalAbilities INTEGER, "+
                            "Interpersonalskills INTEGER, "+
                            "DecisionMakingSkills INTEGER, "+
                            "SelfConfidence INTEGER, "+
                            "Creativity INTEGER, "+
                            "Punctualityregularity INTEGER, "+
                            "GeneralAwareness INTEGER, "+
                            "Commitment INTEGER, "+
                            "Hardwork INTEGER, "+

                            " PRIMARY KEY ( id ))"; 

                        newStmt.executeUpdate(sql1); 
                    }

                finally
                {
                    try 
                    {
                        if(newStmt!=null)
                            newStmt.close();
                    } 
                    catch (SQLException e) 
                    {
                        e.printStackTrace();
                    }
                }

            }



    } catch (ClassNotFoundException ex) {
        Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
    }
    finally
    {
        try {
                if(rs!=null)
                    rs.close();
                if(stmt!=null)
                    stmt.close();
                if(connection!=null)
                    connection.close();                     
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
    }
}
于 2013-08-14T05:16:26.100 回答
1

您需要创建一个新statement的(单独的变量)来创建一个表。由于结果集是一个cursor过度语句对象,一旦您statement在同一对象上执行另一个,它就会变得无效。

于 2013-08-14T05:13:26.883 回答
1

我认为您需要在 while 循环中创建一个新语句

因为您正在执行一个语句并在 while 循环内用另一个查询覆盖一个语句

这就是为什么只有第一个值正在执行并保持覆盖。

下面是修改后的代码。

public void createTableStudent() {
    try {
        connection = (Connection) dbSource.getConnection();

        String qry = "SELECT studentName From batcha "; 
        System.out.println(qry);

        stmt = (PreparedStatement)  connection.prepareStatement(qry);
            rs =  stmt.executeQuery();

            while (rs.next()) {



                String check=new String(rs.getString(("studentName")));
                String student = check.replaceAll("\\s","");

                Class.forName("com.mysql.jdbc.Driver");
                connection = (Connection) DriverManager.getConnection(DB_URL_table, USER, PASS);
                Statement statement2 = connection.createStatement();

                String sql1 = "CREATE TABLE IF NOT EXISTS "+student+" " +
                    "(id INTEGER not NULL AUTO_INCREMENT, " +
                    " fcltyName VARCHAR(255), " + 

                    "CommunicationOral INTEGER, "+
                    "Communicationwritten INTEGER, "+
                    "Leadership INTEGER, "+
                    "AnalyticalAbilities INTEGER, "+
                    "Interpersonalskills INTEGER, "+
                    "DecisionMakingSkills INTEGER, "+
                    "SelfConfidence INTEGER, "+
                    "Creativity INTEGER, "+
                    "Punctualityregularity INTEGER, "+
                    "GeneralAwareness INTEGER, "+
                    "Commitment INTEGER, "+
                    "Hardwork INTEGER, "+

                    " PRIMARY KEY ( id ))"; 
                statement2.executeUpdate(sql1); 

                        }



    } catch (ClassNotFoundException ex) {
        Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
    }
}

希望能帮助到你

于 2013-08-14T05:58:36.147 回答
-1

我记得读过我们应该避免使用“如果不存在则创建表”。或者可以使用 java.sql.DatabaseMetaData 来检查表是否存在。下面的链接解释了这一点。

http://somesimplethings.blogspot.co.uk/2010/03/derby-create-table-if-not-exists.html

于 2013-08-14T05:41:50.257 回答