java sql 结果_java.sql.SQLException:在结果集开始之前.结果集循环中的查询如何?...

参见英文答案 > ResultSet exception – before start of result set                                    6个

我正在尝试在结果集循环中创建一个查询,但我不断收到错误“在结果集开始之前”.我尝试了很多不同的方法,但他们不断提出相同的错误.

有人可以帮帮我吗?

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,";

}

解决方法:

A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

您需要先调用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...

}

标签:java,mysql,jdbc

来源: https://codeday.me/bug/20190725/1534563.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值