Java simple login registration applet
- 2020-05-12 02:36:13
- OfStack
Login registration small code, will learn 1 small knowledge fusion in 1 use, deepen the impression. If there are comments in this example that are not detailed, see other blogs.
Simple login and registration system, using database sqlserver, singleton mode, regular expression and graphical development knowledge.
1. Users can log in or register at the login interface. Register the user interface, enter the information in the format specified by the regular expression, and re-enter if there is an error.
2. Click to register, and first connect to SQLserver database. If the connection is successful, it will judge whether the user name already exists. Register instead.
3, login interface, click the login button, first establish a connection with the database. Search the database by username and password, if so, login is successful. Instead, give a hint.
4. Using the singleton pattern, only one object of class SQLserver is created, which greatly saves the memory overhead.
Application complete code see: https: / / github com/chaohuangtianjie994 / A - login register -- System
5. Based on this, a lot of functions can be expanded.
The code is as follows:
UserRegister.java login interface.
package package1;
/*
* Function: login interface with the registration function, popup registration interface.
* The registered information is saved in the database and can be logged in.
* author : ywq
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class UserRegister extends JFrame implements ActionListener{
// Define the components of the login interface
JButton jb1,jb2,jb3=null;
JRadioButton jrb1,jrb2=null;
JPanel jp1,jp2,jp3=null;
JTextField jtf=null;
JLabel jlb1,jlb2=null;
JPasswordField jpf=null;
public static void main(String[] args)
{
UserRegister ur=new UserRegister();
}
public UserRegister()
{
// Create components
// Create components
jb1=new JButton(" The login ");
jb2=new JButton(" registered ");
jb3=new JButton(" exit ");
// Set up to monitor
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jlb1=new JLabel(" User name: ");
jlb2=new JLabel(" The secret Code: ");
jtf=new JTextField(10);
jpf=new JPasswordField(10);
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp1.add(jlb1);
jp1.add(jtf);
jp2.add(jlb2);
jp2.add(jpf);
jp3.add(jb1);
jp3.add(jb2);
jp3.add(jb3);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setVisible(true);
this.setResizable(false);
this.setTitle(" Registration and login interface ");
this.setLayout(new GridLayout(3,1));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(300, 200, 300, 180);
}
@Override
public void actionPerformed(ActionEvent e) {
// Listen for buttons
if(e.getActionCommand()==" exit ")
{
System.exit(0);
}else if(e.getActionCommand()==" The login ")
{
// Calling the login method
this.login();
}else if(e.getActionCommand()==" registered ")
{
// Calling the registration method
this.Regis();
}
}
// Registration method
public void Regis() {
this.dispose(); // Close the current interface
new UI(); // Open up a new interface
}
// Login method
public void login() {
SQLserver s=new SQLserver();
s.ConnectSQL();
s.SQLverify(jtf.getText(), jpf.getText());
this.jtf.setText("");
this.jpf.setText("");
}
}
UI.java for registration page display. Regular expressions are used to specify the content of the input. When you register, you need to first determine whether the user name exists. If it does, you will be prompted. Otherwise, you will register.
package package1;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/*
* Registration interface.
*/
class UI extends JFrame implements ActionListener{
// Define the components
JFrame jf;
JPanel jp;
JLabel jl1,jl2,jl3,jl4;
JTextField jtf1,jtf2,jtf3,jtf4;
JButton jb1,jb2;
public UI()
{
// Initialization component
jf=new JFrame();
jp=new JPanel();
jl1=new JLabel(" Please enter user name: ");
jtf1=new JTextField(10);
jtf1.setToolTipText(" The user name must be 3-6 A letter _ Or digital ");
jl2=new JLabel(" Please enter your password: ");
jtf2=new JTextField(10);
jtf2.setToolTipText(" The password must be 6 A letter _ Or digital ");
jl3=new JLabel(" Please enter your name: ");
jtf3=new JTextField(10);
jtf3.setToolTipText(" Name must be in Chinese 2-4 position ");
jl4=new JLabel(" Please enter student number: ");
jtf4=new JTextField(10);
jtf4.setToolTipText(" Student id must be 3-6 A digital ");
jb1=new JButton(" return ");
jb1.setToolTipText(" Click me to return to the login screen ");
jb2=new JButton(" registered ");
jb1.addActionListener(this);
jb2.addActionListener(this);
jp.setLayout(new GridLayout(5,2));
jp.add(jl1);
jp.add(jtf1);
jp.add(jl2);
jp.add(jtf2);
jp.add(jl3);
jp.add(jtf3);
jp.add(jl4);
jp.add(jtf4);
jp.add(jb1);
jp.add(jb2);
this.add(jp);
this.setTitle(" The registration screen ");
this.setBounds(200, 100, 250, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()==" return ")
{
this.dispose();
new UserRegister();
// System.out.println("-------");
}else if(e.getActionCommand()==" registered ")
{
// Calling the registration method
this.zhuce();
}
}
public void zhuce()
{
String regex1="\\w{3,6}"; // The username must be 3-6 position
boolean flag1=jtf1.getText().matches(regex1);
String regex2="\\w{6}"; // The password must be 6 position
boolean flag2=jtf2.getText().matches(regex2);
String regex3="[\\u4e00-\\u9fa5]{2,4}"; // Names must be in Chinese characters 2-4 A word
boolean flag3=jtf3.getText().matches(regex3);
String regex4="\\d{3,6}"; // Student id must be 3-6 position
boolean flag4=jtf4.getText().matches(regex4);
// if(jtf1.getText()==null||jtf2.getText()==null||jtf3.getText()==null||jtf4.getText()==null)
if(flag1==false)
{
JOptionPane.showMessageDialog(null, " Incorrect user name , Must be 3-6 A letter _ Or digital ", " Prompt information ", JOptionPane.WARNING_MESSAGE);
jtf1.setText("");
}else if(flag2==false)
{
JOptionPane.showMessageDialog(null, " Password error , Must be 6 A letter _ Or digital ", " Prompt information ", JOptionPane.WARNING_MESSAGE);
jtf2.setText("");
}else if(flag3==false)
{
JOptionPane.showMessageDialog(null, " Name error , Must be Chinese 2-4 position ", " Prompt information ", JOptionPane.WARNING_MESSAGE);
jtf3.setText("");
}else if(flag4==false)
{
JOptionPane.showMessageDialog(null, " Wrong student number , Must be 3-6 A digital ", " Prompt information ", JOptionPane.WARNING_MESSAGE);
jtf4.setText("");
}else
{
// Calling the registration method / First check to see if the user name you want to register exists
SQLserver ss=new SQLserver();
ss.ConnectSQL();
ss.ZhuceVerify(jtf1.getText());
// ss.UserRegis(jtf1.getText(),jtf2.getText(),jtf3.getText(), jtf4.getText());
this.jtf1.setText("");
this.jtf2.setText("");
this.jtf3.setText("");
this.jtf4.setText("");
}
}
}
SQLserver.java realizes the connection with the database, query verification and other functions.
package package1;
import java.sql.*;
import javax.swing.JOptionPane;
/*
* Database-related operations, wrapped separately into classes
*/
class SQLserver {
Connection ct;
PreparedStatement ps;
ResultSet rs;
String user,pwd;
// Encapsulates the method to connect to the database as 1 A method of
public void ConnectSQL()
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // The load driver
ct=DriverManager.getConnection("jdbc:odbc:ywq"); // Get the connection
System.out.println(" The database was connected successfully ...");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Method of registering users
public void UserRegis(String a,String b,String c,String d)
{
// Creating a rocket car
try {
ps=ct.prepareStatement("insert into users values(?,?,?,?)");
ps.setString(1,a);
ps.setString(2,b);
ps.setString(3,c);
ps.setString(4,d);
// perform
int i=ps.executeUpdate();
if(i==1)
{
JOptionPane.showMessageDialog(null, " Registered successfully "," The prompt message ",JOptionPane.WARNING_MESSAGE);
}else
{
JOptionPane.showMessageDialog(null, " Registration failed "," The prompt message ",JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Login verification method
public void SQLverify(String a,String b)
{
try {
ps=ct.prepareStatement("select * from users where The user name =? and password =? ");
ps.setString(1, a);
ps.setString(2, b);
// ResultSet The result set , You can take ResultSet Understood as returning 1 The result set of a table row
rs = ps.executeQuery();
if(rs.next())
{
user = rs.getString(1);
pwd = rs.getString(2);
JOptionPane.showMessageDialog(null, " Login successful!! ", " The prompt message ", JOptionPane.WARNING_MESSAGE);
System.out.println(" The password and username were successfully obtained from The database ");
System.out.println(user + "\t" + pwd + "\t");
}else
{
JOptionPane.showMessageDialog(null, " User name or password error, please re - enter! ", " The prompt message ", JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// Register the validation method to determine if the user name already exists
public void ZhuceVerify(String a)
{
try {
ps=ct.prepareStatement("select * from users where The user name =?");
// System.out.println(ps);
ps.setString(1, a);
rs=ps.executeQuery();
if(rs.next())
{
JOptionPane.showMessageDialog(null, " The user name already exists ", " Prompt information ", JOptionPane.WARNING_MESSAGE);
}else
{
// To register
UI ui=new UI();
this.UserRegis(ui.jtf1.getText(),ui.jtf2.getText(),ui.jtf3.getText(),ui.jtf4.getText());
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}