Java-mysql coding

Java-mysql coding

View Answers

August 20, 2009 at 1:57 PM

Hi Friend,

Try the following code:

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

class EmployeeInformation {
JFrame f;
JPanel p1,p2,p3;
JTabbedPane tp;
ImageIcon btnimg1,btnimg2;
JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10;
JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
JScrollPane sp1;
JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn ;

EmployeeInformation(){
f=new JFrame("Form");
p1=new JPanel(new GridLayout(5,2));
p2=new JPanel(new GridLayout(5,2));
p3=new JPanel(new GridLayout(2,2));
tp=new JTabbedPane();
l1=new JLabel("Employee ID:");
l2=new JLabel("Employee Name:");
l3=new JLabel("Employee Address:");
l4=new JLabel("Salary:");
l5=new JLabel("Enter Employee ID to delete record:");

l7=new JLabel("Employee ID:");
l8=new JLabel("Employee Name:");
l9=new JLabel("Employee Address:");
l10=new JLabel("Salary:");
tf1=new JTextField(12);
tf2=new JTextField(12);
tf3=new JTextField(12);
tf4=new JTextField(12);
tf5=new JTextField(12);
tf6=new JTextField(12);
tf7=new JTextField(12);
tf8=new JTextField(12);
tf9=new JTextField(12);
tf10=new JTextField(12);
savebtn=new JButton(" Add ");
resetbtn=new JButton(" Reset");
editbtn1=new JButton(" Edit ");
editbtn2=new JButton(" Save");
deletebtn=new JButton("Delete");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
p1.add(l4);
p1.add(tf4);
p1.add(savebtn);
p1.add(resetbtn);

p2.add(l7);
p2.add(tf7);
p2.add(l8);
p2.add(tf8);
p2.add(l9);
p2.add(tf9);
p2.add(l10);
p2.add(tf10);
p2.add(editbtn1);
p2.add(editbtn2);

p3.add(l5);
p3.add(tf5);
p3.add(deletebtn);
resetbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
}
});

savebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=tf1.getText();
String value2=tf2.getText();
String value3=tf3.getText();
String value4=tf4.getText();

Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
System.out.println(value1+value2+value3+value4);
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("insert into employee(emp_id,emp_name,emp_address,salary) values(?,?,?,?)");
st.setString(1,value1);
st.setString(2,value2);
st.setString(3,value3);
st.setString(4,value4);
st.executeUpdate();
JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(p1,"Error in submitting data!");
}
}
});

August 20, 2009 at 1:58 PM

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

String value1=tf5.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("DELETE FROM employee WHERE emp_id = ?");
st.setString(1,value1);
st.executeUpdate();
JOptionPane.showMessageDialog(p3,"Record is deleted successfully.");
con.close();
}
catch(Exception exp3)
{
JOptionPane.showMessageDialog(p3,"Error in deleting record.");
}
}
});


editbtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String value=tf7.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("select * from employee where emp_id=?");
st.setString(1,value);
ResultSet res=st.executeQuery();
res.next();
tf7.setText(Integer.toString(res.getInt(1)));
tf8.setText(res.getString(2));
tf9.setText(res.getString(3));
tf10.setText(Integer.toString(res.getInt(4)));
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(p2,"Can not edit data");
}
}
});
editbtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try
{
int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced");
if(x==0)
{
try
{
String value1=tf7.getText();
String value2=tf8.getText();
String value3=tf9.getText();
String value4=tf10.getText();

Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);;
Statement st=con.createStatement();
st.executeUpdate("update employee set emp_name='"+value2+"', emp_address='"+value3+"', salary='"+value4+"' where emp_id='"+value1+"'");
JOptionPane.showMessageDialog(p2,"Updated successfully");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error in updating edit fields");
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error");
}
}
});
}
void dis()
{
f.getContentPane().add(tp);
tp.addTab("Add Record",p1);
tp.addTab("Edit Record",p2);
tp.addTab("Delete Record",p3);

f.setSize(350,180);
f.setVisible(true);
f.setResizable(true);
}
public static void main(String z[])
{
EmployeeInformation pro=new EmployeeInformation();
pro.dis();
}
}
Thanks









