java

java

View Answers

May 26, 2009 at 4:05 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 Project
{
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,searchbtn1,searchbtn2,editbtn1,editbtn2 ;
GridBagLayout gbl;
GridBagConstraints gbc;

Project()
{
f=new JFrame("SMS");
p1=new JPanel(new GridLayout(5,2));
p2=new JPanel(new GridLayout(2,2));
p3=new JPanel(new GridLayout(5,2));
tp=new JTabbedPane();
l1=new JLabel("Code:");
l2=new JLabel("Name:");
l3=new JLabel("Mobile:");
l4=new JLabel("Email:");

l5=new JLabel("Code :");
l6=new JLabel("Name :");

l7=new JLabel("Code:");
l8=new JLabel("Name:");
l9=new JLabel("Mobile:");
l10=new JLabel("Email:");
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(" Save ");
resetbtn=new JButton(" Reset");
searchbtn1=new JButton("Search1");
searchbtn2=new JButton("Search2");
editbtn1=new JButton(" Edit ");
editbtn2=new JButton(" Save");

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(l5);
p2.add(tf5);

p2.add(searchbtn1);
p2.add(l6);

p2.add(tf6);
p2.add(searchbtn2);

p3.add(l7);
p3.add(tf7);

p3.add(l8);
p3.add(tf8);

p3.add(l9);
p3.add(tf9);

p3.add(l10);
p3.add(tf10);

p3.add(editbtn1);
p3.add(editbtn2);

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){
try
{
String temp=tf1.getText();
String temp2=tf2.getText();
String ttemp=tf3.getText();
String temp4=tf4.getText();
System.out.println(temp+temp2);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con1;
con1=DriverManager.getConnection("jdbc:odbc:access","","");
PreparedStatement stat1=con1.prepareStatement("insert into proj values(?,?,?,?)");
stat1.setString(1,temp);
stat1.setString(2,temp2);
stat1.setString(3,ttemp);
stat1.setString(4,temp4);
stat1.executeUpdate();
con1.close();
}
catch(Exception exp1)
{
JOptionPane.showMessageDialog(p1,"Error Code 1 : Submit Failed... ");
}
}
});

May 26, 2009 at 4:08 PM

continue.........

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

try
{
String temp5=tf5.getText();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con2;
con2=DriverManager.getConnection("jdbc:odbc:access","","");
PreparedStatement stat2=con2.prepareStatement("select*from proj where code=?");
stat2.setString(1,temp5);
ResultSet res=stat2.executeQuery();
res.next();
String val1=res.getString(1);
String val2=res.getString(2);
String val3=res.getString(3);
String val4=res.getString(4);
CreateTable tab=new CreateTable();
tab.setVisible(true);
JTextArea area = new JTextArea();
area.setText(val1+" "+val2+" "+val3+" "+val4);
JScrollPane scrollPane = new JScrollPane(area);
tab.add(scrollPane, BorderLayout.CENTER);
con2.close();
}
catch(Exception exp2)
{
JOptionPane.showMessageDialog(p2,"Error Code 2 : Can't Search may be you have entered an invalid code");
}
}
});

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

try
{
String name=tf6.getText();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con3;
con3=DriverManager.getConnection("jdbc:odbc:access","","");
PreparedStatement stat3=con3.prepareStatement("select * from proj where name=?");
stat3.setString(1,name);
ResultSet res=stat3.executeQuery();
String strVal="";
while(res.next()){

ArrayList al = new ArrayList();
al.add(res.getString(1));
al.add(res.getString(2));
al.add(res.getString(3));
al.add(res.getString(4));
Iterator itr = al.iterator();
while(itr.hasNext())
{
strVal+= itr.next().toString()+" ";
}
}
CreateTable tab=new CreateTable();
tab.setVisible(true);
JTextArea area = new JTextArea();
area.setText(strVal);
JScrollPane scrollPane = new JScrollPane(area);
tab.add(scrollPane, BorderLayout.CENTER);
con3.close();
}
catch(Exception exp3)
{
JOptionPane.showMessageDialog(p2,"Error Code 3: Can't Search may be you have entered an invalid name");
}
}
});


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

try
{
String temp7=tf7.getText();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con4;
con4=DriverManager.getConnection("jdbc:odbc:access","","");
PreparedStatement stat4=con4.prepareStatement("select * from proj where code=?");
stat4.setString(1,temp7);
ResultSet res=stat4.executeQuery();
res.next();
tf7.setText(res.getString(1));
tf8.setText(res.getString(2));
tf9.setText(res.getString(3));
tf10.setText(res.getString(4));
con4.close();
}
catch(Exception exp4)
{
JOptionPane.showMessageDialog(p3,"Error Code 4: Can not edit data...");
}

}
});

May 26, 2009 at 4:10 PM

continue........

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

try
{
int x=JOptionPane.showConfirmDialog(p3,"Confirm edit? All data will be replaced");
if(x==0)
{
try
{
String temp9=tf7.getText();
String temp11=tf8.getText();
String temp12=tf9.getText();
String temp14=tf10.getText();
System.out.println(temp9+temp11);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con5;
con5=DriverManager.getConnection("jdbc:odbc:access","","");
Statement stat5=con5.createStatement();
stat5.executeUpdate("update proj set code='"+temp9+"', name='"+temp11+"', mobile='"+temp12+"', email='"+temp14+"' where code='"+temp9+"'");
JOptionPane.showMessageDialog(p3,"Saved........");
con5.close();
}
catch(Exception exp5)
{
JOptionPane.showMessageDialog(p3,"Error Code 5 : Problem in updating edit fields...");
}
}
else if(x==1)
{

JOptionPane.showMessageDialog(p3,"Action terminated by user...");
}
else if(x==2)
{
tf7.setText("");
tf8.setText("");
tf9.setText("");
tf10.setText("");
}
}
catch(Exception exp6)
{
JOptionPane.showMessageDialog(p3,"Error Code 6 : Editing Failed... ");
}
}
});
}
void dis()
{
f.getContentPane().add(tp);
tp.addTab("Client Details",p1);
tp.addTab("Search Options",p2);
tp.addTab("Edit Options",p3);

f.setSize(350,180);
f.setVisible(true);
f.setResizable(true);
}


public static void main(String z[])
{
Project pro=new Project();
pro.dis();
}
}

We have used another class in order to display the text area in another frame:

import javax.swing.*;
import java.awt.*;

class CreateTable extends JFrame{
CreateTable()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setSize(400, 150);
}
}
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