Skip to main content

Help! how to insert data into database through jtextfield

1 reply [Last post]
salmang
Offline
Joined: 2012-07-12
Points: 0

I need help from any one who can assist me in correcting the code below to enable insert data into database through jtextfield. When I ran it, it displayed error in submitting data

package staffsalary;

import java.awt.*;
import java.awt.Color;
import javax.swing.*;
import java .awt.event.*;
import java.sql.*;
import java.text.DecimalFormat;
class Staffsalary extends JFrame

{
DecimalFormat naira = new DecimalFormat("###,##0.00");

JTextField sureNameTxt= new JTextField(10);
JLabel surel = new JLabel("Sure name");

JTextField nameTxt = new JTextField(10);
JLabel naml = new JLabel("First name");

JTextField deptTxt = new JTextField(10);
JLabel deptl = new JLabel("Department");

JComboBox state = new JComboBox();
JLabel statel = new JLabel("State");

JTextField fileTxt = new JTextField(5);
JLabel filel = new JLabel("File number");

JTextField glTxt = new JTextField(3);
JLabel gll = new JLabel("Grade level");

JTextField acctTxt = new JTextField(10);
JLabel acctl = new JLabel("Account\n number");

JTextField bankTxt = new JTextField(10);
JLabel bankl = new JLabel("Bank");

JTextField emlTxt = new JTextField(10);
JLabel emll = new JLabel("Total emolument");

JComboBox sex = new JComboBox();
JLabel sexl = new JLabel("Marital status");

JLabel dedl = new JLabel("Deductions");

JTextField taxTxt = new JTextField(7);
JLabel taxl = new JLabel("PAYE");

JTextField duesTxt = new JTextField(7);
JLabel duesl = new JLabel("Union dues");

JTextField swaTxt = new JTextField(7);
JLabel swal = new JLabel("SWA");

JTextField nhsTxt = new JTextField(7);
JLabel nhsl = new JLabel("NHS");

JTextField penTxt = new JTextField(7);
JLabel penl = new JLabel("Pension");

JTextField totTxt = new JTextField(10);
JLabel totl = new JLabel("Total deduction");

JTextField netTxt = new JTextField(10);
JLabel netl = new JLabel("Net salary");

JLabel bit = new JLabel("Bio Data");

JButton salbtn = new JButton("Compute salary");
JButton resetbtn = new JButton("Reset");

public static void main(String[]args)
{
System.out.printf("%7s%12s%15s%22s%20s%18s%18s\n","File number","Sure name", "Name","Bank",
"Gross pay","Total deduction","Net pay");
Staffsalary jf = new Staffsalary();
}
public Staffsalary()
{
setLayout(new FlowLayout());
setSize(370,500);
setTitle("NPC STAFF SALARY PAY ROLL");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.PINK);
JPanel bio = new JPanel();
bio.add(bit);
add("North", bio);

JPanel top = new JPanel();
top.setLayout(new FlowLayout());
top.add(surel);
top.add(sureNameTxt);
top.add(naml);
top.add(nameTxt);
sureNameTxt.setEditable(true);
nameTxt.setEditable(true);
add("West", top);

JPanel side = new JPanel();
side.setLayout(new FlowLayout());
side.add(deptl);
side.add(deptTxt);
side.add(statel);

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");
side.add(state);
deptTxt.setEditable(true);
add("West", side);

JPanel low = new JPanel();
low.setLayout(new FlowLayout());
low.add(filel);
low.add(fileTxt);
low.add(gll);
low.add(glTxt);
fileTxt.setEditable(true);
glTxt.setEditable(true);
add("West", low);

JPanel rigth = new JPanel();
rigth.setLayout(new FlowLayout());
rigth.add(acctl);
rigth.add(acctTxt);
rigth.add(bankl);
rigth.add(bankTxt);
add("West", rigth);
JPanel left = new JPanel();
left.setLayout(new FlowLayout());
left.add(emll);
left.add(emlTxt);
add("West", left);

JPanel back = new JPanel();
back.setLayout(new FlowLayout());
sex.addItem("Married");
sex.addItem("Single");
sex.addItem("Seperated");
sex.addItem("Divorce");
sex.addItem("Widow");
back.add(sex);
add("West", back);

JPanel front = new JPanel();
front.setLayout(new FlowLayout());
front.add(dedl);
add("Center", front);

JPanel centre = new JPanel();
centre.setLayout(new FlowLayout());
centre.add(taxl);
centre.add(taxTxt);
centre.add(duesl);
centre.add(duesTxt);
add("West", centre);

JPanel act = new JPanel();
act.setLayout(new FlowLayout());
act.add(swal);
act.add(swaTxt);
act.add(nhsl);
act.add(nhsTxt);
add("West", act);

JPanel sat = new JPanel();
sat.setLayout(new FlowLayout());
sat.add(penl);
sat.add(penTxt);
add("West", sat);

JPanel middle = new JPanel();
middle.setLayout(new FlowLayout());
middle.add(totl);
middle.add(totTxt);
totTxt.setEditable(false);
add("West", middle);

JPanel wide = new JPanel();
wide.setLayout(new FlowLayout());
wide.add(netl);
wide.add(netTxt);
netTxt.setEditable(false);
add("West", wide);

final JPanel min = new JPanel();
min.setLayout(new FlowLayout());
min.add(salbtn);
min.add(resetbtn);
add("West", min);
resetbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

sureNameTxt.setText(" ");
nameTxt.setText(" ");
deptTxt.setText(" ");
fileTxt.setText(" ");
bankTxt.setText(" ");
acctTxt.setText(" ");
emlTxt.setText(" ");
taxTxt.setText(" ");
duesTxt.setText(" ");
swaTxt.setText(" ");
nhsTxt.setText(" ");
penTxt.setText(" ");
totTxt.setText(" ");
netTxt.setText(" ");
glTxt.setText(" ");
}
});
salbtn.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent er)
{
double tamount = calculateSalary();
double ttamt = tamount;
taxTxt.setText("" + naira.format(ttamt * 0.026398));
duesTxt.setText("" + naira.format(ttamt * 0.00771730));
double swat = 0;
nhsTxt.setText("" + naira.format(ttamt * 0.0115767));
penTxt.setText("" + naira.format(ttamt * 0.05636));
emlTxt.setText("" +naira.format(ttamt));
double totalDeduction = (ttamt * 0.026398 )+ (ttamt * 0.00771730) +
(ttamt * 0.0115767) + (ttamt * 0.05636 ) + swat;
double netPay = (ttamt - totalDeduction);
totTxt.setText(" " + naira.format(totalDeduction));
netTxt.setText(" " + naira.format(netPay));
String due = duesTxt.getText();
String nhs = nhsTxt.getText();
String pen = penTxt.getText();
String eml = emlTxt.getText();
String tot = totTxt.getText();
String net = netTxt.getText();
String tx = taxTxt.getText();
String sureName = sureNameTxt.getText();
String name = nameTxt.getText();
String acc = acctTxt.getText();
String bnk = bankTxt.getText();
String file = fileTxt.getText();
String dep = deptTxt.getText();
String url = "jdbc:mysql://localhost:3306/salary";
String user = "admin";
String pass = "";

System.out.printf( "\n%s" ,file + " " +sureName + " "+
name + " " + dep +" " + bnk + " " +
naira.format(ttamt ) +
" "+ naira.format(totalDeduction) + " " +
" " +naira.format( netPay)+" "+acc) ;
try{
Connection con = DriverManager.getConnection(url, user, pass);
PreparedStatement st=con.prepareStatement("insert into staffs(`file_name`," +
"`sure_name`,`first_name`,`department`,`payee`,`union_dues`,`nhs`,`pension`," +
"`total_emolument`,`total_deduction`,`net_salary`,`bank_name`,`accountNumber`" +
") values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
st.setString(1, file);
st.setString(2,sureName);
st.setString(3, name);
st.setString(4, dep);
st.setString(5, tx);
st.setString(6, due);
st.setString(7, nhs);
st.setString(8, pen);
st.setString(9, eml);
st.setString(10, tot);
st.setString(11, net);
st.setString(12, bnk);
st.setString(13, acc);
st.executeUpdate();
JOptionPane.showMessageDialog(null, "Data is successfully inserted into database");
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "error in submitting data");
}
}
});

setResizable(true);
setVisible(true);
}
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) * 4469);

return amount;

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

return amount;

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

return amount;

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

return amount;

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

return amount;

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

return amount;

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

return amount;

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

return amount;

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

return amount;

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

return amount;

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

return amount;

case 13:
amount = 96952.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) * 1313.92);

return amount;

default :
amount = 0;

return amount;
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kauedb
Offline
Joined: 2009-02-10
Points: 0

Can you post the error message?