how to retrieve data from the database

the assignment i am working on is this

i have a table 'Employees" in the database with five columns(First Name, Surname, City, Country, SSN)

i have designed a page "Mainpage" already with textfields to represent each of the columns in the table "Employees".

I also have five buttons (Add, Delete, Previous, Next, Search) at the bottom on mainpage

At the moment when I press the add button, the data in the textfields are register under their corresponding columns on the table Employees..

What I am struggling with now is making the other buttons work.

i have been working on the search button but it is not able to retrieve the full information of the whatever SSN that is entered unto the SSN textfield.

The error i am getting is this:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at line 115

line of code 115:

PreparedStatement pstmt = con.prepareStatement("SELECT * FROM Employees where SSN=?");

Below is the entire code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

import javax.sql.*;

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

Connection con;

PreparedStatement pstmt;

//String rtr, strTemp, strTemp1, strTemp2, strTemp3, strTemp4, sn;

ResultSet rs;

public Mainpage2 (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);

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.createStatement();

}

catch(SQLException ce)

{

System.out.println(ce);

}

catch(ClassNotFoundException ce)

{

System.out.println(ce);

}

}

publicvoid actionPerformed(ActionEvent ae)

{

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

{

try

{

// Create a result set containing all data from my_table

PreparedStatement pstmt = con.prepareStatement("SELECT * FROM Employees where SSN=?");

pstmt.setString(5,jSSN.getText());

ResultSet rs = pstmt.executeQuery();

rs.next();

}

catch(SQLException ce)

{

showRecord(rs);

}

}

}

publicvoid showRecord(ResultSet rs)

{

try

{

jFirstName.setText(rs.getString(1));

jSurname.setText(rs.getString(2));

jCity.setText(rs.getString(3));

jCountry.setText(rs.getString(4));

jSSN.setText(rs.getString(5));

}

catch(Exception e)

{

System.out.println(e);

}

}

publicstaticvoid main(String args[])

{

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

}

}

[7959 byte] By [bakesa] at [2007-11-27 8:39:54]
# 1
>pstmt.setString(5,jSSN.getText());why 5 ?!!use this instead : pstmt.setString(1,jSSN.getText());see http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.htmlhope that helps
java_2006a at 2007-7-12 20:38:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

> Exception in thread "AWT-EventQueue-0" java.lang.

> NullPointerException at line 115

> line of code 115:

> PreparedStatement pstmt = con.prepareStatement("SELECT * FROM

> Employees where SSN=?");

your con actually still null. test it:

if (con == null) System.out.println("connection still null");

//PreparedStatement pstmt = con....

j_shadinataa at 2007-7-12 20:38:06 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...