Error in updating records using JDBC

170 Views Asked by At

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 ?

2

There are 2 best solutions below

0
On BEST ANSWER

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:

0
On

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