Related Tutorials/Questions & Answers:
Java-mysql coding - Java Beginners
Java-mysql coding  Dear Sir, Suppose I have Employee master file in Mysql table format & I want to update that file thru JavaSwing Input format... employee joins, his master details are putted in Java-Swing Form, how I can insert
coding
coding  I need the logout coding. can you please help me.   Please visit the following links: http://www.roseindia.net/quickguide/tomcat/Logout.shtml http://www.roseindia.net/jsp/loginstatus.shtml
Advertisements
program coding
program coding  how to rearrange the array a[1]. . a[n] even elements and odd elements? please give the example coding program.please provide the coding for this problem
coding for project
coding for project  hai how to write jsp coding for project smart accessories ...... that s to navigate to another page when you click on a tag
Java Coding
Java Coding  Hello, Can u please tel me how to improve my coding techniques.I am feeling difficulty with coding but I am perfect with my theory part
coding for chart
coding for chart  I want to convert a character into binary then convert it into its diagram in form of digital electrical signals.How can I do java coding for this diagram
coding of button
coding of button  What is code of fetching the value of button in a textbox using javascript
program coding
program coding  how to rearrange the array a[1]. . a[n] even elements and odd elements? please give the example coding program
coding C
coding C  Write a program for a GENERAL NUMBER CONVERTERS which include binary, decimal, octa and hexadecimal. You need to write the program using C language
coding for login page
coding for login page  coding for login page
coding for anchor tags
coding for anchor tags  coding for anchor tags
coding for anchor tags
coding for anchor tags  coding for anchor tags
coding for anchor tags
coding for anchor tags  coding for anchor tags
coding for updating profile
coding for updating profile  coding for updating profile
coding for forgot password
coding for forgot password  coding for forgot password
coding for update profile
coding for update profile  coding for update profile
coding for deleting file
coding for deleting file  coding for deleting file
coding for anchor tags
coding for anchor tags  coding for anchor tags
coding for online reservation
coding for online reservation  coding for online reservation
need coding
need coding  sir i need code for simple bank application in jsp please send it   sir i need the coding for simple bank application in jsp.   Please visit the following link: Jsp Bank Application
coding in java
coding in java  write a code to calculate the product of odd integers from 1-15   class ProductOfOddIntegers { public static void main(String[] args) { long product=1; for(int i=1;i<=15;i
Coding with Ejb
Coding with Ejb  Hi Team, I am learning Ejb now. Can anybody help me how to implement this Stateful Session Bean Example by using this Hibernate example. Thanks in advance.   Sorry, actually I want simple code adding
coding shape
coding shape  i need to write a simple java program. the system must be in an applet and allows a user to choose whether to draw shapes such as circles, rectangles or lines on the screen. also, user can add circles by choosing
CODING
coding
coding
coding
coding
coding
coding
coding  Write a java program to Create table - Employee containing empId String (20), empName String (50), DOB Date, deptId String(20). Primary key is empId. Foreign Key is deptId. Populate some records
coding
coding for logout - Development process
coding for logout  hi.. coding for logout using the session in jsp
coding for employee management system
coding for employee management system  coding for employee management system
coding for emloyee managent system
coding for emloyee managent system  coding for employee management system
coding for re-type password
coding for re-type password  coding for re-type password
CODING TEST
CODING TEST  Problem Statement Consider a inter dealer-broker network which various financial stock brokers use to execute customer stock trading...¢ Coding productivity (more time you take to submit the exercise, lesser you
CODING TEST
CODING TEST  Problem Statement Consider a inter dealer-broker network which various financial stock brokers use to execute customer stock trading...¢ Coding productivity (more time you take to submit the exercise, lesser you
Coding In Paradise
Coding In Paradise       Excellent and thoughtful AJAX tutorials and techniques by Brad Neuberg Read full DescriptionADS_TO_REPLACE_1
Home page coding
Home page coding  coding fot html
Java coding - WebSevices
Java coding for web services  How to start Java Coding for web services
coding in jsp & servlet
coding in jsp & servlet  plz... provide me coding of "online examination system" in jsp & servlet
coding for gnerating timesheet
coding for gnerating timesheet  hi.... how to do coding for generating timesheet of the employees...working in the office
coding for railway reservation project
coding for railway reservation project  i want to help for creating coding in visual basic for railway reservation project
ModuleNotFoundError: No module named 'coding'
ModuleNotFoundError: No module named 'coding'  Hi, My Python... 'coding' How to remove the ModuleNotFoundError: No module named 'coding'... to install padas library. You can install coding python with following command
ModuleNotFoundError: No module named 'coding'
ModuleNotFoundError: No module named 'coding'  Hi, My Python... 'coding' How to remove the ModuleNotFoundError: No module named 'coding'... to install padas library. You can install coding python with following command
Coding for life cycle in threads
Coding for life cycle in threads  program for life cycle in threads
complete coding for shopping card
complete coding for shopping card  complete coding for shopping card   Please visit the following link: Shopping Cart Application
coding for eletric signal graph
coding for eletric signal graph  How can I draw an electric signal from Manchester and Differential Manchester coding from binary bits that converted from a character for example character
Medical coding training
Medical coding training  Hi, What is Medical coding training course? How anyone can make career in Medical coding training? Thanks   Hi, Medical coding training can be a good career for any graduate. It's one
coding - Java Beginners
coding  i need start coding but don't have any idea to start plz help me

Ads