473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HELP problems linking an applet form to a Database

Ok, here i have the code for the form...but whenever i click update, delete or search all it does is close the form and nothing is done to the database. Could someone be kind enough to help me out here...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.net.*;
import java.util.*;

public class EmpForm extends JFrame
{
private JLabel lbl_empname, lbl_empid, lbl_emptrn, lbl_bsalary, lbl_tax, lbl_nsalary, lbl_qualification, lbl_gender;
private JTextField txt_empname, txt_empid, txt_emptrn, txt_bsalary, txt_tax, txt_nsalary;
private JRadioButton Female,Male;
private JCheckBox cbox_masters,cbox_bachelors,cbox_diploma;
private JButton btn_search, btn_update, btn_delete;
private JPanel topPanel, radioPanel, cboxPanel, buttonPanel;
private String genderVal, qualVal;

public EmpForm()
{
super("Employee information");
Container container=getContentPane();
container.setLayout(new FlowLayout());

JPanel topPanel=new JPanel(new GridLayout(6,2));;
JPanel radioPanel=new JPanel(new GridLayout(1,3));
JPanel cboxPanel=new JPanel(new GridLayout(1,4));
JPanel buttonPanel=new JPanel(new GridLayout(1,4));

lbl_empname= new JLabel("Employee Name");
txt_empname = new JTextField(15);
lbl_empid= new JLabel("Employee ID");
txt_empid = new JTextField(15);
lbl_emptrn= new JLabel("TRN");
txt_emptrn = new JTextField(15);
lbl_bsalary= new JLabel("Basic Salary");
txt_bsalary = new JTextField(15);
lbl_tax= new JLabel("Tax");
txt_tax = new JTextField(15);
lbl_nsalary= new JLabel("Net Salary");
txt_nsalary = new JTextField(15);
lbl_gender=new JLabel("Gender");

GHandler gendhandler=new GHandler();
Male=new JRadioButton("Male");
Female=new JRadioButton("Female");
Male.addItemListener(gendhandler);
Female.addItemListener(gendhandler);
ButtonGroup radgroup= new ButtonGroup();
radgroup.add(Male);
radgroup.add(Female);

QHandler qualhandler=new QHandler();
lbl_qualification= new JLabel("Qualification:");
cbox_bachelors= new JCheckBox("Bachelors");
cbox_diploma= new JCheckBox("Diploma");
cbox_masters= new JCheckBox("Masters");
cbox_bachelors.addItemListener(qualhandler);
cbox_diploma.addItemListener(qualhandler);
cbox_masters.addItemListener(qualhandler);

btn_search=new JButton("Search");
btn_update=new JButton("Update");
btn_delete=new JButton("Delete");

BHandler btnhandler=new BHandler();
btn_search.addActionListener(btnhandler);
btn_update.addActionListener(btnhandler);
btn_delete.addActionListener(btnhandler);

topPanel.add(lbl_empname);
topPanel.add(txt_empname);
topPanel.add(lbl_empid);
topPanel.add(txt_empid);
topPanel.add(lbl_emptrn);
topPanel.add(txt_emptrn);
topPanel.add(lbl_bsalary);
topPanel.add(txt_bsalary);
topPanel.add(lbl_tax);
topPanel.add(txt_tax);
topPanel.add(lbl_nsalary);
topPanel.add(txt_nsalary);

radioPanel.add(lbl_gender);
radioPanel.add(Male);
radioPanel.add(Female);

cboxPanel.add(lbl_qualification);
cboxPanel.add(cbox_bachelors);
cboxPanel.add(cbox_diploma);
cboxPanel.add(cbox_masters);

buttonPanel.add(btn_search);
buttonPanel.add(btn_update);
buttonPanel.add(btn_delete);

container.add(topPanel);
container.add(radioPanel);
container.add(cboxPanel);
container.add(buttonPanel);

setSize(400, 400);
setVisible(true);

}

public class GHandler implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if(event.getSource()==Male)
{
genderVal="Male";
}
else if(event.getSource()==Female)
{
genderVal="Female";
}
}
}

public class QHandler implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if(event.getSource()==cbox_bachelors)
{
if(event.getStateChange()==ItemEvent.SELECTED)
{
qualVal+="Bachelors";
}
}
else if(event.getSource()==cbox_diploma)
{
if(event.getStateChange()==ItemEvent.SELECTED)
{
qualVal+="Diploma";
}
}
else if(event.getSource()==cbox_masters)
{
if(event.getStateChange()==ItemEvent.SELECTED)
{
qualVal+="Masters";
}
}
}
}

