2

I have a table like this

Signup table

Login_id   Address  User_type  state 

    1      Ansari     s        Alaska
    2      Rajesh     b        Rameshwaram

String retrieve_query="select User_type from Signup where State="Alaska";                           
ResultSet rs1=st.executeQuery(retrieve_query);

String User_type=rs1.getString("User_type");//I expect "S" to be the User_type

But i am getting the following exception

Exception occured java.sql.SQLException: Operation not allowed after ResultSet closed

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
VijayIndia
  • 91
  • 1
  • 7
  • Execute query return a array from db you must fetch it and iterate – Laurentiu Nov 13 '15 at 06:53
  • @Laurentiu i am using the condition directly in my select statement ,so i don't wish to iterate.I want the exact ResultSet rs1 without iterating .Can you suggest – VijayIndia Nov 13 '15 at 06:56
  • I'm assuming `String retrieve_query="select User_type from Signup where State="Alaska"; ` is not the actual SQL, since it won't pass compilation. – Eran Nov 13 '15 at 06:56
  • 1
    @VijayIndia As mentioned in my answer, you are missing `rs1.next()`. – Eran Nov 13 '15 at 06:57
  • Yes, check here http://stackoverflow.com/a/21476846/3270074 – Laurentiu Nov 13 '15 at 07:00

3 Answers3

2

You're missing rs1.next().

String User_type=null;
if (rs1.next()) 
    User_type = rs1.getString("User_type");
Eran
  • 387,369
  • 54
  • 702
  • 768
0

You should try to use prepared statement like this:

PreparedStatement ps;
ResultSet rs;
String user_type=null;
ps = con.prepareStatement("select User_type from Signup where State='?'");
ps.setString(1, "Alaska");
rs = ps.executeQuery();
if (rs.next()) 
   user_type = rs.getString("User_type");
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Try replace you're query with:

String retrieve_query="select User_type from Signup where State='Alaska'";
Mike
  • 137
  • 1
  • 13