943,865 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 8610
  • Java RSS
Jun 8th, 2009
0

Java SQL update syntax

Expand Post »
        try {
            //1.Load the database Driver class
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            //2.Make a connection to the database
            Connection conn = DriverManager.getConnection(
                    "jdbc:odbc:MyDataSource");
            Statement stmt = conn.createStatement();
            String x = jTextField1.getText().toString();
            String sqll ="UPDATE Patient_Comp SET taken='Taken'WHERE ID="+x ;
            ResultSet rs = stmt.executeQuery(sqll);

This is what i wrote to update a instance of a column, but i am getting exceptions.. i am not sure if executeQuery
is the correct attribute. will some one please provide me with the working code ..
Last edited by localp; Jun 8th, 2009 at 3:28 pm.
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Jun 8th, 2009
0

Re: Java SQL update syntax

Well, the first hint would be the exception itself, which you did not specify here.

I would recommend using executeUpdate() which returns the number of rows affected instead of executeQuery(), which return a result set.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jun 9th, 2009
0

Re: Java SQL update syntax

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Well, the first hint would be the exception itself, which you did not specify here.

I would recommend using executeUpdate() which returns the number of rows affected instead of executeQuery(), which return a result set.
I thought that you cannot use executeQuery for "insert", "update", "delete" queries.
Anyway apart from using executeUpdate maybe you should leave a space between 'Taken' and WHERE:

String sqll ="UPDATE Patient_Comp SET taken='Taken'WHERE ID="+x ;
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Jun 9th, 2009
0

Re: Java SQL update syntax

Java Syntax (Toggle Plain Text)
  1. java.sql.SQLException: No ResultSet was produced
  2. at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:259)
  3. at shanesmed.DocDisplay.jButton2_actionPerformed(DocDisplay.java:150)
  4. at shanesmed.DocDisplay_jButton2_actionAdapter.actionPerformed(DocDisplay.java:179)
  5. at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
  6. at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
  7. at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
  8. at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
  9. at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
  10. at java.awt.Component.processMouseEvent(Component.java:5488)
  11. at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
  12. at java.awt.Component.processEvent(Component.java:5253)
  13. at java.awt.Container.processEvent(Container.java:1966)
  14. at java.awt.Component.dispatchEventImpl(Component.java:3955)
  15. at java.awt.Container.dispatchEventImpl(Container.java:2024)
  16. at java.awt.Component.dispatchEvent(Component.java:3803)
  17. at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
  18. at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
  19. at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
  20. at java.awt.Container.dispatchEventImpl(Container.java:2010)
  21. at java.awt.Window.dispatchEventImpl(Window.java:1774)
  22. at java.awt.Component.dispatchEvent(Component.java:3803)
  23. at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
  24. at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
  25. at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
  26. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
  27. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
  28. at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)


This is the exception that i got
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Jun 9th, 2009
0

Re: Java SQL update syntax

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Well, the first hint would be the exception itself, which you did not specify here.

I would recommend using executeUpdate() which returns the number of rows affected instead of executeQuery(), which return a result set.

Then how can i update ?? what will be the java code for update
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Jun 9th, 2009
0

Re: Java SQL update syntax

As I already posted, and so did javaAddict:
use executeUpdate().
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jun 9th, 2009
0

Re: Java SQL update syntax

Java Syntax (Toggle Plain Text)
  1. String sqll ="UPDATE Patient_Comp SET taken='Taken' WHERE ID="+x ;
  2. int rs = stmt.executeUpdate(sqll);

thanks guys it worked !!!
Reputation Points: 1
Solved Threads: 3
Junior Poster
localp is offline Offline
190 posts
since Apr 2008
Jun 9th, 2009
0

Re: Java SQL update syntax

BTW, consider using prepared statements / parameterized queries instead of creating queries on fly using string concatenation to avoid SQL Injection.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Sending files in socket problem///
Next Thread in Java Forum Timeline: Contd from Mouse over





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC