MYSQL error:com.mysql.jdbc.NotUpdatable
Asked Answered
E

2

7

I am getting this error

javax.servlet.ServletException: com.mysql.jdbc.NotUpdatable: Result Set not updatable.

I know this error is regarding the primary key but for all my tables I initially insert a primary key.So for this table also I have a primary key.I am posting part of my code.

Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery("Select * from test3 order by rand() limit 5");
List arrlist = new ArrayList();
while(rs.next()){
   String xa =rs.getString("display");
   if(xa.equals("1")){
      arrlist.add(rs.getString("question_text"));
   }
   rs.updateString("display", "0");
   rs.updateRow();

Just tell me if something is going wrong in this code.please help. This is my database

+----------------+---------------+------+-----+---------+----------------+
| Field          | Type          | Null | Key | Default | Extra          |
+----------------+---------------+------+-----+---------+----------------+
| id             | int(11)       | NO   | PRI | NULL    | auto_increment |
| index_question | varchar(45)   | YES  |     | NULL    |                |
| question_no    | varchar(10)   | YES  |     | NULL    |                |
| question_text  | varchar(1000) | YES  |     | NULL    |                |
| file_name      | varchar(128)  | YES  |     | NULL    |                |
| attachment     | mediumblob    | YES  |     | NULL    |                |
| display        | varchar(10)   | YES  |     | NULL    |                |
+----------------+---------------+------+-----+---------+----------------+
Electrokinetics answered 1/10, 2013 at 5:54 Comment(8)
You did not show how you allocated the statement (st). You must set it updatable in the createStatement call.Bertrambertrand
That is also there Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);Electrokinetics
Did you try adding the "FOR UPDATE" clause to the SELECT?Bertrambertrand
No,how do you do that?Electrokinetics
You read the MySQL Manual description of SELECT and look for "FOR UPDATE"Bertrambertrand
Thanks for the link @JimGarrison but I need to update the row later not along with the select statement.Electrokinetics
Then you are confused about how updates work. Either you update the row immediately after you have fetched it (FOR UPDATE and rs.updateRow(), OR you write an UPDATE tablename set ... where ... statement to update a row at any time.Bertrambertrand
For transactional integrity, and when you need to read the row before updating it, you should announce this intention by including FOR UPDATE on the SELECT. That blocks anyone else from changing the row -- which might lead to the UPDATE not doing what you expect.Konrad
E
1

You have to update the row immediately after you have fetched it (FOR UPDATE and rs.updateRow(),

OR

you have to write an UPDATE tablename set = where statement to update a row at any time

Eighty answered 5/6, 2015 at 9:37 Comment(0)
A
0

The query can not use functions. Try removing the "rand()" from the SQL query string.
See the JDBC 2.1 API Specification, section 5.6 for more details.

Anthropogeography answered 3/8, 2016 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.