3

I'm trying to create a query within a results set loop but I keep getting the error "Before start of result set". I've attempted many different methods but they keep coming up with the same error.

Can someone help me out here?

String insertSQL = "INSERT INTO MonthlyReportTable VALUES(NULL,"; //Primary Key.

String PlannetSchemeCode = "";
int ResponcibleAuthorityID = 0;

Statement stmt = ConnectionDetails.getNewConnectionPPARSDB().createStatement();
ResultSet resultsSet = stmt.executeQuery("SELECT * FROM planning_scheme");

Statement insideStatement = ConnectionDetails.getNewConnectionPPARSDB().createStatement();

//Loop though each planning scheme and create the data for each field.
while (resultsSet.next()) 
{
    PlannetSchemeCode = "'" + resultsSet.getString("ps_code") + "'";

    //Planning Scheme Primary Key
    insertSQL += PlannetSchemeCode + ",";

    /*
    //Responsible Authority ID
    insertSQL += "'" + String.valueOf(
            ResponcibleAuthorityID = MySQLUtil.getResults(
                ConnectionDetails.Database_Connection_PPARSDB, 
                "SELECT resp_authority_id " +
                "FROM resp_authority_to_ps " +
                "WHERE ps_code = " + PlannetSchemeCode
            )
            .getInt("resp_authority_id")
        ) + "'";
    */

    ResultSet insideResultsSet = 
            insideStatement.executeQuery(
                "SELECT resp_authority_id " +
                "FROM resp_authority_to_ps " +
                "WHERE ps_code = " + PlannetSchemeCode
            );

    //ERROR HERE, some reason results set is getting set wrong??
    //Error here, this current results set is resetting the Results set.
    ResponcibleAuthorityID = insideResultsSet.getInt("resp_authority_id");

    //Total_Received_CM

    //Add the rest of the values temporary.
    int FeildsAdded = 3;
    for(int i = 1 + FeildsAdded; i < 458; i++)
    {
        insertSQL += String.valueOf(0) + ",";
    }

    //Insert date and end SQL string.
    insertSQL += "NOW()";
    insertSQL += ")";

    System.out.println(insertSQL);

    //Do Insert in PPARS.
    //stmt.executeQuery(insertSQL);
    //Reset the SQL String for the new Row.
    insertSQL = "INSERT INTO MonthlyReportTable VALUES(NULL,";
}
4

1 に答える 1

8

ResultSetカーソルは最初、最初の行の前に配置されます。メソッドの最初の呼び出しでnextは、最初の行が現在の行になります。2 番目の呼び出しでは、2 番目の行が現在の行になります。

ResultSet#next()返されたデータを読み取る前に呼び出す必要があります。

ResultSet insideResultsSet = insideStatement.executeQuery(
    "SELECT resp_authority_id " +
    "FROM resp_authority_to_ps " +
    "WHERE ps_code = " + PlannetSchemeCode
);

if (insideResultsSet.next()) {
    ResponcibleAuthorityID = insideResultsSet.getInt("resp_authority_id");

    // etc...
}
于 2013-05-07T21:40:21.683 に答える