public class BHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try
{
Connection conn;
PreparedStatement searchQuery, updateQuery, deleteQuery;
ResultSet rs;
String qualification="";
String gender="";
String dbUrl="jdbc:odbc:employee";
String user="";
String password="";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(dbUrl,user,password);

if(event.getSource()==btn_search)
{

searchQuery=conn.prepareStatement("SELECT * FROM emptable WHERE Emp_id=?");
searchQuery.setInt(1, Integer.parseInt(txt_empid.getText()));
rs=searchQuery.executeQuery();
while(rs.next()){
txt_empname.setText(rs.getString(1));
txt_emptrn.setText(""+rs.getInt(3)+"");
txt_bsalary.setText(""+rs.getInt(4)+"");
txt_tax.setText(""+rs.getInt(5)+"");
txt_nsalary.setText(""+rs.getInt(6)+"");
gender=rs.getString(7);
qualification=rs.getString(8);}

if(gender.equals("Male"))
{
Male.setSelected(true);
}
else if(gender.equals("Female"))
{
Female.setSelected(true);
}

if(qualification.regionMatches(0, "Bachelors", 1, qualification.length())==true)
{
cbox_bachelors.setSelected(true);
}
if(qualification.regionMatches(0, "Diploma", 1, qualification.length())==true)
{
cbox_diploma.setSelected(true);
}
if(qualification.regionMatches(0, "Masters", 1, qualification.length())==true)
{
cbox_masters.setSelected(true);
}

searchQuery.close();
}
else if(event.getSource()==btn_update)
{
updateQuery=conn.prepareStatement("UPDATE emptable SET Empname=?, Emp_id=?, TRN=?, Basic_Salary=?, Tax=?, Net_Salary=?, Gender=?, Qualification=? WHERE Emp_id=?");

updateQuery.setString(1, txt_empname.getText());
updateQuery.setInt(2, Integer.parseInt(txt_empid.getText()));
updateQuery.setInt(3, Integer.parseInt(txt_emptrn.getText()));
updateQuery.setInt(4, Integer.parseInt(txt_bsalary.getText()));
updateQuery.setInt(5, Integer.parseInt(txt_tax.getText()));
updateQuery.setInt(6, Integer.parseInt(txt_nsalary.getText()));
updateQuery.setString(7, genderVal);
updateQuery.setString(8, qualVal);
updateQuery.setInt(9, Integer.parseInt(txt_empid.getText()));
updateQuery.executeUpdate();
updateQuery.close();
JOptionPane.showMessageDialog(null, "update is successful");
genderVal="";
}
else if(event.getSource()==btn_delete)
{
deleteQuery=conn.prepareStatement("DELETE FROM emptable WHERE Emp_id=?");
deleteQuery.setInt(1, Integer.parseInt(txt_empid.getText()));
deleteQuery.executeUpdate();
deleteQuery.close();
JOptionPane.showMessageDialog(null, "Record deleted successfully");
}

conn.close();

}

catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
System.exit(1);
}

finally
{

}

}
}

public static void main(String[] args)
{
EmpForm empframe=new EmpForm();
empframe.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
}
}
Apr 27 '07 #1
0 2221

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: david | last post by:
Hello all, let's me explains my long problem: I'm doing an intranet with a media part. So im must be able to upload files on a ftp server AND have a record of informations about this file and...
8
by: DKM | last post by:
Here are the source code files to a Java applet that utilizes LiveConnect to communicate with Javascript, and the HTML file. The thing works both in IE 6.0 and FireFox 1.4. but with some...
2
by: iforsyth | last post by:
I have an ASP.NET application where end users essentially can do database inquiries and minor updates. There are times; however, when the end users have slow internet connections or no internet...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
2
by: revansx | last post by:
Dear JavaScript experts, Please give me some advice on the following situation: I am attempting to develop a completely web-based set of tools that can match the functionality of a set of...
3
by: revansx | last post by:
Folks, I aplogize for posting a second time. I am surprised at what little has be said about this issue. Is there another list that I should post this to?...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: Richard Maher | last post by:
Hi, I am in a mouseup event for button A and I'd like to disable=false button B before starting some work. Is there anyway that an event for button B can then fire before my event processing for...
3
by: ARC | last post by:
I'm having trouble here with Access 2007 and connecting to a different database. The code below works great IF the previous back-end database connection is still present and you are trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.