*
The moose likes Swing / AWT / SWT and the fly likes Updating a record in database using a save button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Updating a record in database using a save button" Watch "Updating a record in database using a save button" New topic
Author

Updating a record in database using a save button

Rodney Ibanez
Greenhorn

Joined: Aug 30, 2012
Posts: 17
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

Joined: Mar 10, 2008
Posts: 1756
    
    7

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.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1756
    
    7

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

Joined: Aug 30, 2012
Posts: 17
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

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Aug 30, 2012
Posts: 17
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Updating a record in database using a save button
 
Similar Threads
fixed element in gridbaglayout
capture the time differeence of keyevents
GridBagConstraints Problem
Failed to start error in Apache Derby
passing value and setting it to jlabel text