Results 1 to 9 of 9
Thread: Update statement gone HeWire
- 09-27-2011, 05:11 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Update statement gone HeWire
I'm trying to update the database but whatever I change there is alway's 1 or more lines red underlined because of an error. In this version all lines containing stmt give the following error:Java Code:public void setSettings(int gebruikersNr, String wachtwoord, boolean notRunning) { Statement stmt = con.createStatement( ); stmt.executeUpdate ("UPDATE Werknemer " + "SET password = '" + wachtwoord + "' " + "nietLopend = '" + notRunning + "'" + "WHERE userID = '" + gebruikersNr + "'"); stmt.close(); }
Unreported exception java.sql.SQLException; must be caught of
explained to be thrown.
It's probably a + or " in the wrong place but I tryed a LOT of them and couldn't find where I was wrong.
Any help would be appreciated.
- 09-27-2011, 05:49 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Update statement gone HeWire
Pretty much everyone of those methods inside your setSettings method can throw a SQLException, so you need to handle that.
That's the general structure.Java Code:Statement stmt = null; try { .. your code here (except the close) } catch (SQLException ex) { ex.printStatckTrace(); } finally { try { if (stmt != null) { stmt.close(); } } catch (...etc) { } }
You ought to be using a PreparedStatement as well, since concatenating your SQL together is usually a Bad Thing (see SQL Injection, or string escaping).
- 09-28-2011, 10:23 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Update statement gone HeWire
Java Code:public void setSettings(int gebruikersNr, String wachtwoord, boolean notRunning) { Connect(); System.out.println("700"); try { System.out.println("701"); PreparedStatement stmt = con.prepareStatement ( "UPDATE Werknemer " + "SET password = ?,nietLopend = ?" + "WHERE userID = ?" ); System.out.println("702"); int[] rows; for(int i = 0; i < 35;i++) { stmt.setString(1, wachtwoord); stmt.setBoolean(2, notRunning); stmt.setInt(3, gebruikersNr); stmt.addBatch(); } System.out.println("703"); rows = stmt.executeBatch(); } catch(SQLException ex) { ex.printStackTrace(); } finally { try { if(stmt != null) { stmt.close(); } } catch(SQLException ex) { ex.printStackTrace(); } } }I have changed it like you advise and now the program wil run again but when I try to use this part i get the above mentiond Exceptions.Java Code:700 701 java.sql.SQLException: No operations allowed after connection closed. Connection was closed explicitly by the application at the following location: ** BEGIN NESTED EXCEPTION ** java.lang.Throwable STACKTRACE: java.lang.Throwable at com.mysql.jdbc.Connection.close(Connection.java:1125) at urenreg107.DBConnect.Close(DBConnect.java:174) at urenreg107.DBConnect.getSettings(DBConnect.java:218) at urenreg107.UrenReg107View.SettingsUButtonButtonActionPerformed(UrenReg107View.java:714) at urenreg107.UrenReg107View.access$1400(UrenReg107View.java:23) at urenreg107.UrenReg107View$10.actionPerformed(UrenReg107View.java:333) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6504) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6269) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4860) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4686) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2713) at java.awt.Component.dispatchEvent(Component.java:4686) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707) at java.awt.EventQueue.access$000(EventQueue.java:101) at java.awt.EventQueue$3.run(EventQueue.java:666) at java.awt.EventQueue$3.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:680) at java.awt.EventQueue$4.run(EventQueue.java:678) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) ** END NESTED EXCEPTION **
Now I checked the net and some NESTED EXCEPTIONS are because I left the server on for to long but mine didn't work after a restart.
as you can see in the system.out the code reaches PrepareStatement but something about if is wrong because it won't print 702.
If you have a WORKING COMPLETE EXAMPLE where I have to change some stuff to match my own version that is fine but al the examples I fine just show like a couple of lines.
- 09-28-2011, 10:32 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Update statement gone HeWire
at urenreg107.DBConnect.Close(DBConnect.java:174)
at urenreg107.DBConnect.getSettings(DBConnect.java:21 8)
at urenreg107.UrenReg107View.SettingsUButtonButtonAct ionPerformed(UrenReg107View.java:714)
at urenreg107.UrenReg107View.access$1400(UrenReg107Vi ew.java:23)
at urenreg107.UrenReg107View$10.actionPerformed(UrenR eg107View.java:333)
In that code there you are closing the connection.
You are then attempting to use that connection, hence the exception.
Not knowing what you are trying to do I cannot say if closing the connection was correct, or whether that is the mistake and you should be getting a new connection each time.
- 09-28-2011, 12:25 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Update statement gone HeWire
problem solved code above is working with what I want.Java Code:public void setSettings(int gebruikersNr, String wachtwoord, boolean notRunning) { Connect(); System.out.println("700"); try { con = DriverManager.getConnection(url, "borg", ""); PreparedStatement stmt = con.prepareStatement ( "UPDATE Werknemer " + "SET password = ? ,nietLopend = ? " + "WHERE userID = ?" ); System.out.println("702"); int[] rows; for(int i = 0; i < 35;i++) { stmt.setString(1, wachtwoord); stmt.setBoolean(2, notRunning); stmt.setInt(3, gebruikersNr); stmt.addBatch(); } System.out.println("703"); rows = stmt.executeBatch(); } catch( SQLException e ) { e.printStackTrace(); } finally { try { if(stmt != null) { stmt.close(); } } catch(SQLException e) { e.printStackTrace(); } } }
main problems: con = DriverManager.getConnection(url, "borg", ""); was missing.
and: changed it to a prepareStatement.
THX for the help again TollsLast edited by _-Blackhawk-_; 10-06-2011 at 11:57 AM.
- 09-28-2011, 12:30 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Update statement gone HeWire
Are you ensuring you close any open connections?
That code (as it stands) will likely result in more and more open connections, until the db finally decides not to allow anymore.
- 09-28-2011, 01:25 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Re: Update statement gone HeWire
Would running Close();
be sufficiant at the end of every Statement? or should I add something?Java Code:public void Close() { if( con != null ) { try { con.close( ); } catch( Exception e ) { e.printStackTrace( ); } } }
- 09-28-2011, 01:45 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Update statement gone HeWire
As I said, I don't know your setup.
I don't know what it is you;re writing.
Some systems work best with a single connection you maintain, some with a connection pool, some with opening a connection as needed...
- 10-06-2011, 11:56 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
error while using sql update statement
By jttslg in forum Advanced JavaReplies: 14Last Post: 07-03-2011, 10:38 PM -
oracle update statement within the resultset is not working.pls see code.
By renu in forum New To JavaReplies: 1Last Post: 05-17-2011, 09:43 AM -
issue with update on table and prepared statement
By hacktorious in forum JDBCReplies: 1Last Post: 01-10-2011, 01:44 AM -
Update statement not working.
By OMFGITSROHIT in forum JDBCReplies: 5Last Post: 04-08-2010, 01:03 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks