Log in and register the small code, combine some of the small knowledge you have learned and use it to deepen your impression. If there are any comments in this example, please refer to other blogs for details.
Function introduction: A simple login and registration system, using knowledge such as database sqlserver, singleton pattern, regular expressions, and graphical development.
1. In the login interface, you can log in or register a user. Register the user interface and enter information according to the format requirements specified in the regular expression. If there is any error, re-enter.
2. Click to register and first connect to the SQLserver database. If the connection is successful, it will determine whether the user name already exists. If it exists, a prompt will be given. Otherwise, register.
3. Log in interface. When clicking the login button, first establish a connection with the database. Search in the database according to the user name and password. If so, the login will be successful. Otherwise, give a prompt.
4. Using singleton mode, we can only create an object of SQLserver-like, which greatly saves memory overhead.
See: https://github.com/chaohuangtianjie994/A-login-register-System
5. Based on this, a large number of expansion functions can be carried out.
The code is as follows:
UserRegister.java login interface.
package package1; /* * Function: The login interface has the registration function and the registration interface pops up. * Save the registered information in the database and log in can be performed. * 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 component//Create component jb1=new JButton("Login"); jb2=new JButton("Register"); jb3=new JButton("Exit"); //Set listening jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jlb1=new JLabel("Username:"); jlb2=new JLabel("Password:"); 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("Register 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 to each button if(e.getActionCommand()=="Exit") { System.exit(0); }else if(e.getActionCommand()=="Login") { //Call the login method this.login(); }else if(e.getActionCommand()=="Register") { //Call the registration method this.Regis(); } } //Register method public void Regis() { this.dispose(); //Close the current interface new UI(); //Open the 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 is used for the page display of registration. Regular expressions are used to specify the input content. When registering, you need to first determine whether the user name exists. If it exists, a prompt is given, otherwise register.
package package1; import java.awt.event.*; import java.awt.*; import javax.swing.*; /* * Registration interface. */ class UI extends JFrame implements ActionListener{ //Define component JFrame jf; JPanel jp; JLabel jl1,jl2,jl3,jl4; JTextField jtf1,jtf2,jtf3,jtf4; JButton jb1,jb2; public UI() { //Initialize component jf=new JFrame(); jp=new JPanel(); jl1=new JLabel("Please enter the user name: "); jtf1=new JTextField(10); jtf1.setToolTipText("The user name must be 3-6-digit letters_or numbers"); jl2=new JLabel("Please enter password: "); jtf2=new JTextField(10); jtf2.setToolTipText("Password must be 6-digit letters_or numbers"); jl3=new JLabel("Please enter name: "); jtf3=new JTextField(10); jtf3.setToolTipText("The name must be 2-4 digits of Chinese characters"); jl4=new JLabel("Please enter student number: "); jtf4=new JTextField(10); jtf4.setToolTipText("Student number must be 3-6 digits"); jb1=new JButton("Return"); jb1.setToolTipText("Click me to return to the login interface"); jb2=new JButton("Register"); 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("Register interface"); 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()=="Register") { //Calling the registration method this.zhuce(); } } public void zhuce() { String regex1="//w{3,6}"; //The user name must be 3-6-bit boolean flag1=jtf1.getText().matches(regex1); String regex2="//w{6}"; //The password must be 6-bit boolean flag2=jtf2.getText().matches(regex2); String regex3="[//u4e00-//u9fa5]{2,4}"; //The name must be 2-4 Chinese characters boolean flag3=jtf3.getText().matches(regex3); String regex4="//d{3,6}"; // The student number must be 3-6-bit 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, "User name is filled incorrectly, must be 3-6 letters or numbers", "Prompt message", JOptionPane.WARNING_MESSAGE); jtf1.setText(""); }else if(flag2==false) { JOptionPane.showMessageDialog(null, "Password is filled incorrectly, must be 6 letters or numbers", "Prompt message", JOptionPane.WARNING_MESSAGE); jtf2.setText(""); }else if(flag3==false) { JOptionPane.showMessageDialog(null, "Incorrect name filling, 2-4 Chinese characters must be 2-4 digits", "Prompt message", JOptionPane.WARNING_MESSAGE); jtf3.setText(""); }else if(flag4==false) { JOptionPane.showMessageDialog(null, "Incorrect student number filling, must be 3-6 digits", "Prompt message", JOptionPane.WARNING_MESSAGE); jtf4.setText(""); }else { //Calling the registration method/First check whether the user name to be registered 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 implements various functions such as connection to the database and query verification.
package package1; import java.sql.*; import javax.swing.JOptionPane; /* * Operations related to databases, encapsulated separately into classes*/ class SQLserver { Connection ct; PreparedStatement ps; ResultSet rs; String user,pwd; //Encapsulate the method connecting to the database into a method public void ConnectSQL() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load the driver ct=DriverManager.getConnection("jdbc:odbc:ywq"); //Get connection System.out.println("Connected database..."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //Method for registering user public void UserRegis(String a,String b,String c,String d) { //Create rocket try { ps=ct.prepareStatement("insert into users values(?,?,?,?)"); ps.setString(1,a); ps.setString(2,b); ps.setString(3,c); ps.setString(4,d); //Execute int i=ps.executeUpdate(); if(i==1) { JOptionPane.showMessageDialog(null, "Registered Success", "Prompt Message",JOptionPane.WARNING_MESSAGE); }else { JOptionPane.showMessageDialog(null, "Register failed", "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 username=? and password=? "); ps.setString(1, a); ps.setString(2, b); // ResultSet result set, you can understand ResultSet as the result set that returns a table row rs = ps.executeQuery(); if(rs.next()) { user = rs.getString(1); pwd = rs.getString(2); JOptionPane.showMessageDialog(null, "Login successfully! ! ! ", "Prompt Message", JOptionPane.WARNING_MESSAGE); System.out.println("Successfully obtained the password and username from database"); System.out.println(user + "/t" + pwd + "/t"); }else { JOptionPane.showMessageDialog(null, "User name or password is incorrect, please re-enter!", "Prompt Message", JOptionPane.ERROR_MESSAGE); } } catch (SQLException e) { e.printStackTrace(); } } //Register verification method to determine whether the username already has public void ZhuceVerify(String a) { try { ps=ct.prepareStatement("select * from users where username=?"); // System.out.println(ps); ps.setString(1, a); rs=ps.executeQuery(); if(rs.next()) { JOptionPane.showMessageDialog(null, "This username already exists", "Prompt message", JOptionPane.WARNING_MESSAGE); }else { // Register UI ui=new UI(); this.UserRegis(ui.jtf1.getText(),ui.jtf2.getText(),ui.jtf3.getText(),ui.jtf4.getText()); } } catch (SQLException e) { e.printStackTrace(); } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.