Java SQL update syntax

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Java SQL update syntax

 
0
  #1
Jun 8th, 2009
        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.
Local P ...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Java SQL update syntax

 
0
  #2
Jun 8th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Java SQL update syntax

 
0
  #3
Jun 9th, 2009
Originally Posted by Ezzaral View Post
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 ;
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: Java SQL update syntax

 
0
  #4
Jun 9th, 2009
  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
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: Java SQL update syntax

 
0
  #5
Jun 9th, 2009
Originally Posted by Ezzaral View Post
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
Local P ...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Java SQL update syntax

 
0
  #6
Jun 9th, 2009
As I already posted, and so did javaAddict:
use executeUpdate().
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 102
Reputation: localp is an unknown quantity at this point 
Solved Threads: 2
localp localp is offline Offline
Junior Poster

Re: Java SQL update syntax

 
0
  #7
Jun 9th, 2009
  1. String sqll ="UPDATE Patient_Comp SET taken='Taken' WHERE ID="+x ;
  2. int rs = stmt.executeUpdate(sqll);

thanks guys it worked !!!
Local P ...
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Java SQL update syntax

 
0
  #8
Jun 9th, 2009
BTW, consider using prepared statements / parameterized queries instead of creating queries on fly using string concatenation to avoid SQL Injection.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC