Results 1 to 6 of 6
  1. #1
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default java sql update freezes

    I am developing a management system for personal use, but I have encountered a problem, when I try to update data from a database, my application just freezes and does nothing anymore. My code for updating is this:

    Java Code:
    public void pasGegevensAan(int id, String voornaam, String achternaam)
    	{
    		String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    		String dbName = "hoogvlietDB";
    		String connectionURL = "jdbc:derby:" + dbName + ";create=true;";
    		String sqlstatement = "SELECT * FROM WERKNEMERS";
    		
    		try
    		{
    			Class.forName(driver);
    			try
    			{				
    				Connection conn = DriverManager.getConnection(connectionURL);
    				Statement s = conn.createStatement();
    				ResultSet allPersons = s.executeQuery(sqlstatement);
    				if(allPersons.next())
    				{
    					PreparedStatement psInsert = conn.prepareStatement("UPDATE WERKNEMERS " +
    							"SET First_Name = '" + voornaam + "', Last_Name = '" + achternaam + "'" +
    							"WHERE W_id = " + id);
    					psInsert.executeUpdate();
    				}
    			} catch (Throwable ek)
    			{
    				JOptionPane.showMessageDialog(null, ek);
    			}
    		} catch (java.lang.ClassNotFoundException ec)
    		{
    			JOptionPane.showMessageDialog(null, ec);
    		}
    	}
    Can you guys help me with this problem?

    L19htn1n9

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: java sql update freezes

    my application just freezes and does nothing anymore
    Where does it freeze? Add some println's in to debug the code flow (or step through using a debugger)....and recommend reading about how to use PreparedStatements to take advantage of its features (placeholders and setting values of placeholders)

  3. #3
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default Re: java sql update freezes

    Updated code:
    Java Code:
    public void pasGegevensAan(int id, String voornaam, String achternaam)
    	{
    		String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    		String dbName = "hoogvlietDB";
    		String connectionURL = "jdbc:derby:" + dbName + ";create=true;";
    		String sqlstatement = "SELECT * FROM WERKNEMERS";
    		
    		try
    		{
    			Class.forName(driver);
    			try
    			{				
    				Connection conn = DriverManager.getConnection(connectionURL);
    				Statement s = conn.createStatement();
    				ResultSet allPersons = s.executeQuery(sqlstatement);
    				if(allPersons.next())
    				{
    					System.out.println("Before updating");
    					PreparedStatement psInsert = conn.prepareStatement("UPDATE WERKNEMERS " +
    							"SET First_Name = '" + voornaam + "', Last_Name = '" + achternaam + "'" +
    							"WHERE W_id = " + id);
    					psInsert.executeUpdate();
    					System.out.println("After updating");
    				}
    			} catch (Throwable ek)
    			{
    				JOptionPane.showMessageDialog(null, ek);
    			}
    		} catch (java.lang.ClassNotFoundException ec)
    		{
    			JOptionPane.showMessageDialog(null, ec);
    		}
    	}
    I get the first System.out.println();, the "Before updating"
    I dont get the second one, "After updating".
    So the problem is with the executeUpdate statement.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: java sql update freezes

    First, I'd suggest logging the Exceptions when caught (for instance printing out the stack trace) - if any are thrown (my bet is that there is, because the SQL is incorrect - in at least the spacing). Are you running this remotely (if so, you will never see the JOptionPane and it will seem like it freezes)? Do you see the JOptionPane dialog if not running remotely?

  5. #5
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default Re: java sql update freezes

    it freezes when i press the button and i don't see a JOptionPane for a long time
    http://puu.sh/t9lA

    Then after a long time, I get this JOptionPane
    http://puu.sh/t9lu

  6. #6
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default Re: java sql update freezes

    ow nevermind, I saw the problem, there was an connection opened when i tried to edit the database, added the line in the program to close the connection after i was done, and now it works!
    Thanks alot doWhile!

Similar Threads

  1. JFrame freezes when button is clicked
    By JG4m3r in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 03:20 AM
  2. JFrame freezes when button is clicked
    By JG4m3r in forum New To Java
    Replies: 2
    Last Post: 04-04-2012, 11:43 PM
  3. Server Socket GUI Freezes
    By mrhid6 in forum Threads and Synchronization
    Replies: 12
    Last Post: 09-30-2011, 04:39 PM
  4. Java Freezes in Control Panel
    By valpal1919 in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 12:04 PM
  5. Client freezes when method is called
    By chyrl in forum Advanced Java
    Replies: 9
    Last Post: 07-26-2010, 09:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •