18. Registration Form Demo In JAVA
Steps:
1.Copy program below in a text file.
2.Give File Name As "registrationform.java"
3.Open This file Through Netbeans or JDK
4. Run File and check output.
//***************************Start*******************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class registrationform implements ActionListener {
JFrame jf;
JPanel jp;
JLabel l1,l2,l3,l4,l5;
JButton b1,b2,b3,b4;
JTextField jf1,jf2,jf3,jf4;
public registrationform()
{
jf=new JFrame("Registration Form");
jf.setSize(400,350);
jf.setLocation(500 ,100);
jf.setVisible(true);
jf.setResizable(true);
jp=new JPanel();
jp.setLayout(null);
jf.add(jp);
l1=new JLabel("First Name");
l2=new JLabel("last Name");
l3=new JLabel("city Name");
l4=new JLabel("contact no");
l5=new JLabel("Registration Form");
l5.setFont(new Font("",Font.BOLD,20));
l1.setBounds(50, 40, 100,50);
l2.setBounds(50, 80, 100,50);
l5.setBounds(110, 5, 200 ,50);
l3.setBounds(50, 120, 100,50);
l4.setBounds(50, 160, 100,50);
jp.add(l1);
jp.add(l2);
jp.add(l3);
jp.add(l4);
jp.add(l5);
jf1=new JTextField();
jf2=new JTextField();
jf3=new JTextField();
jf4=new JTextField();
jf1.setBounds(200,65,100,25);
jf2.setBounds(200,100,100,25);
jf3.setBounds(200,135,100,25);
jf4.setBounds(200,170,100,25);
jp.add(jf1);
jp.add(jf2);
jp.add(jf3);
jp.add(jf4);
b1=new JButton("sign up");
b1.addActionListener(this);
b1.setActionCommand("sign up");
b1.setBounds(50,235,80,25);
jp.add(b1);
b3=new JButton("Update");
b3.addActionListener(this);
b3.setActionCommand("Update");
b3.setBounds(140,235,80,25);
jp.add(b3);
b4=new JButton("Delete");
b4.addActionListener(this);
b4.setActionCommand("Delete");
b4.setBounds(50,270,80,25);
jp.add(b4);
b2=new JButton("reset");
b2.addActionListener(this);
b2.setActionCommand("reset");
b2.setBounds(235,235,80,25);
jp.add(b2);
}
void reset()
{
jf1.setText("");
jf2.setText("");
jf3.setText("");
jf4.setText("");
}
public static void main(String arsg[])
{
registrationform reg=new registrationform();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="sign up")
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = null;
conn = (Connection)DriverManager.getConnection("jdbc:odbc:p");
conn.setAutoCommit(false);
PreparedStatement stmt=conn.prepareStatement("insert into login values('"+jf1.getText()+"','"+jf2.getText()+"','"+jf3.getText()+"','"+jf4.getText()+"');");
int result=stmt.executeUpdate();
if(result>0)
{
System.out.println("recored inserted");
}
else
{
System.out.println("not inserted");
}
}
catch(Exception ex){
JOptionPane jp=new JOptionPane();
jp.showMessageDialog(null,ex.getMessage());;
}
JOptionPane jp=new JOptionPane();
jp.showMessageDialog(null,"hi bro how r u");
}
if(e.getActionCommand()=="Update")
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = null;
conn = (Connection)DriverManager.getConnection("jdbc:odbc:p");
conn.setAutoCommit(false);
PreparedStatement stmt=conn.prepareStatement("update login set firstname='"+jf1.getText()+"',lastname='"+jf2.getText()+"',city='"+jf3.getText()+"' where contact='"+jf4.getText()+"'");
int result=stmt.executeUpdate();
if(result>0)
{
System.out.println("recored updated");
}
else
{
System.out.println("not updated");
}
}
catch(Exception ex){
JOptionPane jp=new JOptionPane();
jp.showMessageDialog(null,ex.getMessage());;
}
JOptionPane jp=new JOptionPane();
jp.showMessageDialog(null,"hi bro how r u");
}
if(e.getActionCommand()=="Delete")
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = null;
conn = (Connection)DriverManager.getConnection("jdbc:odbc:p");
conn.setAutoCommit(false);
PreparedStatement stmt=conn.prepareStatement("delete from login where contact='"+jf4.getText()+"'");
int result=stmt.executeUpdate();
if(result>0)
{
System.out.println("recored Deleted");
}
else
{
System.out.println("not Deleted");
}
}
catch(Exception ex){
JOptionPane jp=new JOptionPane();
jp.showMessageDialog(null,ex.getMessage());;
}
JOptionPane jp=new JOptionPane();
jp.showMessageDialog(null,"hi bro how r u");
}
if(e.getActionCommand()=="reset")
{
reset();
}
}
}
//*****************************End******************************