Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updating a record in database using a save button

 
Rodney Ibanez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help.. I need to update my table with the values I get from a textbox. Each time I fire the save button, it gives me an error like so.
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:499)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1518)
at GradingSystem.DetailedClass$3.actionPerformed(DetailedClass.java:139)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Here is my code...
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 121 in the code sample above you're executing an update statement by invoking Statement's executeQuery() method, which is not the appropriate method for DML / DDL queries. You need to use executeUpdate() instead.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also several other issues with your code, not the least of which is a massive security hole that makes the application vulnerable to SQL injection. You should have a look at the PreparedStatement (<- click) class.
 
Rodney Ibanez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:There are also several other issues with your code, not the least of which is a massive security hole that makes the application vulnerable to SQL injection. You should have a look at the PreparedStatement (<- click) class.

Thank you for that insight. So that's what the PreparedStatement is for! I was thingking, why do I need to use a PreparedStatement when it is easier to use CreateStatement. But now I know. Thank you. I am learning Java on my own so I was not informed of this. But I got the issue solved too I used

it got my code working. But now I guess I'll practice using the PreparedStatement. I've got another question though.. is there a way that I can like refresh my database so that the data i get on the textfield on another frame can be updated to the new values that I have just input on the other frame (Class DetailedClass). Thanks!
 
Martin Vajsar
Sheriff
Pie
Posts: 3751
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rodney Ibanez wrote:I was thingking, why do I need to use a PreparedStatement when it is easier to use CreateStatement.

Using PreparedStatement is actually the easier approach. Without it, you have to quote string parameters and convert dates to formats recognized by your database, and every database has it's own ideas about how these things should be done (single quote or double quote? mm/dd/yyyy or dd/mm/yyyy? and so on).

PreparedStatements spares you of this. Plus it's safe. Plus in many databases it performs significantly better. It's a win-win-win.

I've got another question though.. is there a way that I can like refresh my database so that the data i get on the textfield on another frame can be updated to the new values that I have just input on the other frame (Class DetailedClass).

Some databases have mechanism you can use to monitor for changes that occurred in them. Some don't, but you might poll (ie. periodically query the DB) for such changes. But if this all happens in just one application, both of these approaches would be a huge overkill. You should have an underlying data structure (a "model" in Swing terms) which would "own" the data, controlled their updates in the database and notified all dependent controls of any changes that were made to that data (generally only those that happened in your application, but if you wanted to react to external changes, this would be a great place to put that logic in too).
 
Rodney Ibanez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:
Rodney Ibanez wrote:I was thingking, why do I need to use a PreparedStatement when it is easier to use CreateStatement.

Using PreparedStatement is actually the easier approach. Without it, you have to quote string parameters and convert dates to formats recognized by your database, and every database has it's own ideas about how these things should be done (single quote or double quote? mm/dd/yyyy or dd/mm/yyyy? and so on).

PreparedStatements spares you of this. Plus it's safe. Plus in many databases it performs significantly better. It's a win-win-win.

That was enlightening! Thanks. I'll practice using that from now on!

I've got another question though.. is there a way that I can like refresh my database so that the data i get on the textfield on another frame can be updated to the new values that I have just input on the other frame (Class DetailedClass).

Some databases have mechanism you can use to monitor for changes that occurred in them. Some don't, but you might poll (ie. periodically query the DB) for such changes. But if this all happens in just one application, both of these approaches would be a huge overkill. You should have an underlying data structure (a "model" in Swing terms) which would "own" the data, controlled their updates in the database and notified all dependent controls of any changes that were made to that data (generally only those that happened in your application, but if you wanted to react to external changes, this would be a great place to put that logic in too).


I don't know how to do the advice you have just given me. I'm sure it's very good. I just can't seem to know hoe to implement it. This is my code actually to better illustrate what I mean...

This works already. I am able to update but when I go back to the other frame(LoginEngine), the value that is set in my textboxes are not updated with the value that has been recently updated by the method just above this line. I would have to log out and log back in just so i may see the updated values. If I can do like data refresh on my "LoginEngine" frame, I think I would be able to see the new data in the textboxes. I can do it in VB6 by using LoginEngine.data1.refresh but I don't know how to do it in java. Please help. Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic