Java Simple Login Register applet _java

Source: Internet
Author: User
Tags gettext odbc

Login Registration Small Code, will learn some of the small knowledge fused together for use, deepen the impression. In this case, if there are no details of the comments, see other blogs.

Function Introduction: Simple Login registration system, using database SQL Server, single case mode, regular expression and graphical development and other knowledge.

1, in the login interface, you can login or registered users. Register the user interface, enter the information according to the format required by the regular expression, and re-enter it if it is wrong.
2, click Registration, the first connection to the SQL Server database, the success of the connection will determine whether the user name already exists, if there is a hint. On the contrary, register.
3, login interface, click on the Login button, first to establish a connection with the database. Follow the username and password to find the database, and if so, log in successfully. On the contrary, give hints.
4, the use of a single example mode, the implementation of the only class SQL Server to create an object, greatly saving memory overhead.
Program complete code See: Https://github.com/chaohuangtianjie994/A-login-register-System
5, based on this, can carry out a large number of expansion functions.

The code is as follows:

Userregister.java Login interface.

Package package1; 
 * * Function: Login interface with the registration function, pop-up 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.*; 
  The public class Userregister extends JFrame implements actionlistener{//defines 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 ("registered"); 
  Jb3=new JButton ("exit"); 
  Set up 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) {//Monitor individual buttons if (e.getactioncommand () = "Exit") { 
  System.exit (0); 
  }else if (e.getactioncommand () = "Login") {//Call login method This.login (); }else if (e.getactioncommand () = "Register") {//Call registration method this. 
  Regis (); 
 
 }//Registration method public void Regis () {this.dispose ();//close the current interface new UI ();//Open New Interface} 
  Login method public void login () {SQL Server s=new SQL Server (); 
  S.connectsql (); 
   
  S.sqlverify (Jtf.gettext (), Jpf.gettext ()); 
  This.jtf.setText (""); 
   
 This.jpf.setText (""); 
 }
} 
 

The

ui.java   the page displayed for registration. A regular expression is used to specify what is entered. Registration, you need to first determine whether the user name exists, if there is a hint, and vice versa.

Package package1; 
Import java.awt.event.*; 
 
Import java.awt.*; 
 
Import javax.swing.*; 
 * * Registration interface. 
 * * Class UI extends JFrame implements actionlistener{//definition component 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 ("username must be 3-6-digit letter _ or number"); 
  Jl2=new JLabel ("Please enter password:"); 
  Jtf2=new JTextField (10); 
  Jtf2.settooltiptext ("Password must be 6-digit letter _ or number"); 
  Jl3=new JLabel ("Please enter your name:"); 
  Jtf3=new JTextField (10); 
  Jtf3.settooltiptext ("Name must be 2-4 characters"); 
  Jl4=new JLabel ("Please enter the school number:"); 
  Jtf4=new JTextField (10); 
   
  Jtf4.settooltiptext ("The school number must be 3-6 digits"); 
  Jb1=new JButton ("return"); 
  Jb1.settooltiptext ("Point I return Login interface Oh"); 
  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 ("Registration Interface"); 
  This.setbounds (200, 100, 250, 150); 
  This.setvisible (TRUE); 
This.setdefaultcloseoperation (Jframe.exit_on_close); 
   
   
   
 This.setresizable (FALSE); The public void actionperformed (ActionEvent e) {if (E.getactioncommand () = = "return") {This.dispose ( 
   ); 
New Userregister (); 
    
  System.out.println ("-------"); 
    
  }else if (e.getactioncommand () = "registered") {//Call registration Method This.zhuce (); The public void Zhuce () {String regex1= "\\w{3,6}";//The username must be a 3-6-bit boolean flag1=jtf1.gettext (). Matches (Rege 
   
  X1); String regex2= "\\w{6}"; 
   
  The password must be a 6-bit Boolean flag2=jtf2.gettext (). matches (REGEX2); String regex3= "[\\u4e00-\\u9fa5]{2,4}"; 
   
  The name must be a 2-4-character Boolean Flag3=jtf3.gettext (). matches (REGEX3); String regex4= "\\d{3,6}"; The school number must be a 3-6-digit 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 fills in error, must be 3-6-digit letter _ or number", "Hint info", J 
   Optionpane.warning_message); 
  Jtf1.settext (""); }else if (flag2==false) {Joptionpane.showmessagedialog (null, "Password fill error, must be 6-digit letter _ or number", "hint", joptionpane.warning_mes 
   SAGE); 
  Jtf2.settext (""); }else if (flag3==false) {Joptionpane.showmessagedialog (null, "name fill error, must be kanji 2-4", "Hint info", joptionpane.warning_message 
   ); 
  Jtf3.settext (""); }else if (flag4==false) {Joptionpane.showmessagedialog (null, "number fill error, must be 3-6 digits", "hint info", JOPTIONPANE.WARNING_MESSAG 
   E); 
  Jtf4.settext (""); 
    }else {//Call registration method/Check to see if the username being registered has SQL Server ss=new SQL Server (); 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 (""); 
 } 
 } 
  
}

The

Sqlserver.java implements various functions such as connection to a database and query validation.

Package package1; 
 
Import java.sql.*; 
Import Javax.swing.JOptionPane; 
 * * * Database-related operations, individually packaged into class * * SQL Server {Connection ct; 
 PreparedStatement PS; 
 ResultSet rs; 
  
 String user,pwd; 
    
   Encapsulates the method of connecting a database as a method public void Connectsql () {try {class.forname ("sun.jdbc.odbc.JdbcOdbcDriver");//Load Driver Ct=drivermanager.getconnection ("Jdbc:odbc:ywq"); 
    
  Get Connection System.out.println ("Successfully connected Database ..."); 
  catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); The user's method public void Userregis (String a,string b,string c,string D) {///create rocket motor try {Ps=ct.prepar 
   Estatement ("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, "Registration failed", "prompt Message", joptionpane.error_message); 
  The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }//Logon authentication method public void SQLVerify (String a,string b) {try {ps=ct.preparestatement ("select *") s where user name =? and password =? 
   "); 
   Ps.setstring (1, a); 
    
   Ps.setstring (2, B); 
    
   ResultSet result set, you can understand resultset to return a table row of the result set rs = Ps.executequery (); 
    if (Rs.next ()) {user = rs.getstring (1); 
    PWD = rs.getstring (2); Joptionpane.showmessagedialog (NULL, "Login succeeded!!! 
    "," Prompt Message ", joptionpane.warning_message); 
    System.out.println ("Successfully acquired password and username from database"); 
   System.out.println (user + "T" + pwd + "T"); }else {joptionpane.showmessagedialog (null, "username or password is wrong, please re-enter!") 
     
   "," Prompt Message ", joptionpane.error_message); 
  } catch (SQLException e) {e.printstacktrace (); }//Register authentication method to determine if the user name already exists with public void zhuceverify (String a) {try {ps=ct.Preparestatement ("SELECT * from users where user name =?"); 
   SYSTEM.OUT.PRINTLN (PS); 
    
   Ps.setstring (1, a); 
   Rs=ps.executequery (); 
   if (Rs.next ()) {Joptionpane.showmessagedialog (null, "The username already exists", "hint info", 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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.