Anda di halaman 1dari 2

package guipackage;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
public class RegistrationForm extends JFrame implements ActionListener{
private JLabel l1,l2,l3,l4;
private JTextField t1,t2,t3;
private JPasswordField p1;
private JButton save,show,clear;
private Connection con=null;
private Statement stmt=null;
private static final String URL="jdbc:mysql://localhost:3306/mydb";

public RegistrationForm() throws ClassNotFoundException


{
setSize(430,400);
setTitle("Registration Form");
setLayout(null);
l1=new JLabel("Name:");
l2=new JLabel("Email:");
l3=new JLabel("Password:");
l4=new JLabel("Mobile:");
t1=new JTextField();
t2=new JTextField();
t3=new JTextField();
p1=new JPasswordField();
save=new JButton("SAVE");
show=new JButton("SHOW");
clear=new JButton("CLEAR");
add(l1);add(l2);add(l3);add(l4);
add(t1);add(t2);add(t3);add(p1);
add(show);add(save);add(clear);
show.addActionListener(this);
save.addActionListener(this);
clear.addActionListener(this);
l1.setBounds(50, 50, 100, 30);
t1.setBounds(150, 50, 120, 30);
l2.setBounds(50, 100, 100, 30);
t2.setBounds(150, 100, 120, 30);
l3.setBounds(50, 150, 100, 30);
p1.setBounds(150, 150, 120, 30);
l4.setBounds(50, 200, 100, 30);
t3.setBounds(150, 200, 120, 30);
save.setBounds(50, 250, 100, 30);
show.setBounds(150, 250, 100, 30);
clear.setBounds(250, 250, 100, 30);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dispose();
}
});
setVisible(true);
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(URL, "root", "");
stmt=con.createStatement();
} catch (SQLException e) {
e.getMessage();
}

}
public void insertData()
{
String name=t1.getText();
String email=t2.getText();
String password=p1.getText();
String contact=t3.getText();

if(name.isEmpty()||email.isEmpty()||password.isEmpty()||contact.isEmpty())
JOptionPane.showMessageDialog(save,"Please Fill the Fields");
else if(password.length()<8)
JOptionPane.showMessageDialog(save, "Password is too short");
else if(contact.length()<10)
JOptionPane.showMessageDialog(save, "Contact Number is incorrect");
else
{
try{
int x=stmt.executeUpdate("INSERT INTO regform
(name,email,password,mobile) "
+ "VALUES('"+name+"','"+email+"','"+password+"','"+contact+"')");
if(x>0)
JOptionPane.showMessageDialog(save, "Record Saved Successfully");
else
JOptionPane.showMessageDialog(save, "Try Again");
} catch (SQLException e) {
e.getMessage();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==save)
insertData();
}
public static void main(String[] args) throws ClassNotFoundException{
new RegistrationForm();
}

Anda mungkin juga menyukai