0

我正在做一个项目,用户可以在其中组装瑜伽课的组件。它分布在多个文件中,因此太大而无法全部放在这里。我遇到的麻烦是在一种方法中,我需要遍历结果集的水平列,该结果集仅从 MySQL 数据库返回 ONE ROW。

我知道我必须将光标放在结果集的第一行(我相信我正在这样做)。由于我在结果集中只有一行(我的变量是 rset),我应该只使用一次 rset.next(),对吗?然后我应该能够使用一个简单的循环来遍历每一列并将值附加到我的字符串生成器。我想跳过第一列并附加每个后续值,直到循环到达具有空值的列。我找不到为什么我的代码一直返回“结果集开始之前”异常。

任何人都可以发现任何问题吗?

我将发布该方法以及该方法调用的方法。(我在另一个问题中发布了这个,但我相信我的问题的性质已经改变,所以我用不同的标题重新发布这个。)

    // Query that returns the poses within a specific section
public String listPosesInSection(String tableName, String sectionName) {
    String strList;
    StringBuilder strBuilderList  = new StringBuilder("");
    // Run the query
    try {
        statement = connection.createStatement();
        // Query will collect all columns from one specific row
        rset = statement.executeQuery("SELECT * FROM " + tableName + " WHERE " + tableName + "_name = '" + sectionName + "'");
        rset.next();  
        System.out.println("Column count is " + countColumnsInTable(tableName));
        for (int i = 2; i <= countColumnsInTable(tableName); i++) {// First value (0) is always null, skip first column (1)
            System.out.println("test");
            strBuilderList.append(rset.getString(i) + "\n"); // This is line 126 as indicated in the exception message
            }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    strList = strBuilderList.toString();
    return strList.replaceAll(", $",""); // Strips off the trailing comma
}

// Method for getting the number of columns in a table using metadata
public int countColumnsInTable(String sectionType) {
    int count = 16;
    try {
        statement = connection.createStatement();
        rset = statement.executeQuery("SELECT * FROM " + sectionType);
        rsMetaData = rset.getMetaData();
        count = rsMetaData.getColumnCount();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return count;
}

这是异常消息的第一部分:

Column count is 26
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:855)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5773)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)
at YogaDatabaseAccess.listPosesInSection(YogaDatabaseAccess.java:126)
at YogaSectionDesigner$5.actionPerformed(YogaSectionDesigner.java:231)
4

3 回答 3

4

Looks to me like you're re-using the rset between your two methods. So when countColumnsInTable has completed, the rset variable is pointing to a different result set than it was before, in listPosesInSection. And that result set has not been advanced with next, hence the error message you're getting. You probably want to assign it to a local ResultSet within that method instead.

public int countColumnsInTable(String sectionType) {
    int count = 16;
    try {
        Statement statement = connection.createStatement();
        ResultSet rset = statement.executeQuery("SELECT * FROM " + sectionType);
        ResultSetMetaData rsMetaData = rset.getMetaData();
        count = rsMetaData.getColumnCount();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    // Remember to clean up
    return count;
}
于 2013-05-15T11:47:55.833 回答
1

尝试这个

 public String listPosesInSection(String tableName, String sectionName) {
        String strList;
        StringBuilder strBuilderList  = new StringBuilder("");
        // Run the query
        try {
            statement = connection.createStatement();
            // Query will collect all columns from one specific row
            rset = statement.executeQuery("SELECT * FROM " + tableName + " WHERE " + tableName + "_name = '" + sectionName + "'");
            while (rset.next()){
            System.out.println("Column count is " + countColumnsInTable(tableName));
            for (int i = 2; i <= countColumnsInTable(tableName); i++) {// First value (0) is always null, skip first column (1)
                System.out.println("test");
                strBuilderList.append(rset.getString(i) + "\n"); 
                }
             }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        strList = strBuilderList.toString();
        return strList.replaceAll(", $",""); // Strips off the trailing comma
    }
于 2013-05-15T11:49:46.887 回答
1

rset 是一个 ResultSet 对象

我认为您在 listPosesInSection 和 countColumnsInTable 中使用相同的 ResultSet 对象

所以这里发生的事情是在 listPosesInSection rset 中保存了一个结果,并且您还移动了光标,但是在 countColumnsInTable 中您再次使用了相同的 rset,因此它被覆盖了,即它现在保存了一个新结果,您将获得列数,但是因为它现在拥有一个新结果,光标将位于第一条记录之前,因此在 countColumnsInTable 中使用不同的结果集对象

于 2013-05-15T11:58:29.400 回答