Skip to main content

Updating ms database records

1 reply [Last post]
mozum
Offline
Joined: 2012-05-01

Hello good teacher. Pls help me once more with this one. I cannot update ms access database. My attempt always end up in error message that "mismatched data type". What have done wrong.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jamesharte
Offline
Joined: 2012-09-28

Please paste your class listing, with line numbers, for the code which you are running. Please also provide the table schema for the table(s) which you are trying to access. Please provide the full error message message which should identify the line number on which the error is occurring. From that you should be able to identify which field you are having issues with.

mozum
Offline
Joined: 2012-05-01

Thank you for showing concern to my problem. Find below complete program

package access;

import javax.swing.*;
import java .awt.event.*;
import java.sql.*;
import java.util.Date;
import java.awt.Color;
import java.text.DecimalFormat;
class Access extends JFrame
{
DecimalFormat naira=new DecimalFormat("###,##0.00");
DecimalFormat digit=new DecimalFormat("00");
JFrame f;
JPanel p0,p1,p2,p3;
JTabbedPane tp;
ImageIcon btnimg1,btnimg2;
JLabel fileLab, sureLab, firstLab, depLab,stateLab2,fileLab2,l6,fileLab3,sureLab2,firstLab2,depLab2,stateLab
,accLab,bankLab,emoLab,sexLab,payeeLab,unionLab,swaLab,nhfLab,penLab,dedLab,netLab,
accLab2,bankLab2,emoLab2,sexLab2,payeeLab2,unionLab2,swaLab2,nhfLab2,penLab2,dedLab2,netLab2,datel,datela;
JTextField file,sure,first,dep,file2,file3,sure2,first2,dep2,acc,bank,emo,payee,union,swa,nhf,pen,ded,net,tf20,
acc2,bank2,emo2,payee2,union2,swa2,nhf2,pen2,ded2,net2,date,datea;
JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn,calc,comp ;
JComboBox state,sex,state2,sex2;

public Access()
{
f=new JFrame("Staff Payroll");
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
tp=new JTabbedPane();
btnimg1=new ImageIcon();
btnimg2=new ImageIcon();

fileLab=new JLabel("File Number: ");
sureLab=new JLabel("Sure Name:");
firstLab=new JLabel("First Name:");
depLab=new JLabel("Department:");
stateLab = new JLabel(" State");
fileLab2=new JLabel("File Number to delete record:");
fileLab3=new JLabel("File Number: ");
sureLab2=new JLabel("Sure Name:");
firstLab2=new JLabel("First Name:");
depLab2=new JLabel("Department:");
stateLab2=new JLabel(" State");
accLab=new JLabel("Account Number");
bankLab=new JLabel(" Bank Name");
emoLab=new JLabel("Total Emolument");
sexLab = new JLabel(" Marital status");
payeeLab=new JLabel("Payee");
unionLab=new JLabel(" Union Dues");
swaLab=new JLabel("SWA");
nhfLab=new JLabel(" NHF ");
penLab=new JLabel("Pension");
dedLab=new JLabel(" Total Deduction");
netLab=new JLabel("Net Salary");
accLab2=new JLabel("Account Number");
bankLab2=new JLabel(" Bank Name");
emoLab2=new JLabel("Total Emolument");
sexLab2=new JLabel(" Marital Status");
payeeLab2=new JLabel("Payee");
unionLab2=new JLabel(" Union Dues");
swaLab2=new JLabel("SWA");
nhfLab2=new JLabel(" NHF ");
penLab2=new JLabel("Pension");
dedLab2=new JLabel(" Total Deduction");
netLab2=new JLabel("Net Salary");
datel=new JLabel(" Date");
datela=new JLabel(" Date");

file=new JTextField(5);
sure=new JTextField(12);
first=new JTextField(12);
dep=new JTextField(12);
state=new JComboBox();
acc=new JTextField(12);
bank=new JTextField(12);
emo=new JTextField(12);
sex=new JComboBox();
payee=new JTextField(12);
union=new JTextField(12);
swa=new JTextField(12);
nhf=new JTextField(12);
pen=new JTextField(12);
ded=new JTextField(12);
net=new JTextField(12);
date=new JTextField(10);
datea=new JTextField(10);

file2=new JTextField(5);
first2=new JTextField(12);
sure2=new JTextField(12);
dep2=new JTextField(12);
state2=new JComboBox();
acc2=new JTextField(12);
bank2=new JTextField(12);
emo2=new JTextField(12);
sex2=new JComboBox();
payee2=new JTextField(12);
union2=new JTextField(12);
swa2=new JTextField(12);
nhf2=new JTextField(12);
pen2=new JTextField(12);
ded2=new JTextField(12);
net2=new JTextField(12);
file3=new JTextField(12);

savebtn=new JButton(" Add ");
resetbtn=new JButton(" Reset");
editbtn1=new JButton(" View Record ");
editbtn2=new JButton(" Save");
deletebtn=new JButton("Delete");
calc=new JButton("Compute Salary");
comp=new JButton("Re-Compute Salary");

p1.setBackground(Color.PINK);
p1.add(fileLab);
p1.add(file);
p1.add(sureLab);
p1.add(sure);
p1.add(firstLab);
p1.add(first);
p1.add(depLab);
p1.add(dep);
p1.add(stateLab);
state.addItem("Abia");
state.addItem("Adamawa");
state.addItem("Akwa Ibom");
state.addItem("Anambra");
state.addItem("Bauchi");
state.addItem("Bayelsa");
state.addItem("Benue");
state.addItem("Borno");
state.addItem("CrossRiver");
state.addItem("Delta");
state.addItem("Ebonyi");
state.addItem("Edo");
state.addItem("Ekiti");
state.addItem("Enugu");
state.addItem("Gombe");
state.addItem("Imo");
state.addItem("Jigawa");
state.addItem("Kaduna");
state.addItem("Kano");
state.addItem("Katsina");
state.addItem("Kebbi");
state.addItem("Kogi");
state.addItem("Kwara");
state.addItem("Lagos");
state.addItem("Nassarawa");
state.addItem("Niger");
state.addItem("Ogun");
state.addItem("Ondo");
state.addItem("Osun");
state.addItem("Oyo");
state.addItem("Rivers");
state.addItem("Sokoto");
state.addItem("Taraba");
state.addItem("Yobe");
state.addItem("Zamfara");
state.addItem("FCT");
p1.add(state);
p1.add(accLab);
p1.add(acc);
p1.add(bankLab);
p1.add(bank);
p1.add(emoLab);
p1.add(emo);
p1.add(sexLab);
sex.addItem("Single");
sex.addItem("Married");
sex.addItem("Widow");
sex.addItem("Seperated");
sex.addItem("Divorce");
p1.add(sex);
p1.add(payeeLab);
p1.add(payee);
p1.add(unionLab);
p1.add(union);
p1.add(swaLab);
p1.add(swa);
p1.add(nhfLab);
p1.add(nhf);
p1.add(penLab);
p1.add(pen);
p1.add(dedLab);
p1.add(ded);
p1.add(netLab);
p1.add(net);
p1.add(datel);
p1.add(date);
p1.add(calc);
p1.add(savebtn);
p1.add(resetbtn);

p2.setBackground(Color.PINK);
p2.add(fileLab2);
p2.add(file2);
p2.add(sureLab2);
p2.add(sure2);
p2.add(firstLab2);
p2.add(first2);
p2.add(depLab2);
p2.add(dep2);
p2.add(stateLab2);
state2.addItem("Abia");
state2.addItem("Adamawa");
state2.addItem("Akwa Ibom");
state2.addItem("Anambra");
state2.addItem("Bauchi");
state2.addItem("Bayelsa");
state2.addItem("Benue");
state2.addItem("Borno");
state2.addItem("CrossRiver");
state2.addItem("Delta");
state2.addItem("Ebonyi");
state2.addItem("Edo");
state2.addItem("Ekiti");
state2.addItem("Enugu");
state2.addItem("Gombe");
state2.addItem("Imo");
state2.addItem("Jigawa");
state2.addItem("Kaduna");
state2.addItem("Kano");
state2.addItem("Katsina");
state2.addItem("Kebbi");
state2.addItem("Kogi");
state2.addItem("Kwara");
state2.addItem("Lagos");
state2.addItem("Nassarawa");
state2.addItem("Niger");
state2.addItem("Ogun");
state2.addItem("Ondo");
state2.addItem("Osun");
state2.addItem("Oyo");
state2.addItem("Rivers");
state2.addItem("Sokoto");
state2.addItem("Taraba");
state2.addItem("Yobe");
state2.addItem("Zamfara");
state2.addItem("FCT");
p2.add(state2);
p2.add(accLab2);
p2.add(acc2);
p2.add(bankLab2);
p2.add(bank2);
p2.add(emoLab2);
p2.add(emo2);
p2.add(sexLab2);
sex2.addItem("Single");
sex2.addItem("Married");
sex2.addItem("Widow");
sex2.addItem("Seperated");
sex2.addItem("Divorce");
p2.add(sex2);
p2.add(payeeLab2);
p2.add(payee2);
p2.add(unionLab2);
p2.add(union2);
p2.add(swaLab2);
p2.add(swa2);
p2.add(nhfLab2);
p2.add(nhf2);
p2.add(penLab2);
p2.add(pen2);
p2.add(dedLab2);
p2.add(ded2);
p2.add(netLab2);
p2.add(net2);
p2.add(datela);
p2.add(datea);
p2.add(editbtn1);
p2.add(editbtn2);
p2.add(comp);

p3.setBackground(Color.PINK);
p3.add(fileLab3);
p3.add(file3);
p3.add(deletebtn);
resetbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
file.setText("");
sure.setText("");
first.setText("");
dep.setText("");
acc.setText("");
bank.setText("");
emo.setText("");
payee.setText("");
union.setText("");
swa.setText("");
nhf.setText("");
pen.setText("");
ded.setText("");
net.setText("");
date.setText("");
}
});
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int dates = dated();
int mont = months();
int dayy = days();
double tamount = calculateSalary();
double ttamt = tamount;
emo.setText("" +naira.format(ttamt));
payee.setText("" + naira.format(ttamt * 0.027449943));
union.setText("" + naira.format(ttamt * 0.00778990));

nhf.setText("" + naira.format(ttamt * 0.02499998));
pen.setText("" + naira.format(ttamt * 0.074999809));
double totalDeduction = (ttamt * 0.033449943 )+ (ttamt * 0.00778990) +
(ttamt * 0.02499998) + (ttamt * 0.07499999 ) ;
double netPay = (ttamt - totalDeduction);
ded.setText(" " + naira.format(totalDeduction));
net.setText(" " + naira.format(netPay));
date.setText(""+digit.format(dayy)+"/"+digit.format(mont)+"/"+(dates));
}
});
savebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){

String val1 = file.getText();
String val2 = sure.getText();
String val3 = first.getText();
String val4 = dep.getText();
String val5 = acc.getText();
String val6 = bank.getText();
String val7 = emo.getText();
String val8 = payee.getText();
String val9 = union.getText();
String val10 = swa.getText();
String val11 = nhf.getText();
String val12 = pen.getText();
String val13 = ded.getText();
String val14 = net.getText();
String host = "jdbc:odbc:Database";
try{
Connection con=DriverManager.getConnection(host);
PreparedStatement st = con.prepareStatement("INSERT INTO payment(file_number," +
"last_name,first_name,department,bank_account,bank_name,total_emolument" +
",payee,union_dues,swa,nhf,pension,total_deduction,net_pay)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
st.setString(1, val1);
st.setString(2, val2);
st.setString(3, val3);
st.setString(4, val4);
st.setString(5, val5);
st.setString(6, val6);
st.setString(7, val7);
st.setString(8, val8);
st.setString(9, val9);
st.setString(10, val10);
st.setString(11, val11);
st.setString(12, val12);
st.setString(13, val13);
st.setString(14, val14);
st.executeUpdate();

JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(p1,"Error in submitting Data"+"\n"+ e.getMessage());

}
}
});
deletebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String val1=file3.getText();
String host = "jdbc:odbc:Database";
String user = "admin";
String pass = "";
try{
int s=JOptionPane.showConfirmDialog(p3," Are sure you want to delete?\n Record will be deleted permanently");
if(s==0){
Connection con = DriverManager.getConnection(host, user, pass);
PreparedStatement st=con.prepareStatement("DELETE FROM payment WHERE file_number = ?");
st.setString(1,val1);
st.executeUpdate();
JOptionPane.showMessageDialog(p3,"Record is deleted successfully.");
con.close();
}
}
catch(Exception exp3){
JOptionPane.showMessageDialog(p3,"Error in deleting record."+"\n"+ exp3.getMessage());
System.out.println(exp3.getMessage());
}
}
});
editbtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String value=file2.getText();

