Results 1 to 5 of 5
  1. #1
    Brutto is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default JTextField to database

    Hello, Guys! I need your advice.
    I have some troubles with binding JTextField to my database (MSSQL Server). I need to write text from a JTextfield to the database when users click a jbutton. There are no problems with the database connection and the code works if I put any constant in the line "String table = "INSERT INTO Department " + "VALUES ('"+maxid+"',"+Dptmnt.getText()+")";" istead of "+Dptmnt.getText()+"

    Here is a bit of the code:

    Java Code:
    class RegIntDoc extends JDialog
    {  
    	public RegIntDoc(JFrame owner)
    	{  
    		super(owner, "Registration", true);  
    		// 
    		JLabel label = new JLabel ("Department: ");
    		Dptmnt = new JTextField(20);
    		Dptmnt.setText("Enter");
    		//txt = Department.getText();
    		JButton cancel = new JButton("Cancel");
    		JButton ok = new JButton("Ок");
    		System.out.println("Department=" + Dptmnt);
    		cancel.addActionListener(new ActionListener()
    		{  
    			public void actionPerformed(ActionEvent event) 
    			{ 
    				setVisible(false); 
    			} 
    		});
    		ok.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent event) 
    			{ 
    				try {
    					registerInternal();
    				} catch (SQLServerException e) {
    					e.printStackTrace();
    				}
    			} 
    		});
    		
    		JPanel panel = new JPanel();
    		JPanel panel2 = new JPanel();
    		panel.add(ok);
    		panel.add(cancel);
    		panel2.add(label);
    		panel2.add(Dptmnt);
    		//////////Dptmnt = new JTextField(20);
    		add(panel2, BorderLayout.CENTER);
    		add(panel, BorderLayout.SOUTH);
    		setSize(350, 200);
    	}
    	/**
    	 * 
    	 * @throws SQLServerException 
    	 */
    	public void registerInternal() throws SQLServerException
    	{
    		System.out.println("Table Creation Example!");
    		//Connection con = null;
    
    		SQLServerDataSource ds = new SQLServerDataSource();
    		ds.setUser("sa");
    		ds.setPassword("111");
    		ds.setServerName("localhost");
    		ds.setPortNumber(1433); 
    		ds.setDatabaseName("uchet");
    		Connection con = ds.getConnection();
    		try
    		{
    			Statement st = con.createStatement();
    			String query = "SELECT MAX(ID) FROM Department";
    			ResultSet rs = st.executeQuery(query);
    			if (rs.next()) 
    			{
    				  maxid = rs.getInt(1)+1;
    			}
    			
    			String table = "INSERT INTO Department " + "VALUES ('"+maxid+"',"+Dptmnt.getText()+")";
    			st.executeUpdate(table);
    			System.out.println("Table creation process successfully!");
    		}
    		catch(SQLException s)
    		{
    			System.out.println("Something goes wrong");
    		}
    	}
    	static int maxid=1;
    	private JTextField Dptmnt;
    }
    Last edited by Brutto; 06-04-2012 at 11:15 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: JTextField to database

    Use PreparedStatement, not Statement. That will take care of the quoting you omitted.

    Since this is really a JDBC, not Swing, question, I'm moving the thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: JTextField to database

    So what happens at the moment?
    (And you might want to printStackTrace() in that catch block).

    And shouldn't your ID field be an IDENTITY field? It would save you the rather un-thread safe technique you are using to calculate the next ID.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Brutto is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: JTextField to database

    Thanks,
    The program really works now.
    Tolls, you are right, I will take note of this.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: JTextField to database

    Quote Originally Posted by Brutto View Post
    The program really works now.
    Since this is a forum, would you like to share the solution for the benefit of other readers who come across this thread?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2011, 11:59 PM
  2. Replies: 16
    Last Post: 05-24-2011, 09:29 AM
  3. Adding jTextField() content to the database?
    By Stephen Douglas in forum New To Java
    Replies: 12
    Last Post: 04-08-2010, 09:50 AM
  4. Replies: 3
    Last Post: 01-07-2010, 12:07 PM
  5. Replies: 1
    Last Post: 01-30-2009, 06:44 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
  •