Skip to content
Advertisement

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

I am getting the following error when i try to execute my query(s),but i don’t know why it gives me the following error every time i try to execute my query(s).The error also appears in my logcat and not in a toast as i expected.

here is my code!!

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

I have tried removing while and replacing it with if statement but it showed me the same error. I also tried my query on mssql and it executed successfully.

EDIT I solved it by changing the date format to MM/dd/yyyy. But now i get the following error:

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

Any ideas?

Advertisement

Answer

Looks like there is no null check for rs2 in the below line so could throw that error:

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

Also a better way to check for ResultSet not returning anything is like this:

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

Read more: https://javarevisited.blogspot.com/2016/10/how-to-check-if-resultset-is-empty-in-Java-JDBC.html#ixzz6KIFZZHNB

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement