Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MySQL-迭代结果集中的列_Java_Mysql_Loops_Resultset - Fatal编程技术网

Java MySQL-迭代结果集中的列

Java MySQL-迭代结果集中的列,java,mysql,loops,resultset,Java,Mysql,Loops,Resultset,我正在做一个项目,用户可以组装瑜伽课程的组件。它分布在几个文件中,因此太大,无法全部放在这里。我遇到的问题是在一个方法中,我需要迭代结果集的水平列,该结果集只从MySQL数据库返回一行 我知道我必须将光标定位在结果集的第一行(我相信我正在这样做)。因为我在结果集中只有一行(我的变量是rset),所以我应该只使用一次rset.next(),对吗?然后我应该能够使用一个简单的循环来迭代每一列,并将值附加到我的字符串生成器中。我想跳过第一列并附加每个后续值,直到循环到达具有空值的列。我找不到为什么我的

我正在做一个项目,用户可以组装瑜伽课程的组件。它分布在几个文件中,因此太大,无法全部放在这里。我遇到的问题是在一个方法中,我需要迭代结果集的水平列,该结果集只从MySQL数据库返回一行

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

有人能看出什么不对劲吗

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


在我看来,您正在两种方法之间使用
rset
。因此,当
countColumnsInTable
完成时,
rset
变量指向与以前不同的结果集,在
listPosesInSection
中。并且该结果集没有使用
next
进行升级,因此您会收到错误消息。您可能希望将其分配给该方法中的本地
ResultSet

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;
}

在我看来,您正在两种方法之间使用
rset
。因此,当
countColumnsInTable
完成时,
rset
变量指向与以前不同的结果集,在
listPosesInSection
中。并且该结果集没有使用
next
进行升级,因此您会收到错误消息。您可能希望将其分配给该方法中的本地
ResultSet

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;
}
试试这个

 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
    }
publicstringlistposesinsection(stringtablename,stringsectionname){
字符串strList;
StringBuilder strBuilderList=新StringBuilder(“”);
//运行查询
试一试{
statement=connection.createStatement();
//查询将从一个特定行收集所有列
rset=statement.executeQuery(“从“+tableName+”中选择*,其中“+tableName+”_name='“+sectionName+”);
while(rset.next()){
System.out.println(“列计数为”+countColumnsTable(tableName));
对于(inti=2;i试试这个

 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
    }
publicstringlistposesinsection(stringtablename,stringsectionname){
字符串strList;
StringBuilder strBuilderList=新StringBuilder(“”);
//运行查询
试一试{
statement=connection.createStatement();
//查询将从一个特定行收集所有列
rset=statement.executeQuery(“从“+tableName+”中选择*,其中“+tableName+”_name='“+sectionName+”);
while(rset.next()){
System.out.println(“列计数为”+countColumnsTable(tableName));

for(int i=2;irset是一个ResultSet对象

我认为您在ListPoseInsection和CountColumnSTable中使用了相同的ResultSet对象


因此,这里发生的事情是在ListPoseInsection中,rset保存一个结果,您也移动了光标,但在CountColumns中,您使用的是相同的rset,因此它会被覆盖,即它现在保存一个新结果,您得到的是列数,但由于它现在保存一个新结果,光标将位于第一条记录之前,因此使用不同的CountColumnSTable中的nt Resultset对象是Resultset对象

我认为您在ListPoseInsection和CountColumnSTable中使用了相同的ResultSet对象


因此,这里发生的事情是在ListPoseInsection中,rset保存一个结果,您也移动了光标,但在CountColumns中,您使用的是相同的rset,因此它会被覆盖,即它现在保存一个新结果,您得到的是列数,但由于它现在保存一个新结果,光标将位于第一条记录之前,因此使用不同的nt Resultset object in CountColumnsTable

这成功了!非常感谢!这成功了!非常感谢!谢谢!你们太棒了!谢谢!你们太棒了!感谢你们的帮助!感谢你们的帮助!