Results 1 to 5 of 5
  1. #1
    dragon_baby is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default Updating SQL database

    I have this user interface where I write into a JTextField a card number. I am supposed to update a table from SQL database, inserting a new row in that table and adding to a certain column the string I typed in the JTextField.
    This is how my code looks like:
    Java Code:
    public void setCardNumber(String text) throws SQLException{
    		try
    		{
    			Statement s = connection.createStatement();
    			int update_rows = s.executeUpdate("INSERT INTO Loans(card_number)" + "VALUES(" + text + "')");
    			
    		
    			System.out.println(update_rows + "rows inserted");
    			s.close();
    		}
    		catch (Exception w)
    		{
    			w.printStackTrace();
    		}
    		
    	}
    And this is the code from the GUI:

    Java Code:
    public class Handler2 implements ActionListener{
    		@Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			String s = (String)JOptionPane.showInputDialog("Enter card number:", card_number);
    			
    	
    			
    			if ((s != null) && (s.length() > 0)) 
    				System.out.printf("the string is %s", s);
    			{
    			    
    
    				try
    				{
    					database.setCardNumber(s);
    				
    				}
    				catch (Exception ex)
    				{
    					ex.printStackTrace();
    				}
    			}
    		}
    I get a whole bunch of errors, some of them are ...
    Java Code:
    com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed.
    	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:388)
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(SQLServerConnection.java:2156)
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(SQLServerConnection.java:1845)
    	at persistency.Database.setCardNumber(Database.java:140)
    	at userInterface.Interface$Handler2.actionPerformed(Interface.java:177)
    Line 140 is this one
    Java Code:
    Statement s = connection.createStatement();
    , line 177 is
    Java Code:
    database.setCardNumber(s);
    I am not extremely familiar with java and I am stuck. Please help

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

    Default Re: Updating SQL database

    The exception states that the Connection is closed. Where do you create the Connection? Do you ever close it prior to calling the method posted above?

  3. #3
    dragon_baby is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default Re: Updating SQL database

    You are right, the connection was closed and I did not open it again. :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Updating SQL database

    Opening a connection can cause quite some work for the database, i.e. resources need to be created, buffers need to be initialized etc. etc. Better keep your connection open for as long as you need it frequently.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    dragon_baby is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Updating Ms Access Database using jsp
    By gauravmanral in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 07-01-2012, 07:07 PM
  2. Problem updating access database from jsp
    By Penhexy in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-17-2011, 09:50 AM
  3. Updating my GUI
    By Catkill in forum AWT / Swing
    Replies: 6
    Last Post: 09-01-2009, 06:09 PM
  4. Updating database table from JTable
    By yesjava in forum New To Java
    Replies: 1
    Last Post: 08-16-2008, 11:16 PM
  5. Help needed with updating mysql database
    By SilentCodingOne in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 11:23 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
  •