Skip to content

Instantly share code, notes, and snippets.

@2DNQ
Created August 25, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2DNQ/30164cbb3a883670947c5ae9eae2b651 to your computer and use it in GitHub Desktop.
Save 2DNQ/30164cbb3a883670947c5ae9eae2b651 to your computer and use it in GitHub Desktop.
Login and register in eclipse
package Login_Sys;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import RanSanMoi.FrameScreen;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.awt.event.ActionEvent;
import javax.swing.JSeparator;
import java.awt.Font;
import java.awt.Color;
import java.awt.Window.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import Register_Sys.Register_S;
import java.awt.SystemColor;
public class Login_S {
protected static Component frmLoginSystem = null;
private JFrame frmLogin;
private JTextField textField;
private JPasswordField passwordField;
Connection connection=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login_S window = new Login_S();
window.frmLogin.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Login_S() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
try {
Class.forName("org.h2.Driver");
connection= DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test","sa","");
}
catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frmLogin = new JFrame();
frmLogin.setType(Type.POPUP);
frmLogin.setTitle("Login");
frmLogin.setBackground(Color.BLUE);
frmLogin.getContentPane().setBackground(new Color(106, 90, 205));
frmLogin.setForeground(Color.BLACK);
frmLogin.getContentPane().setForeground(Color.DARK_GRAY);
frmLogin.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 15));
frmLogin.setBounds(200, 200, 500, 300);
frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmLogin.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Login Systems");
lblNewLabel.setBounds(167, 11, 186, 26);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 25));
frmLogin.getContentPane().add(lblNewLabel);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(66, 70, 90, 30);
lblUsername.setFont(new Font("Tahoma", Font.BOLD, 15));
frmLogin.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(66, 135, 75, 30);
lblPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
frmLogin.getContentPane().add(lblPassword);
textField = new JTextField();
textField.setBounds(151, 71, 161, 30);
textField.setFont(new Font("Tahoma", Font.BOLD, 15));
frmLogin.getContentPane().add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(151, 136, 161, 30);
passwordField.setFont(new Font("Tahoma", Font.BOLD, 15));
frmLogin.getContentPane().add(passwordField);
JButton btnLogin = new JButton("Login");
btnLogin.setForeground(Color.BLUE);
btnLogin.setBackground(Color.LIGHT_GRAY);
btnLogin.setBounds(10, 210, 89, 23);
btnLogin.setFont(new Font("Tahoma", Font.BOLD, 15));
btnLogin.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent arg0) {
try{
String validate= "SELECT * FROM USERTABLE WHERE USER=? AND PASS=? ";
PreparedStatement statement = connection.prepareStatement(validate);
statement.setString(1, textField.getText());
statement.setString(2, passwordField.getText());
ResultSet set = statement.executeQuery();
if(set.next()){
frmLogin.setVisible(false);
FrameScreen info= new FrameScreen();
FrameScreen.main(null);
}
else{
JOptionPane.showMessageDialog(null, "Invalid Login Details","Login System Error",JOptionPane.ERROR_MESSAGE);
passwordField.setText(null);
textField.setText(null);
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
});
frmLogin.getContentPane().add(btnLogin);
JButton btnReset = new JButton("Reset");
btnReset.setForeground(Color.BLUE);
btnReset.setBackground(Color.LIGHT_GRAY);
btnReset.setBounds(135, 210, 89, 23);
btnReset.setFont(new Font("Tahoma", Font.BOLD, 15));
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
passwordField.setText(null);
textField.setText(null);
}
});
frmLogin.getContentPane().add(btnReset);
JButton btnExit = new JButton("Exit");
btnExit.setForeground(Color.BLUE);
btnExit.setBackground(Color.LIGHT_GRAY);
btnExit.setBounds(385, 210, 89, 23);
btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frmLoginSystem= new JFrame("Exit");
if(JOptionPane.showConfirmDialog(frmLoginSystem,"Confirm if you want to exit","Login Systems",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_NO_OPTION){
System.exit(1);
}
}
});
frmLogin.getContentPane().add(btnExit);
JSeparator separator = new JSeparator();
separator.setBounds(10, 188, 464, 2);
frmLogin.getContentPane().add(separator);
JSeparator separator_1 = new JSeparator();
separator_1.setBounds(10, 35, 464, 2);
frmLogin.getContentPane().add(separator_1);
JButton btnRegister = new JButton("Register");
btnRegister.setBackground(Color.LIGHT_GRAY);
btnRegister.setForeground(Color.BLUE);
btnRegister.setFont(new Font("Tahoma", Font.BOLD, 15));
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Register_S info = new Register_S();
Register_S.main(null);
frmLogin.setVisible(false);
}
});
btnRegister.setBounds(256, 210, 97, 23);
frmLogin.getContentPane().add(btnRegister);
}
}
package Register_Sys;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import Login_Sys.Login_S;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.Window.Type;
public class Register_S {
private JFrame frmRegister;
private JTextField mailField;
private JTextField nameField;
private JTextField phoneField;
private JLabel lblUsername;
private JTextField userField;
private JLabel lblPassword;
private JPasswordField passwordField;
private JButton btnSubmit;
private JButton btnCanel;
private JSeparator separator;
private JSeparator separator_1;
private JButton btnExit;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register_S window = new Register_S();
window.frmRegister.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Register_S() {
initialize();
}
Connection connection=null;
public void createTableNew(){
try {
DatabaseMetaData dmd=connection.getMetaData();
ResultSet set=dmd.getTables(null, null, "USERTABLE", null);
if(set.next()){
}
else{
String creat_table="create table USERTABLE ("
+"email varchar2(20),"
+"name varchar2(30), "
+"phone varchar2(12), "
+"user varchar2(30), "
+"pass varchar2 (20))";
PreparedStatement statement = connection.prepareStatement(creat_table);
statement.executeUpdate();
JOptionPane.showMessageDialog(null, "Table create sucessfull");
}
}
catch (Exception e){
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
try {
Class.forName("org.h2.Driver");
connection= DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test","sa","");
}
catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frmRegister = new JFrame();
frmRegister.setTitle("REGISTER");
frmRegister.setType(Type.POPUP);
frmRegister.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 15));
frmRegister.getContentPane().setBackground(new Color(30, 144, 255));
frmRegister.setBounds(200, 200, 400, 400);
frmRegister.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmRegister.getContentPane().setLayout(null);
JLabel lblRegisterSystem = new JLabel("Register System");
lblRegisterSystem.setFont(new Font("Tahoma", Font.BOLD, 20));
lblRegisterSystem.setBounds(116, 11, 176, 35);
frmRegister.getContentPane().add(lblRegisterSystem);
JLabel lblEmail = new JLabel("Email");
lblEmail.setFont(new Font("Tahoma", Font.BOLD, 15));
lblEmail.setBounds(24, 60, 46, 14);
frmRegister.getContentPane().add(lblEmail);
mailField = new JTextField();
mailField.setBounds(120, 57, 170, 20);
frmRegister.getContentPane().add(mailField);
mailField.setColumns(10);
JLabel lblName = new JLabel("Name");
lblName.setFont(new Font("Tahoma", Font.BOLD, 15));
lblName.setBounds(24, 100, 46, 14);
frmRegister.getContentPane().add(lblName);
nameField = new JTextField();
nameField.setBounds(120, 100, 170, 20);
frmRegister.getContentPane().add(nameField);
nameField.setColumns(10);
JLabel lblPhone = new JLabel("Phone");
lblPhone.setFont(new Font("Tahoma", Font.BOLD, 15));
lblPhone.setBounds(24, 140, 46, 14);
frmRegister.getContentPane().add(lblPhone);
phoneField = new JTextField();
phoneField.setBounds(120, 140, 170, 20);
frmRegister.getContentPane().add(phoneField);
phoneField.setColumns(10);
lblUsername = new JLabel("Username");
lblUsername.setFont(new Font("Tahoma", Font.BOLD, 15));
lblUsername.setBounds(24, 180, 86, 14);
frmRegister.getContentPane().add(lblUsername);
userField = new JTextField();
userField.setBounds(120, 180, 170, 20);
frmRegister.getContentPane().add(userField);
userField.setColumns(10);
lblPassword = new JLabel("Password");
lblPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
lblPassword.setBounds(24, 220, 73, 14);
frmRegister.getContentPane().add(lblPassword);
passwordField = new JPasswordField();
passwordField.setBounds(120, 220, 170, 20);
frmRegister.getContentPane().add(passwordField);
btnSubmit = new JButton("Submit");
btnSubmit.setFont(new Font("Tahoma", Font.BOLD, 15));
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String insert_data=" insert into USERTABLE values(?,?,?,?,?)";
PreparedStatement statement= connection.prepareStatement(insert_data);
statement.setString(1, mailField.getText());
statement.setString(2, nameField.getText());
statement.setString(3, phoneField.getText());
statement.setString(4, userField.getText());
statement.setString(5, passwordField.getText());
int data_entered= statement.executeUpdate();
if (data_entered>0)
{
JOptionPane.showMessageDialog(null, "Data inserted into table sucessfull");
}
else
JOptionPane.showMessageDialog(null, "Data insert false");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnSubmit.setBounds(21, 286, 89, 23);
frmRegister.getContentPane().add(btnSubmit);
btnCanel = new JButton("Canel");
btnCanel.setFont(new Font("Tahoma", Font.BOLD, 15));
btnCanel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
passwordField.setText(null);
mailField.setText(null);
nameField.setText(null);
phoneField.setText(null);
userField.setText(null);
}
});
btnCanel.setBounds(149, 286, 89, 23);
frmRegister.getContentPane().add(btnCanel);
separator = new JSeparator();
separator.setBounds(10, 47, 364, 2);
frmRegister.getContentPane().add(separator);
separator_1 = new JSeparator();
separator_1.setBounds(10, 273, 364, 2);
frmRegister.getContentPane().add(separator_1);
btnExit = new JButton("Exit");
btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame frmRegisterSystem = new JFrame("Exit");
if(JOptionPane.showConfirmDialog(frmRegisterSystem,"Confirm if you want to change login","Register Systems",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_NO_OPTION){
frmRegister.setVisible(false);
Login_S info= new Login_S();
Login_S.main(null);
}
}
});
btnExit.setBounds(271, 286, 89, 23);
frmRegister.getContentPane().add(btnExit);
createTableNew();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment