gpt4 book ai didi

java - Android :java. sql.SQLException:结果集中没有当前行

转载 作者:行者123 更新时间:2023-12-01 17:17:30 25 4
gpt4 key购买 nike

当我尝试执行查询时,我收到以下错误,但我不知道为什么每次我尝试执行查询时都会出现以下错误。该错误也出现在我的 logcat 并没有像我预期的那样处于 toast 状态。

这是我的代码!!

  @Override
protected String doInBackground(String... strings) // Connect to the database, write query and add items to array list
{
runOnUiThread(new Runnable() {
public void run() {


try {
Connection conn = connectionClass.CONN(); //Connection Object
if (conn == null) {
success = false;
msg = "Sorry something went wrong,Please check your internet connection";
} else {
// Change below query according to your own database.
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy/mm/dd");
String formattedDate = df.format(c);
System.out.println("it isssssssssssssssssssssssssssssssssssssssssaaaaaaaaaaaaaaaaaaaa"+getIntent().getStringExtra("nameid"));
String query = "Insert into CustomerSupportChat values('" + formattedDate + "','" + themsg.getText().toString() + "','Customer','3','"+getIntent().getStringExtra("nameid")+"','1','1') " +
"Select MessageID,MessageDate,MessageText,SenderType,MessageRecieved,MessageReaded,Users_Login_Data.Username,StoresData.StoreEnglishName,StoresData.StoreArabicName FROM " +
"CustomerSupportChat INNER JOIN Users_Login_Data ON " +
"CustomerSupportChat.CustomerID = Users_Login_Data.CustomerID INNER JOIN StoresData ON " +
"CustomerSupportChat.StoreID = StoresData.StoreID";
String query2 =
"Select MessageID,MessageDate,MessageText,SenderType,MessageRecieved,MessageReaded,Users_Login_Data.Username,StoresData.StoreEnglishName,StoresData.StoreArabicName FROM " +
"CustomerSupportChat INNER JOIN Users_Login_Data ON " +
"CustomerSupportChat.CustomerID = Users_Login_Data.CustomerID INNER JOIN StoresData ON " +
"CustomerSupportChat.StoreID = StoresData.StoreID Where SenderType = 'Store'";

Statement stmt = conn.createStatement();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSet rs2 = stmt2.executeQuery(query2);


if (rs != null) // if resultset not null, I add items to itemArraylist using class created
{
while (rs.next()) {

try {
itemArrayList.add(new ClassListChat(rs.getString("MessageDate"), rs.getString("MessageText"), rs.getString("SenderType"), rs2.getString("MessageText")));
themsg.setText("");
} catch (Exception ex) {
ex.printStackTrace();
}
}

msg = "Found";
success = true;
} else {
msg = "No Data found!";
success = false;
}
}
} catch (Exception e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
Log.d("Error", writer.toString());
success = false;
}

}
});
return msg;
}

我尝试删除 while 并将其替换为 if 语句,但它显示了相同的错误。我还尝试了在 mssql 上的查询,它执行成功。

编辑我通过将日期格式更改为 MM/dd/yyyy 解决了这个问题。但现在我收到以下错误:

java.sql.SQLException: No current row in the ResultSet.

有什么想法吗?

最佳答案

看起来下面的行中没有对 rs2 进行空检查,因此可能会引发该错误:

itemArrayList.add(new ClassListChat(rs.getString("MessageDate"), rs.getString("MessageText"), rs.getString("SenderType"), rs2.getString("MessageText")));

检查 ResultSet 是否不返回任何内容的更好方法如下:

if (rs.next() == false){ 
msg = "No Data found!";
success = false;
} else {
do {
// add items to itemArraylist...
} while (rs.next());
}

了解更多:https://javarevisited.blogspot.com/2016/10/how-to-check-if-resultset-is-empty-in-Java-JDBC.html#ixzz6KIFZZHNB

关于java - Android :java. sql.SQLException:结果集中没有当前行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61353831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com