String host = "jdbc:odbc:Database";
String user = "admin";
String pass = "";
try{

Connection con = DriverManager.getConnection(host, user, pass);
PreparedStatement st=con.prepareStatement("select * from payment where file_number=?");
st.setString(1,value);
ResultSet res=st.executeQuery();
res.next();
file2.setText(res.getString(1));
sure2.setText(res.getString(2));
first2.setText(res.getString(3));
dep2.setText(res.getString(4));
acc2.setText(res.getString(5));
bank2.setText(res.getString(6));
emo2.setText(res.getString(7));
payee2.setText(res.getString(8));
union2.setText(res.getString(10));
swa2.setText(res.getString(9));
nhf2.setText(res.getString(11));
pen2.setText(res.getString(12));
ded2.setText(res.getString(13));
net2.setText(res.getString(14));

con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(p2,"Record not found. \n make sure you type the Correct File Number"
+"\n"+e.getMessage());

}
}
});
comp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
double tamount = calculateSalary();
int dates = dated();
int mont = months();
int dayy = days();
double ttamt = tamount;
emo2.setText("" +naira.format(ttamt));
payee2.setText("" + naira.format(ttamt *0.027449943));
union2.setText("" + naira.format(ttamt * 0.00778990));
swa2.setText(""+ naira.format(0));
nhf2.setText("" + naira.format(ttamt * 0.02499998));
pen2.setText("" + naira.format(ttamt * 0.074999809));
double totalDeduction = (ttamt * 0.027449943 )+ (ttamt * 0.00778990) +
(ttamt * 0.02499998) + (ttamt * 0.074999809) + (0);
double netPay = (ttamt - totalDeduction);
ded2.setText(" " + naira.format(totalDeduction));
net2.setText(" " + naira.format(netPay));
datea.setText(""+digit.format(dayy)+"/"+digit.format(mont)+"/"+(dates));
String vall1 = emo2.getText();
String vall2 = payee2.getText();
String vall3 = union2.getText();
String vall4 = swa2.getText();
String vall5 = nhf2.getText();
String vall6 = pen2.getText();
String vall7 = ded2.getText();
String vall8 = net2.getText();
String host = "jdbc:odbc:Database";

