stucked!!
Hi there,
Anyone has an idea or ever written an auction program?....I'm quite new to java programming....Please I'm in need of help.
So many problems....am not sure where to begin. I'll have to start frm somwhere!
1 im unable to store to the database from the CustomerUI. the codes are below:
CustomerPD:
import java.util.Vector;
publicclass CustomerPD
{
// attribute definitions
private String name;
private String address;
private String phoneNo;
// constructor with parameters
public CustomerPD(String aName, String anAddress, String aPhoneNo)
{
// invoke setters to populate attributes
setName(aName);
setAddress(anAddress);
setPhoneNo(aPhoneNo);
}
// DA static methods *********************************
publicstaticvoid initialize()
{CustomerDA.initialize();}
publicstatic CustomerPD find(String phoneNo)throws NotFoundException
{return CustomerDA.find(phoneNo);}
publicstatic Vector getAll()
{return CustomerDA.getAll();}
publicstaticvoid terminate()
{CustomerDA.terminate();}
// DA instance methods *********************************
publicvoid addNew()throws DuplicateException
{CustomerDA.addNew(this);}
publicvoid delete()throws NotFoundException
{CustomerDA.delete(this);}
publicvoid update()throws NotFoundException
{CustomerDA.update(this);}
// get accessors
public String getName()
{return name;}
public String getAddress()
{return address;}
public String getPhoneNo()
{return phoneNo;}
// set accessors
publicvoid setName(String newName)
{ name = newName;}
publicvoid setAddress(String newAddress)
{ address = newAddress;}
publicvoid setPhoneNo(String newPhoneNo)
{ phoneNo = newPhoneNo;}
public String tellAboutSelf()
{return (getName() +", " + getAddress() +", " + getPhoneNo());}
}
CustomerUI:
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
publicclass CustomerUIextends JFrameimplements ActionListener
{
//JLabel
private JLabel jsJLabel;
private JLabel bargainJLabel;
private JLabel pic1JLabel;
private JLabel welJLabel;
private JLabel contJLabel;
//JLabel for customer attributes
private JLabel phoneJLabel;
private JLabel addressJLabel;
private JLabel nameJLabel;
// JButton
private JButton addJButton;
private JButton clearJButton;
private JButton closeJButton;
private JButton homeJButton;
//JTextField
private JTextField phoneJTextField;
private JTextField addressJTextField;
private JTextField nameJTextField;
CustomerPD aCustomer;
String customerName, customerAddress, customerPhone;
// no-argument constructor
public CustomerUI()
{
createUserInterface();
}
// create and position GUI components; register event handlers
privatevoid createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout(null );
contentPane.setBackground(Color.PINK);
// set up jsJLabel
jsJLabel =new JLabel();
jsJLabel.setBounds( 100, 24, 250, 21 );
jsJLabel.setText("JS Auction House" );
jsJLabel.setFont(new Font("SanSerif", Font.BOLD, 22 ) );
jsJLabel.setHorizontalAlignment(JLabel.CENTER );
contentPane.add( jsJLabel );
// set up bargainJLabel
bargainJLabel =new JLabel();
bargainJLabel.setBounds( 80, 46, 250, 20 );
bargainJLabel.setText("...house of huge bargains!");
bargainJLabel.setFont(
new Font("SanSerif", Font.ITALIC, 14 ) );
bargainJLabel.setHorizontalAlignment(
JLabel.CENTER );
contentPane.add( bargainJLabel );
// set up pic1JLabel
pic1JLabel =new JLabel ();
pic1JLabel.setIcon (new ImageIcon("pic1.gif") );
pic1JLabel.setBounds( 350, 10, 150, 91);
pic1JLabel.setHorizontalAlignment( JLabel.LEFT );
contentPane.add( pic1JLabel);
//set up welJLabel
welJLabel=new JLabel();
welJLabel.setBounds(99,70,250, 71 );
welJLabel.setText("Welcome to JS Auction House.");
welJLabel.setFont(new Font("Sanserif",Font.PLAIN,14));
welJLabel.setBackground(Color.LIGHT_GRAY);
contentPane.add(welJLabel);
//set up contJLabel
contJLabel=new JLabel();
contJLabel.setBounds(299,90,250, 71 );
contJLabel.setText("Add customer");
contJLabel.setFont(new Font("Sanserif",Font.BOLD,16));
contentPane.add(contJLabel);
//set up nameJLabel
nameJLabel=new JLabel();
nameJLabel.setBounds(155,145,250,71 );
nameJLabel.setText("Name:");
nameJLabel.setFont(new Font("Sanserif",Font.PLAIN,14));
contentPane.add(nameJLabel);
//set up phoneJLabel
phoneJLabel=new JLabel();
phoneJLabel.setBounds(155,220,250,71 );
phoneJLabel.setText("Telephone:");
phoneJLabel.setFont(new Font("Sanserif",Font.PLAIN,14));
contentPane.add(phoneJLabel);
//set up addressJLabel
addressJLabel =new JLabel();
addressJLabel.setBounds(155,180,250,71 );
addressJLabel.setText("Address:");
addressJLabel.setFont(new Font("Sanserif",Font.PLAIN,14));
contentPane.add(addressJLabel);
//set up nameTextField
nameJTextField =new JTextField();
nameJTextField.setBounds(250,165,160,20 );
contentPane.add(nameJTextField);
//set up addressTextField
addressJTextField =new JTextField();
addressJTextField.setBounds(250,205,160,20 );
contentPane.add(addressJTextField);
//set up phoneTextField
phoneJTextField =new JTextField();
phoneJTextField.setBounds(250,245,160,20 );
contentPane.add(phoneJTextField);
//set up homeJButton
homeJButton =new JButton();
homeJButton.setBounds(100,350,70,24 );
homeJButton.setHorizontalAlignment( JButton.RIGHT );
homeJButton.setText("Home");
contentPane.add(homeJButton);
//set up addJButton
addJButton =new JButton();
addJButton.setBounds(200,350,80,24 );
addJButton.setHorizontalAlignment( JButton.RIGHT );
addJButton.setText("Add Me!");
contentPane.add(addJButton);
//set up clearJButton
clearJButton =new JButton();
clearJButton.setBounds(310,350,70,24 );
clearJButton.setHorizontalAlignment( JButton.RIGHT );
clearJButton.setText("Clear");
contentPane.add(clearJButton);
//set up closeJButton
closeJButton =new JButton();
closeJButton.setBounds(410,350,70,24 );
closeJButton.setHorizontalAlignment( JButton.RIGHT );
closeJButton.setText("Close");
contentPane.add(closeJButton);
// register frame as listener for button events
homeJButton.addActionListener(this);
addJButton.addActionListener(this);
clearJButton.addActionListener(this);
closeJButton.addActionListener(this);
//set properties of application's window
this.setTitle("JS Auction House - Add customer" );// set title bar string
this.setSize( 607, 507 );// set window size
this.setVisible(true );// display window
// create anonymous inner class to handle window closing event
this.addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent event)
{
shutDown();
}
}
);
}// end method createUserInterface
// actionPerformed is invoked when a Button is clicked
publicvoid actionPerformed(ActionEvent e)
{
// see which button was clicked
if(e.getSource() == homeJButton)
{
new AuctionUI();
}
if(e.getSource() == addJButton)
{
CustomerUI();
}
if(e.getSource() == clearJButton)
{
clearForm();
}
if(e.getSource() == closeJButton)
{
shutDown();
}
}
privatevoid CustomerUI()
{
customerName = nameJTextField.getText();
customerAddress = addressJTextField.getText();
customerPhone = phoneJTextField.getText();
if(customerName.length() == 0 || customerAddress.length() == 0 || customerPhone.length() == 0)
JOptionPane.showMessageDialog(this,"Please Enter All Data ");
else
{
aCustomer =new CustomerPD(customerName, customerAddress, customerPhone);
JOptionPane.showMessageDialog(this,"Customer Added");
clearForm();
}
}
privatevoid clearForm()
{
nameJTextField.setText("");
addressJTextField.setText("");
phoneJTextField.setText("");
nameJTextField.requestFocus();
}
publicvoid shutDown()
{
System.exit(0);
}
// main method
publicstaticvoid main( String[] args )
{
CustomerUI application =new CustomerUI();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}// end method main
}// end class CustomerUI
CustomerDA:
import java.util.Vector;// for Vector of customers
import java.io.*;// needed for file i/o
import java.sql.*;// for SQL
publicclass CustomerDA
{
static CustomerPD aCustomer;
//
static String url ="jdbc:odbc:test";
static Connection aConnection;
static Statement aStatement;
// declare variables for Customer attribute values
static String name;
static String address;
static String phoneNumber;
// Implement the four static methods in CustomerPD
// initialize, find, getAll, and terminate
// establish the database connection
publicstaticvoid initialize()
{
try
{
// load the jdbc - odbc bridge driver for Windows
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// create connection instance
aConnection = DriverManager.getConnection(url,"","");
// create statement object instance for this connection
aStatement = aConnection.createStatement();
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
catch (SQLException e)
{
System.out.println(e);
}
}
// find an instance in the database
publicstatic CustomerPD find(String key)throws NotFoundException
{
aCustomer =null;
// define the SQL query statement using the phone number key
String sqlQuery ="SELECT Name, Address, PhoneNo " +
"FROM CustomerTable " +
"WHERE PhoneNo = '" + key +"'";
// execute the SQL query statement
try
{
ResultSet rs = aStatement.executeQuery(sqlQuery);
// next method sets cursor & returns true if there is data
boolean gotIt = rs.next();
if (gotIt)
{
// extract the data
String name = rs.getString(1);
String address = rs.getString(2);
String phoneNumber = rs.getString(3);
// create Customer instance
aCustomer =new CustomerPD(name, address, phoneNumber);
}
else
{
// nothing was retrieved
throw (new NotFoundException("not found "));
}
rs.close();
}
catch (SQLException e)
{
System.out.println(e);
}
return aCustomer;
}
// get all instances from the database
publicstatic Vector getAll()
{
Vector customers =new Vector();
// define the SQL query statement
String sqlQuery ="SELECT Name, Address, PhoneNo " +
"FROM CustomerTable ";
try
{
// execute the SQL query statement
ResultSet rs = aStatement.executeQuery(sqlQuery);
boolean moreData = rs.next();
if (moreData)
// next method sets cursor & returns true if there is data
while (moreData)
{
// extract the data
name = rs.getString(1);
address = rs.getString(2);
phoneNumber = rs.getString(3);
// create Customer instance
aCustomer =new CustomerPD(name, address, phoneNumber);
customers.addElement(aCustomer);
moreData = rs.next();
}
rs.close();
}
catch (SQLException e)
{
System.out.println(e);
}
return customers;
}
// close the database connection
publicstaticvoid terminate()
{
try
{// close everything
aStatement.close();
aConnection.close();
}
catch (SQLException e)
{
System.out.println(e);
}
}
// Implement the four instance methods in Customer *************
// addNew, delete, update
// add new instance to database
publicstaticvoid addNew(CustomerPD aCustomer)
throws DuplicateException
{
// retrieve the customer attribute values
name = aCustomer.getName();
address = aCustomer.getAddress();
phoneNumber = aCustomer.getPhoneNo();
// create the SQL insert statement using attribute values
String sqlInsert ="INSERT INTO CustomerTable " +
"(Name, Address, PhoneNo)" +
"VALUES ('" +
name +"', '" +
address+"', '" +
phoneNumber +"')";
// see if this customer already exists in the database
try
{
CustomerPD c = find(phoneNumber);
throw (new DuplicateException("Customer Exists "));
}
// if NotFoundException, add customer to database
catch(NotFoundException e)
{
try
{
// execute the SQL update statement, a 1 return good
int result = aStatement.executeUpdate(sqlInsert);
}
catch (SQLException ee)
{
System.out.println(ee);
}
}
}
// delete an instance from the database
publicstaticvoid delete(CustomerPD aCustomer)
throws NotFoundException
{
// retrieve the phone no (key)
phoneNumber = aCustomer.getPhoneNo();
// create the SQL delete statement
String sqlDelete ="DELETE FROM CustomerTable " +
"WHERE PhoneNo = '" + phoneNumber +"'";
// see if this customer already exists in the database
// NotFoundException is thrown by find method
try
{
CustomerPD c = CustomerPD.find(phoneNumber);
// if found, execute the SQL update statement, a 1 return
// is good delete
int result = aStatement.executeUpdate(sqlDelete);
}
catch (SQLException e)
{
System.out.println(e);
}
}
// update instance in the database
publicstaticvoid update(CustomerPD aCustomer)throws
NotFoundException
{
// retrieve the customer attribute values
phoneNumber = aCustomer.getPhoneNo();
name = aCustomer.getName();
address = aCustomer.getAddress();
// define the SQL query statement using the phone number key
String sqlUpdate ="UPDATE CustomerTable " +
" SET Name= '" + name +"', " +
" Address= '" + address +"' " +
" WHERE PhoneNo = '" + phoneNumber +"'";
// see if this customer already exists in the database
// NotFoundException is thrown by find method
try
{
CustomerPD c = CustomerPD.find(phoneNumber);
// if found, execute the SQL update statement, a 1 return
// is good delete
int result = aStatement.executeUpdate(sqlUpdate);
}
catch (SQLException ee)
{
System.out.println(ee);
}
}
}
DuplicateException:
publicclass DuplicateExceptionextends Exception
{
public DuplicateException(String message)
{super(message);}
}
NotFoundException:
publicclass NotFoundExceptionextends Exception
{
public NotFoundException(String message)
{super(message);}
}

