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

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.*;

publicclass Mainpageextends JFrameimplements 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);

}

publicvoid 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);

}

}

publicstaticvoid 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);

}

}

}

[6622 byte] By [bakesa] at [2007-11-27 8:16:08]
# 1
ifnotIf
cotton.ma at 2007-7-12 20:01:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

i have restructured the code, but the following line of code is giving error:

String plato = jdbc:odbc:socrates;

the entire code is below:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

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");

Statement stmt;

String ad;

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);

try

{

Class.forname(sun.jdbc.odbc.JdbcOdbcDriver);

String plato = jdbc:odbc:socrates;

Connection con = DriverManager.getConnection(plato);

stmt = con.createStatment();

}

catch(SQLException ce)

{

System.out.println(ce);

}

catch(ClassNotFoundException ce)

{

System.out.println(ce);

}

}

public void actionPerformed(ActionEvent ae)

{

try

{

if(ae.getSource().equals(jbtnA))

{

fst = jFirstName.getText();

srn = jSurname.getText();

cty = jCity.getText();

cnty = jCountry.getText();

int sn = Interger.parseInt(jSSN.getText());

ad = "Insert into Employees

values('"+fst+"',"+srn+"','"+cty+"','"+cnty+"','"+sn+"')";

stmt.executeUpdate(ad);

JOptionPane.showMessageDialog(this, "Your details have been

registered");

}

}

catch(SQLException ce)

{

System.out.println(ce);

}

}

public static void main(String args[])

{

Mainpage ObjFr = new Mainpage("Please fill this registration form");

}

}

bakesa at 2007-7-12 20:01:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
String plato = "jdbc:odbc:socrates";instead ofString plato = jdbc:odbc:socrates;;)
jpadrona at 2007-7-12 20:01:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

I have compiled and ran the codes.

However, when i pressed the Add button upon entering data into the textfiled i got the following error:

java.sql.SQLException[Microsoft][ODBC SQL Server Driver][SQL Server] The name 'Andrew' is not permitted in this context. Only constants, Expression, or variables allowed here. column names are not permitted

below is my TSQL statement for the construction of the table:

create table Employees

(

FirstName char(25) NULL,

Surname char(25) NOT NULL,

City char (25) NOT NULL,

Country char (25) NOT NULL,

SSN int NOT NULL,)

bakesa at 2007-7-12 20:01:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

You've got syntax errors in your SQL. Maybe an extra comma in the table create, and missing apostrophe in the insert.

When you're trying to debug database code and you get a SQL error, have your program print out the query its about to run (with System.out.println) and try running it on the DB directly. That will help you find syntax errors yourself, instead of wasting our time reading your post and wasting your time waiting for a response, when you could fix it yourself in 2 minutes. :)

Jemiah

fishninja007a at 2007-7-12 20:01:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Thanks for your suggestion. However of everyone is as good as you are. some of us are new to java. in fact to programming languages.

i am trying to use a search button in retrieving data from the database.

i have tried employing prepared statement which i am not that conversant with. it is giving me the following error on the following line of code:

pstmt = con.prepareStatement();

i had declared the following instance variables:

Connection con;

PreparedStatement pstmt;

String fst, srn, cty, cnty, sn;

String rtr, strTemp, strTemp1, strTemp2, strTemp3;

ResultSet rs;

below is the part of the code i intend to perform the function of retrieving:

public Connection getConnection()

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String plato = "jdbc:odbc:socrates";

con = DriverManager.getConnection(plato, "sysdba", "oracle");

pstmt = con.prepareStatement();

}

catch(SQLException ce)

{

System.out.println(ce);

}

catch(ClassNotFoundException ce)

{

System.out.println(ce);

}

}

public void actionPerformed(ActionEvent ae)

{

String sn = "";

sn = jSSN.getText();

if(ae.getSource().equals(jbtnSrch))

{

try

{

rtr = "Select * from Employees where SSN like ?";

pstmt =

con.prepareStatement(rtr,ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

sn = "%" +sn +"%";

pstmt.setString(5, sn);

rs = pstmt.executeQuery();

while(rs.next())

{

strTemp = rs.getString("First Name");

strTemp1 = rs.getString("Surname");

strTemp2 = rs.getString("City");

strTemp3 = rs.getString("Country");

jFirstName.setText(strTemp);

jSurname.setText(strTemp1);

jCity.setText(strTemp2);

jCountry.setText(strTemp3);

}

}

catch(SQLException ce)

{

System.out.println(ce);

}

}

}

any idea what i might be doing wrong?

bakesa at 2007-7-12 20:01:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...