Makdumul Islam
Makdumul Islam

Reputation: 21

ResultSet and String variable issue

This Program is retrieving data from a database. I need to use a string variable for my program. but when I Put ResultSet String value in the String Variable and its showing 'java.sql.SQLException: Before start of result set' after run the programm.

here is my code:

try{                
 // for database connection 

    Class.forName("com.mysql.jdbc.Driver");         
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/email_auto_responder","root","");

    Statement stmt=con.createStatement(); 
    ResultSet EmailAccounts = stmt.executeQuery("SELECT * FROM `clemailaccounts`");  

String hostSubtract = EmailAccounts.getString(2).substring(EmailAccounts.getString(2).indexOf("@"));
String partiallyCompanyName = hostSubtract.replace("@", "");
String companyName = partiallyCompanyName.replace(".com", "");          


   while(EmailAccounts.next()) {

  System.out.println(EmailAccounts.getInt(1)+"  "+EmailAccounts.getString(2)+"  "+ companyName +"  "+EmailAccounts.getString(4));
                + "\n");
        }
}
   catch(Exception e)
 { System.out.println(e);} 

I saw in some example they use resultset value in String variable. But I am not sure why I am getting this error. There is another thing in Eclips its also not showing any error. but after run the code I got this exception. any help will be appreciated. Thanks in Advance

Upvotes: 0

Views: 59

Answers (1)

Optional
Optional

Reputation: 4507

Your cursor is before the first row and then you are requesting the data. You need to move the cursor to the first row. Do EmailAccounts.next() first and then get String

Upvotes: 1

Related Questions