Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Add data to the table in the database with the use of add button

843859Jun 20 2007 — edited Jun 21 2007
The name of my database is Socrates.
The name of the table in the database is Employees
I want to be able to add data to the database. i am presently working on the add button such that when i enter date into the textfield and press the add button it should automatically register in the table.

The error upon compilation is with this line of code
If (ae.getSource() == jbtnA)// it says that ";" is expected
Below is the entire code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Mainpage extends JFrame implements ActionListener
{
	JTextField jFirstName = new JTextField(15);
	JTextField jSurname = new JTextField(12);
	JTextField jCity = new JTextField(10);
	JTextField jCountry = new JTextField(12);
	JTextField jSSN = new JTextField(8);

	JLabel jFirstLab = new JLabel("First Name");
	JLabel jSurnameLab = new JLabel("Surname");
	JLabel jCityLab = new JLabel("City");
	JLabel jCountryLab = new JLabel("Country");
	JLabel jSSNLab = new JLabel("Social Security Number (SSN)");
	
	JButton jbtnA = new JButton ("Add");
	JButton jbtnPrv = new JButton ("Previous");
	JButton jbtnNt = new JButton ("Next");
	JButton jbtnDl= new JButton ("Delete");
	JButton jbtnSrch = new JButton ("Search");

	public Mainpage (String title)
	{
		super (title);
		Container cont = getContentPane();
		
		JPanel pane1 = new JPanel();
		JPanel pane2 = new JPanel();
		JPanel pane3 = new JPanel();
		pane1.setLayout (new GridLayout (0,1));
		pane2.setLayout (new GridLayout(0,1));
		pane3.setLayout (new FlowLayout());
		
		pane1.add(jFirstLab);
		pane1.add(jSurnameLab);	
		pane1.add(jCityLab);
		pane1.add(jCountryLab);
		pane1.add(jSSNLab);
		
		pane2.add(jFirstName);
		pane2.add(jSurname);
		pane2.add(jCity);
		pane2.add(jCountry);
		pane2.add(jSSN);

		pane3.add(jbtnA);
		pane3.add(jbtnPrv);
		pane3.add(jbtnNt);
		pane3.add(jbtnDl);
		pane3.add(jbtnSrch);

		cont.add(pane1, BorderLayout.CENTER);
		cont.add(pane2, BorderLayout.LINE_END);
		cont.add(pane3, BorderLayout.SOUTH);

		jFirstName.addActionListener(this);
		jSurname.addActionListener(this);
		jCity.addActionListener(this);
		jCountry.addActionListener(this);
		jSSN.addActionListener(this);

		jbtnA.addActionListener(this);
		jbtnPrv.addActionListener(this);
		jbtnNt.addActionListener(this);
		jbtnDl.addActionListener(this);
		jbtnSrch.addActionListener(this);
		validate();
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setResizable(false);
	}

	public void actionPerformed(ActionEvent ae)
		{
			If (ae.getSource() == jbtnA)
                        {	
                                fst = jFirstName.getText();
				srn = jSurname.getText();
				cty = jCity.getText();
				cnty = jCountry.getText();
				int sn = 

Interger.parseInt(jSSN.getText());
                                String ad = "Insert into Employees 

(Firstname,Surname,City,Country,SSN)" + 

"values('"fst"','"srn"','"cty"','"cnty"','"sn"')";
				Statement stmt = con.createStatment();
				int rowcount = stmt.executeUpdate(ad);
				JOptionPane.showMessageDialog("Your 

details have been registered");
				Statement stmt = con.createStatment();
				int rowcount = stmt.executeUpdate(ad);
			}
		}

public static void main (String args[])
	{
		Mainpage ObjFr = new Mainpage("Please fill this 

registration form");
		try
		{	
			Class.forname("sun.jdbc.odbc.JdbcOdbcDriver");
			String plato = "jdbc:odbc:socrates";
			Connection con = 

DriverManager.getConnection(plato);
			

		}
		catch(SQLException ce)
		{
			System.out.println(ce);
		}
	}		
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 19 2007
Added on Jun 20 2007
6 comments
434 views