使用循环插入值

时间:2013-08-14 05:06:01

标签: java database resultset

我有一个包含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

5 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

您需要创建一个新的statement(一个单独的变量)来创建一个表。由于结果集是cursor over语句对象,因此在同一对象上执行另一个statement后它将变为无效。

答案 2 :(得分:1)

这违反了Statement类的期望。

  

默认情况下,每个Statement对象只能打开一个ResultSet对象   同时。因此,如果读取一个ResultSet对象是   与另一个的读取交错,每个必须已经生成   通过不同的Statement对象。 Statement中的所有执行方法   如果是,接口隐式关闭一个statment的当前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();
        }
    }
}

答案 3 :(得分: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);
    }
}

希望有所帮助

答案 4 :(得分:-1)

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

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