Program creating my Java program
Hello. I am currently having hard time in the past 2 weeks trying to develop this software for my assignment; for APTECH. Finally, I fixed all syntax errors and successfully compiled the program.
However, I am still having problem. Despite importing necessary packages for the program, and setting up the ODBC driver (in this case, "1183"; Microsoft Access Driver), the following errors occur:
1.Under "public void actionPerformed(ActionEvent ae)" section, the first four buttons (BFirst, BPrevious, BNext and BLast) continuously consume the computer's virtual memory without results.
2.Under the same section, the following buttons do not function at all: BSave, BUpdate, and BDelete.
What seems to be the problem? I need an expert to look at it asap. You got 24 hours. Thanks.
import javax.swing.*;
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.math.*;
class JavaFormAssignment1183 extends JFrame implements ActionListener
{
JButton BFirst = new JButton("First");
JButton BPrevious = new JButton("Previous");
JButton BNext = new JButton("Next");
JButton BLast = new JButton("Last");
JButton BBeck = new JButton("Beck");
JButton BSave = new JButton("Save");
JButton BUpdate = new JButton("Update");
JButton BDelete = new JButton("Delete");
JButton BReset = new JButton("Reset");
JButton BExit = new JButton("Exit");
JLabel LStaffId = new JLabel("Staff I.D.:");
JLabel LMonthSal = new JLabel("Monthly salary:");
JLabel LXtrHr = new JLabel("Extra hour:");
JLabel LPLoan = new JLabel("Personal loan:");
JLabel LLunMon = new JLabel("Lunch money:");
JLabel LHouLon = new JLabel("House loan:");
JLabel LTaxes = new JLabel("Tax:");
JLabel LTotal = new JLabel("Total:");
JTextField TStaffId = new JTextField(20);
JTextField TMonthSal = new JTextField(20);
JTextField TXtrHr = new JTextField(20);
JTextField TPLoan = new JTextField(20);
JTextField TLunMon = new JTextField(20);
JTextField THouLon = new JTextField(20);
JTextField TTaxes = new JTextField(20);
JTextField TTotal = new JTextField(20);
Statement st;
PreparedStatement pst;
ResultSet rs;
String strSql;
JPanel pnlStart, pnlEnd;
Connection con;
public JavaFormAssignment1183(String SYR)
{
super(SYR);
getContentPane().setLayout(new BorderLayout());
pnlStart = new JPanel();
pnlStart.setLayout(new GridLayout(2,2));
pnlStart.add(LStaffId);
pnlStart.add(TStaffId);
pnlStart.add(LMonthSal);
pnlStart.add(TMonthSal);
pnlStart.add(LXtrHr);
pnlStart.add(TXtrHr);
pnlStart.add(LPLoan);
pnlStart.add(TPLoan);
pnlStart.add(LLunMon);
pnlStart.add(TLunMon);
pnlStart.add(LHouLon);
pnlStart.add(THouLon);
pnlStart.add(LTaxes);
pnlStart.add(TTaxes);
pnlStart.add(LTotal);
pnlStart.add(TTotal);
pnlEnd = new JPanel();
pnlEnd.add(BFirst);
pnlEnd.add(BPrevious);
pnlEnd.add(BNext);
pnlEnd.add(BLast);
pnlEnd.add(BBeck);
pnlEnd.add(BSave);
pnlEnd.add(BUpdate);
pnlEnd.add(BDelete);
pnlEnd.add(BReset);
pnlEnd.add(BExit);
BFirst.addActionListener(this);
BPrevious.addActionListener(this);
BNext.addActionListener(this);
BLast.addActionListener(this);
BBeck.addActionListener(this);
BSave.addActionListener(this);
BUpdate.addActionListener(this);
BDelete.addActionListener(this);
BReset.addActionListener(this);
BExit.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:1183");
}
catch(Exception JerU)
{
JOptionPane.showMessageDialog(this, JerU);
}
getContentPane().add(pnlStart,BorderLayout.PAGE_START);
getContentPane().add(pnlEnd,BorderLayout.PAGE_END);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(600,400);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
try
{
String StaffId = "";
StaffId = TStaffId.getText();
if(ae.getSource().equals(BFirst))
{
try
{
strSql = "Select * from JE where StaffId like ?";
pst = con.prepareStatement(strSql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
StaffId = "%" + StaffId + "%";
pst.setString(1, StaffId);
rs = pst.executeQuery();
while(rs.first())
{
int Slr, Xhr, PLn, LMn, Hln;
Double Txs, TexasFries;
Slr = rs.getInt("MonthlySalary");
Xhr = rs.getInt("ExtraHour");
PLn = rs.getInt("PersonalLoan");
LMn = rs.getInt("LunchMoney");
Hln = rs.getInt("HouseLoan");
Txs = rs.getDouble("Tax");
TexasFries = rs.getDouble("Total");
TMonthSal.setText("" + Slr);
TXtrHr.setText("" + Xhr);
TPLoan.setText("" + PLn);
TLunMon.setText("" + LMn);
THouLon.setText("" + Hln);
TTaxes.setText("" + Txs);
TTotal.setText("" + TexasFries);
}
}
catch(Exception KsA)
{
JOptionPane.showMessageDialog(this, "An error has occured.");
}
}
else if(ae.getSource().equals(BPrevious))
{
try
{
strSql = "Select * from JE where StaffId like ?";
pst = con.prepareStatement(strSql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
StaffId = "%" + StaffId + "%";
pst.setString(1, StaffId);
rs = pst.executeQuery();
while (rs.previous())
{
int Slr, Xhr, PLn, LMn, Hln;
Double Txs, TexasFries;
Slr = rs.getInt("MonthlySalary");
Xhr = rs.getInt("ExtraHour");
PLn = rs.getInt("PersonalLoan");
LMn = rs.getInt("LunchMoney");
Hln = rs.getInt("HouseLoan");
Txs = rs.getDouble("Tax");
TexasFries = rs.getDouble("Total");
TMonthSal.setText("" + Slr);
TXtrHr.setText("" + Xhr);
TPLoan.setText("" + PLn);
TLunMon.setText("" + LMn);
THouLon.setText("" + Hln);
TTaxes.setText("" + Txs);
TTotal.setText("" + TexasFries);
}
}
catch(Exception KsA)
{
JOptionPane.showMessageDialog(this, "An error has occured.");
}
}
else if(ae.getSource().equals(BNext))
{
try
{
strSql = "Select * from JE where StaffId like ?";
pst = con.prepareStatement(strSql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
StaffId = "%" + StaffId + "%";
pst.setString(1, StaffId);
rs = pst.executeQuery();
while (rs.next())
{
int Slr, Xhr, PLn, LMn, Hln;
Double Txs, TexasFries;
Slr = rs.getInt("MonthlySalary");
Xhr = rs.getInt("ExtraHour");
PLn = rs.getInt("PersonalLoan");
LMn = rs.getInt("LunchMoney");
Hln = rs.getInt("HouseLoan");
Txs = rs.getDouble("Tax");
TexasFries = rs.getDouble("Total");
TMonthSal.setText("" + Slr);
TXtrHr.setText("" + Xhr);
TPLoan.setText("" + PLn);
TLunMon.setText("" + LMn);
THouLon.setText("" + Hln);
TTaxes.setText("" + Txs);
TTotal.setText("" + TexasFries);
}
}
catch(Exception KsA)
{
JOptionPane.showMessageDialog(this, "An error has occured.");
}
}
else if(ae.getSource().equals(BLast))
{
try
{
strSql = "Select * from JE where StaffId like ?";
pst = con.prepareStatement(strSql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
StaffId = "%" + StaffId + "%";
pst.setString(1, StaffId);
rs = pst.executeQuery();
while (rs.last())
{
int Slr, Xhr, PLn, LMn, Hln;
Double Txs, TexasFries;
Slr = rs.getInt("MonthlySalary");
Xhr = rs.getInt("ExtraHour");
PLn = rs.getInt("PersonalLoan");
LMn = rs.getInt("LunchMoney");
Hln = rs.getInt("HouseLoan");
Txs = rs.getDouble("Tax");
TexasFries = rs.getDouble("Total");
TMonthSal.setText("" + Slr);
TXtrHr.setText("" + Xhr);
TPLoan.setText("" + PLn);
TLunMon.setText("" + LMn);
THouLon.setText("" + Hln);
TTaxes.setText("" + Txs);
TTotal.setText("" + TexasFries);
}
}
catch(Exception KsA)
{
JOptionPane.showMessageDialog(this, "An error has occured.");
}
}
else if(ae.getSource().equals(BBeck))
{
int Slr=Integer.parseInt(TMonthSal.getText()), Xhr=Integer.parseInt(TXtrHr.getText()), Hln=Integer.parseInt(THouLon.getText()), LMn=Integer.parseInt(TLunMon.getText()), PLn = Integer.parseInt(TPLoan.getText()), b = 3;
Double Txs, TexasFries, a = 0.025;
Txs = Slr * a;
TexasFries = ((Slr - a) + (Xhr * b) + Hln + LMn - PLn);
TTaxes.setText("" + Txs);
TTotal.setText("" + TexasFries);
}
else if(ae.getSource().equals(BSave))
{
try
{
st = con.createStatement();
if ( !TStaffId.equals( "" ) &&
!TMonthSal.equals( "" ) &&
!TPLoan.equals( "" ) &&
!THouLon.equals( "" ) &&
!TLunMon.equals( "" ) &&
!TXtrHr.equals( "" ) &&
!TTaxes.equals( "" ) &&
!TTotal.equals("") )
{
strSql = "INSERT INTO JE VALUES (' " + TStaffId.getText();
strSql = strSql + "', '" + TMonthSal.getText()+" ')";
strSql = strSql + "', '" + TXtrHr.getText()+" ')";
strSql = strSql + "', '" + TPLoan.getText()+" ')";
strSql = strSql + "', '" + TLunMon.getText()+" ')";
strSql = strSql + "', '" + THouLon.getText()+" ')";
strSql = strSql + "', '" + TTaxes.getText()+" ')";
strSql = strSql + "', '" + TTotal.getText()+" ')";
st.executeUpdate(strSql);
JOptionPane.showMessageDialog(this, "Record saved.");
}
}
catch(Exception KsA)
{
JOptionPane.showMessageDialog(this, "Unable to save.");
}
}
else if(ae.getSource().equals(BUpdate))
{
int Slr=Integer.parseInt(TMonthSal.getText()), Xhr=Integer.parseInt(TXtrHr.getText()), Hln=Integer.parseInt(THouLon.getText()), LMn=Integer.parseInt(TLunMon.getText()), PLn = Integer.parseInt(TPLoan.getText());
String Txs= TTaxes.getText(), TexasFries= TTotal.getText();
try
{
strSql = "Update JE set " +
"Slr='" + TMonthSal.getText() +
"', Xhr='" + TXtrHr.getText() +
"', PLn='" + TPLoan.getText() +
"', LMn='" + TLunMon.getText() +
"', Hln ='" + THouLon.getText() +
"', Txs='" + TTaxes.getText() +
"', TexasFries='" + TTotal.getText() +
"', where StaffId='" + TStaffId.getText();
pst = con.prepareStatement(strSql);
pst.setString(1, StaffId);
pst.setInt(2, Slr);
pst.setInt(3, Xhr);
pst.setInt(4, PLn);
pst.setInt(5, LMn);
pst.setInt(6, Hln);
pst.setString(7, Txs);
pst.setString(8, TexasFries);
int result = pst.executeUpdate();
if(result >= 0)
JOptionPane.showMessageDialog(this, "Record updated.");
}
catch(Exception KsA)
{
JOptionPane.showMessageDialog(this, "Unable to update.");
}
}
else if(ae.getSource().equals(BDelete))
{
try
{
strSql = "Delete from JE " + " where StaffId=" + TStaffId.getText();
strSql = strSql+"' or MonthlySalary like ' " + TMonthSal.getText();
strSql = strSql+"' or ExtraHour like ' " + TXtrHr.getText();
strSql = strSql+"' or PersonalLoan like ' " + TPLoan.getText();
strSql = strSql+"' or LunchMoney like ' " + TLunMon.getText();
strSql = strSql+"' or HouseLoan like ' " + THouLon.getText();
strSql = strSql+"' or Tax like ' " + TTaxes.getText();
strSql = strSql+"' or Total like ' " + TTotal.getText()+" ')";
st.executeUpdate(strSql);
JOptionPane.showMessageDialog(this, "Record deleted.");
}
catch(Exception KsA)
{
JOptionPane.showMessageDialog(this, "Unable to delete.");
}
}
else if(ae.getSource().equals(BReset))
{
TStaffId.setText("");
TMonthSal.setText("");
TXtrHr.setText("");
TPLoan.setText("");
TLunMon.setText("");
THouLon.setText("");
TTaxes.setText("");
TTotal.setText("");
}
else if(ae.getSource().equals(BExit))
{
System.exit(0);
}
}
catch(Exception ce)
{
JOptionPane.showMessageDialog(this, ce);
}
}
public static void main(String args[])
{
JavaFormAssignment1183 LebJavaFormAssignment1183 = new JavaFormAssignment1183("Java Form Assignment - Omar Kaj");
}
}

