Originally Posted by
GregBrannon
I suggest that another class be created to hold all of the relevant data for the entire program. The data kept in an instance of that class should accurately reflect the application's state at any given instant in time, and each supporting object should have a reference to that instance. (If it's possible that the data could be changed or accessed by clients from multiple threads, then you should also read up on synchronized methods.) THEN, any of the other application's objects could update or retrieve the data needed from that instance using getter and setter methods.
If you are not familiar with the use of accessor (getter) and mutator (setter) methods then I suggest you study up on them, because they are a basic element of encapsulation that you should know.
ok so i studied abit of the accessor and mutator and here's what i got:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ChickenListener implements ActionListener {
int loopctrl;
static Connection cn;
static Statement st;
static ResultSet rs;
static PreparedStatement ps;
String sql = "";
String url = "jdbc:mysql://localhost/";
String dbName = "restaurant";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String password = "";
String errorCode;
FriedChicken parent;
public ChickenListener (FriedChicken parent) {
this.parent = parent;
}
public void actionPerformed (ActionEvent ae) {
String tempString;
Object event = ae.getSource ();
for (loopctrl = 0; loopctrl <8; loopctrl++) {
if (event == parent.choose[loopctrl]) {
tempString = parent.choose[loopctrl].getText();
new QuantityCounter ();
Holder hld = new Holder (tempString);
try {
Class.forName (driver).newInstance ();
cn = DriverManager.getConnection (url + dbName, username, password);
try {
st = cn.createStatement ();
ps = cn.prepareStatement ("INSERT INTO corder VALUES (?,?,?)");
ps.setString (1,tempString);
ps.setInt (2,0); //quantity
ps.setString (3,"1999-01-01");//date yyyy-mm-dd
ps.executeUpdate ();
}
catch (SQLException ec) {
errorCode = ("Error code: " + ec);
}
}
catch (Exception ec) {
errorCode = ("Error code: " + ec);
}
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class QuantityCounterListener implements ActionListener {
String tempStrng;
int tempInt;
int x = 0;
static Connection cn;
static Statement st;
static ResultSet rs;
static PreparedStatement ps;
String sql = "";
String url = "jdbc:mysql://localhost/";
String dbName = "restaurant";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String password = "";
String errorCode;
QuantityCounter parent;
public QuantityCounterListener (QuantityCounter parent) {
this.parent = parent;
}
public void actionPerformed (ActionEvent ae) {
Object event = ae.getSource ();
Holder hld = new Holder ();
if (event == parent.okBtn) {
tempStrng = parent.quanFld.getText ();
tempInt = Integer.parseInt (tempStrng);
try {
Class.forName (driver).newInstance ();
cn = DriverManager.getConnection (url + dbName, username, password);
try {
st = cn.createStatement ();
ps = cn.prepareStatement ("UPDATE corder SET quant = ? WHERE oid = ?");
ps.setInt (1,tempInt);
ps.setString (2,(new Holder.getHold())); //quantity
ps.executeUpdate ();
}
catch (SQLException ec) {
errorCode = ("Error code: " + ec);
}
}
catch (Exception ec) {
errorCode = ("Error code: " + ec);
}
parent.frame.setVisible (false);
}
}
}
here is the "holder" class
public class Holder {
public String tempString;
public Holder (String tempString) {
this.tempString = tempString;
}
public String getHold () {
return tempString;
}
}
but i dont know what am i doing wrong
it doesnt work