-2

I am having a problem with an MySQL Update query. Here is the code:

String s2="foo";
String s6="bar";

try{
Class.forName("com.mysql.jdbc.Driver");
Connection       con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
String query="update mydb set Status='"+s6+"' where IC_Number='"+s2+"'";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(query);
con.close();
}
catch(Exception e)
{
System.out.println("Error in login:"+e);
}
}
}

I get the error

can not issue data manipulation statements with executeQuery().

Can anyone point out where the problem is ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

0

For update operation you have to use executeUpdate which returns an integer but not a ResultSet

Change:

ResultSet rs=stmt.executeQuery(query);

To:

int updateResult = stmt.executeUpdate( query );

Documentation:

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
0

You should use executeUpdate() instead of executeQuery();

Shailesh Yadav
  • 1,061
  • 1
  • 15
  • 30