try{
Connection con = DriverManager.getConnection(host);
PreparedStatement st = con.prepareStatement("INSERT INTO payment(total_emolument,payee," +
"union_dues,swa,nhf,pension,total_deduction,net_pay) values(?,?,?,?,?,?,?,?)");
st.setString(1, vall1);
st.setString(2, vall2);
st.setString(3, vall3);
st.setString(4, vall4);
st.setString(5, vall5);
st.setString(6, vall6);
st.setString(7, vall7);
st.setString(8, vall8);
st.executeUpdate();
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(p2, e.getMessage());
}
}
});
editbtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String host = "jdbc:odbc:Database";
Connection con = null;
try
{
int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? Selected data will be replaced");
if(x==0){
try{
String value1=file2.getText();
String value2=sure2.getText();
String value3=first2.getText();
String value4=dep2.getText();
String value5=acc2.getText();
String value6=bank2.getText();
String value7=emo2.getText();
String value8=payee2.getText();
String value9=union2.getText();
String value10=swa2.getText();
String value11=nhf2.getText();
String value12=pen2.getText();
String value13=ded2.getText();
String value14=net2.getText();
con = DriverManager.getConnection(host);
Statement st=con.createStatement();
st.executeUpdate("update payment set last_name='"+value2+"', first_name='"+value3+"'," +
"department='"+value4+"',bank_account='"+value5+"',bank_name='"+value6+"'" +
",total_emolument='"+value7+"',payee='"+value8+"'," +
"union_dues='"+value9+"',swa='"+value10+"',nhf='"+value11+"',pension='"
+value12+"',total_deduction='"+value13+"'," +
"net_pay ='"+value14+"' where file_number='"+value1+"'");
JOptionPane.showMessageDialog(p2,"Updated successfully");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error in updating edit fields"+"\n"+ex.getMessage());
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,ex.getMessage());
}
}
});

}
void dis()
{
f.getContentPane().add(tp);
tp.setBackground(Color.CYAN);
tp.addTab("Add Record",p1);
tp.addTab("Edit Record",p2);
tp.addTab("Delete Record",p3);

f.getContentPane().setBackground(Color.MAGENTA);
f.setSize(500,350);
f.setVisible(true);
f.setResizable(true);
}
public static void main(String z[]){
Access pro=new Access();
pro.dis();
}
public int dated()
{
Date now = new Date();
int year = now.getYear()+ 1900;
return year;
}
public int months(){
Date now = new Date();
int month = now.getMonth()+ 1;
return month;
}
public int days(){
Date now = new Date();
int day = now.getDate();
return day;
}
public double calculateSalary()
{

String gdrStr = JOptionPane.showInputDialog("Enter grade level");
String stepStr = JOptionPane.showInputDialog("Enter step");
int gdr = Integer.parseInt(gdrStr);
int step = Integer.parseInt(stepStr);
double amount;

switch (gdr)

{
case 1:
amount = 17073.17 + ((step - 1) * 446.9);

return amount;

case 2:
amount = 17350.50 + ((step - 1) * 486.92);

return amount;

case 3:
amount = 19414.5 + ((step - 1) * 598.50);

return amount;

case 4:
amount = 20249.2 + ((step- 1) * 719.08);

return amount;

case 5:
amount = 21775.13 + ((step - 1) * 835.33);

return amount;

case 6:
amount = 27025.72 + ((step - 1) * 950.92);

return amount;

case 7:
amount = 43164.93 + ((step - 1) * 1562.33);

return amount;

case 8:
amount = 55515.25 + ((step - 1) * 1860);

return amount;

case 9:
amount = 65054.83 + ((step - 1) * 2214.50);

return amount;

case 10:
amount = 76206.33 + ((step - 1) * 2435.75);

return amount;

case 12:
amount = 87767.06 + ((step - 1) * 3777.42);

return amount;

case 13:
amount = 97852.75 + ((step - 1) * 3993.50);

return amount;

case 14:
amount = 107084.83 + ((step - 1) * 4299.25);

return amount;

case 15:
amount = 147318 + ((step - 1) ) * 6088.83;

return amount;

case 16:
amount = 182239.75 + ((step - 1) * 7292.67);

return amount;

case 17:
amount = 347733.33 + ((step - 1) * 13130.92);

return amount;

default :
amount = 0;

return amount;
}
}
}
Also find below the schema for the table
file_number last_name first_name department bank_account bank_name total_emolument payee swa union_dues nhf pension total_deduction net_pay date

The problem begins while in the edit tab, an error message is displayed if attempt to save an updated record. The message is "[Microsoft][ODBC Microsoft Access Driver]Data type mismatch in criteria expression.