java

java

Hi

iam created a new registration form as a desktop application using swings.
and this is the code: pls have a look




import mysql.DataAcc;
// import package class for getting database connection
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class D1 extends JFrame implements ActionListener{
//instance variable
String str="";
/* Swing classess */
JLabel l,l1,l2,l3,l4,title,l5,l6;
JTextField t,t1,t2,t3,t4;
JButton b,b1,b2,b3,b4,b5,exit;
JTextArea ta,ta1;
//create new instance of class D1
D1() {
setSize(700,500); //set Frame Size
Container c=getContentPane(); //Objects add to Container
c.setLayout(null);
ta=new JTextArea(30,30);
title=new JLabel("Employee Information");
l1=new JLabel("Employee Number");
l2=new JLabel("Employee Name");
l=new JLabel("Employee Password");
l3=new JLabel("Employee Salary");
l4=new JLabel("Employee Address");
l5=new JLabel("Database Operation's ");
t1=new JTextField(10);
t2=new JTextField(10);
t =new JTextField(10);
t3=new JTextField(10);
t4=new JTextField(10);
b=new JButton("Reports");
b1=new JButton("Insert");
b2=new JButton("Delete");
b3=new JButton("Update");
b4=new JButton("Single");
b5=new JButton("View");
exit=new JButton("Exit");
//set bounds() palce components in a givel dimesions in the Frame
title.setBounds(200,10,150,100);
//components add to Continer
c.add(title);
l1.setBounds(100,40,150,100);
c.add(l1);
l2.setBounds(100,70,150,100);
c.add(l2);
l.setBounds(100,100,150,100);
c.add(l);
l3.setBounds(100,130,150,100);
c.add(l3);
l4.setBounds(100,160,150,100);
c.add(l4);
l5.setBounds(200,230,250,100);
c.add(l5);
t1.setBounds(250,80,150,20);
c.add(t1);
t2.setBounds(250,110,150,20);
c.add(t2);
t.setBounds(250,140,150,20);
c.add(t);
t3.setBounds(250,170,150,20);
c.add(t3);
t4.setBounds(250,210,150,20);
c.add(t4);
//add buttons to the ActionListener Interface
b .addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
exit.addActionListener(this);
b .setBounds(10,240,80,20);
c.add(b);
b1.setBounds(100,240,80,20);
c.add(b1);
b2.setBounds(190,240,80,20);
c.add(b2);
b3.setBounds(280,240,80,20);
c.add(b3);
b4.setBounds(370,240,80,20);
c.add(b4);
b5.setBounds(460,240,80,20);
c.add(b5);
exit.setBounds(550,240,80,20);
c.add(exit);
ta.setBounds(200,300,270,100);
c.add(ta);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Connection con=null;
DataAcc d=new DataAcc();
if(e.getSource()==b1) {
ta.setText(str);
/*get values from textfields*/
int sno=Integer.parseInt(t1.getText());
String sname=t2.getText();
String spwd =t.getText();
double sal=Double.parseDouble(t3.getText());
String address=t4.getText();
try {
con=d.getConnect();
PreparedStatement pst=con.prepareStatement("insert into test values(?,?,?,?,?)");
pst.setInt(1,sno);
pst.setString(2,sname);
pst.setString(3,spwd);
pst.setDouble(4,sal);
pst.setString(5,address);
int i=pst.executeUpdate();
if(i>0)
l5.setText("Insertion Operation Performed");
else
l5.setText("Insertion Operation Failed");
pst.clearParameters();
/*text field values clear after insertion performed*/
t1.setText("");
t2.setText("");
t.setText("");
t3.setText("");
t4.setText("");
}catch(Exception ae){
ae.printStackTrace();
}
}//Buuton B1 if

if(e.getSource()==b5) {
ta.setText(str);
try {
con=d.getConnect();
PreparedStatement pst=con.prepareStatement("select * from test");
ResultSet rs=pst.executeQuery();
while(rs.next()) {
int n1=rs.getInt(1);
String n2=rs.getString(2);
String n=rs.getString(3);
double n3=rs.getDouble(4);
String n4=rs.getString(5);
/*append all values to the text area by ta.append()*/
ta.append(""+n1+"\t"+n2+"\t"+n+"\t"+n3+"\t"+n4+"\n");
//JOptionPane.showMessageDialog(null,n1+"\n"+n2+"\n"+n3+"\n"+n4);
l5.setText("View test Table Data");
}
}catch(Exception ae){
ae.printStackTrace();
}
}//Button b5 if
if(e.getSource()==b2) {
ta.setText(str);
int sno=Integer.parseInt(t1.getText());
try {
con=d.getConnect();
PreparedStatement pst=con.prepareStatement("delete from test where Empnumber="+sno);
int i=pst.executeUpdate();
if(i>0)
l5.setText("Deletion Operation Performed");
else
l5.setText("Deletion Operation Failed");
pst.clearParameters();
t1.setText("");
t2.setText("");
t.setText("");
t3.setText("");
t4.setText("");
}catch(Exception ae){
ae.printStackTrace();
}
}//Button b2 if
if(e.getSource()==b3) {
ta.setText(str);
int sno=Integer.parseInt(t1.getText());
String sname=t2.getText();
String spwd=t.getText();
double sal=Double.parseDouble(t3.getText());
String address=t4.getText();
try {
con=d.getConnect();
//PreparedStatement pst=con.prepareStatement("update test set sname=?,spwd=?,sal=?,address=? where sno=?");
PreparedStatement pst=con.prepareStatement("update test set Empname=?,Emppassword=?,Empsal=?,Empadd=? where Empnumber=?");
pst.setString(1,sname);
pst.setString(2,spwd);
pst.setDouble(3,sal);
pst.setString(4,address);
pst.setInt(5,sno);
int i=pst.executeUpdate();
if(i>0)
l5.setText("Update Operation Performed");
else
l5.setText("Update Operation Failed");
pst.clearParameters();
t1.setText("");
t2.setText("");
t.setText("");
t3.setText("");
t4.setText("");
}catch(Exception ae){
ae.printStackTrace();
}
}//button b3 if
if(e.getSource()==b4) {
ta.setText(str);
int sno=Integer.parseInt(t1.getText());
try {
con=d.getConnect();
PreparedStatement pst=con.prepareStatement("select * from test where Empnumber="+sno);
ResultSet rs=pst.executeQuery();
if(rs.next()) {
t1.setText(""+rs.getInt(1));
t2.setText(rs.getString(2));
t.setText(rs.getString(3));
t3.setText(""+rs.getDouble(4));
t4.setText(rs.getString(5));
ta.append(t1.getText()+"\n"+t2.getText()+"\n"+t.getText()+"\n"+t3.getText()+"\n"+t4.getText());
l5.setText("Record Avilable with Serial Number : "+sno);
}
else {
l5.setText("No Record Avilable with Serial Number : "+sno);
pst.clearParameters();
t1.setText("");
t2.setText("");
t.setText("");
t3.setText("");
t4.setText("");
}
}catch(Exception ae){
ae.printStackTrace();
}
}//Button b5 if
if(e.getSource()==exit)
System.exit(0);

}
}
class Database {
public static void main(String[] args) {
D1 d=new D1();
}

}




and





package mysql;
import java.sql.*;
import java.io.*;

public class DataAcc
{
public Connection con=null;
public Connection getConnect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:msdesktop");
System.out.println("Connected");
}
catch(Exception e)
{
System.out.println(e);
}
return con;
}//getConnection

}//class






the o/p is coming
in that o/p we type all the fields and we click "insert" it is inserted into the database and all the operations is working
my doubt is if we type the empnumber and we click "reports" the respective empnumber,name,sal,place is displayed in another frame(window).

please help me.
View Answers

July 21, 2010 at 4:37 PM

Hi Friend,

Add the following code to the actionPerformed() method:

if(e.getSource()==b){
int sno=Integer.parseInt(t1.getText());
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:access");
PreparedStatement pst=con.prepareStatement("select * from test where Empnumber="+sno);
ResultSet rs=pst.executeQuery();
JFrame f=new JFrame();
int n1=0,n3=0;
String n="",n2="",n4="";
JPanel p=new JPanel(new GridLayout(5,1));
if(rs.next()) {
n1=rs.getInt(1);
n2=rs.getString(2);
n=rs.getString(3);
n3=rs.getInt(4);
n4=rs.getString(5);
}
p.add(new JLabel("Emp No: "+Integer.toString(n1)));
p.add(new JLabel("Name: "+n2));
p.add(new JLabel("Password: "+n));
p.add(new JLabel("Salary: "+Integer.toString(n3))) ;
p.add(new JLabel("Address: "+n4));
f.add(p);
f.setVisible(true);
f.pack();
}catch(Exception ae){

}
}

Thanks









Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
java
java   why iterator in java if we for loop
Java
Java   Whether Java is pure object oriented Language
JAVA
JAVA  how the name came for java language as "JAVA
java
java  explain technologies are used in java now days and structure java
java
java  different between java & core java
java
java  is java open source
java
java  what is java reflection
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java  why to set classpath in java
java
java  RARP implementation using java socket
java
java  sample code for RARP using java
Java
Java  how to do java in command prompt
java
java  how use java method
java
java  is java purely object oriented language
java
java  why multiple inheritance is not possible in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java   What is ?static? keyword
java
java  Does java allows multiline comments
java
java  Write a java code to print "ABABBABCABABBA
java
java  write a program in java to acess the email
java
java  send me java interview questions
java
java  what are JAVA applications development tools
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  explain object oriented concept in java
java
java   difference between class and interface
Java
Java  how to draw class diagrams in java
java
java  write a java program using filenotfoundexception
java
java  how to edit text document by using java then how to edit starting and ending of text document by using java
java
java  different between java & core java print("code sample
java
java  how can use sleep in java   which book learn of java language
java
java  hi im new to java plz suggest me how to master java[email protected]
java
java   How to set java Policy for applet using jdk 6
java
java pattern code for a given words  java pattern code for a given words pattern
java
java  dear, i want a field for date picker using java/java script
java
java  create java program for delete and update the details,without using database, just a normal java program
java
java  why methods in java raise exceptions   Have a look at the following link: Java Exceptions
java
java code to search the nodes  how to write the java code to search the nodes using routers
java
java online telephone directory  i need coding for online telephone directory..by using java....pls help me
java
java  different between java & core java print("code sample
java
java  how to invoke one chart java file from another java file
java
java  how to prepare the java   Hi Friend, If you want to learn how to install java, creating and running a java program then go through the following links: http://www.roseindia.net/java/beginners/index.shtml http
java
java  java swing   Swing is a principal GUI toolkit for the Java programming language. It is a part of the JFC (Java Foundation Classes), which is an API for providing a graphical user interface for Java programs
java
java  what is the need of java if java is not there what will happen... work unless you have Java installed, and more are created every day. Java... to scientific supercomputers, cell phones to the Internet, Java is everywhere! http

Ads