Login to register the small code, will learn some of the small knowledge to combine to use, deepen the impression. In this case, if there is no detailed comment, see other blogs.
Java Program Operations Database SQL Server detailed
Features: Simple login registration system, using database SQL Server, singleton mode, regular expression and graphical development and other knowledge.
1, in the login interface, you can login or register users. Register the user interface, enter the information according to the format requirements specified in the regular expression, and re-enter it if it is wrong.
2, click on the registration, the first connection to SQL Server database, connection success will determine whether the user name already exists, if present, give a hint. Otherwise, it is registered.
3, login interface, click the Login button, first establish a connection with the database. Follow the user name and password to locate the database and, if so, log on successfully. Instead, give a hint.
4. By using singleton mode, we realized that only one object of class SQL Server was created, which greatly saved the memory overhead.
Complete code of the program 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;/* * Features: Login interface with registration function, pop up the 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.*;p ublic 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 ("register"); jb3=new JButton ("exit");// Set monitoring Jb1.addactionlistener (this); Jb2.addactionlistener (this); Jb3.addactionlistener (this); Jlb1=new JLabel ("Username:") ; jlb2=new JLabel ("Password:"); jtf=new JTextField (ten); Jpf=new JPasswordField (ten); 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);. Settitle ("Register login Interface"); This.setlayout (new GridLayout (3,1)); This.setdefaultcloseoperation (Jframe.exit_on_close); This.setbounds (300, 200, 300, 180);} @Overridepublic void actionperformed (ActionEvent e) {//Listen for 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 the 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 ("");}}
ui.java the page display for registration. A regular expression is used to specify what is entered. Registration, you need to first determine whether the user name exists, if present, give a hint, reverse registration.
Package Package1;import java.awt.event.*;import Java.awt.*;import javax.swing.*;/* * Registration screen. */class UI extends JFrame implements actionlistener{//define components 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 user name:"); jtf1=new JTextField (10) ; Jtf1.settooltiptext ("Username must be 3-6 letters _ or numbers"), Jl2=new JLabel ("Please enter password:"); jtf2=new JTextField () jtf2.settooltiptext (" The password must be 6 letters _ or number "); Jl3=new JLabel (" Please enter Name: "); jtf3=new JTextField (" Name must be 2-4 characters "); Jtf3.settooltiptext JLabel ("Please enter the study number:"); jtf4=new JTextField, Jtf4.settooltiptext ("study number must be 3-6 digits"); Jb1=new JButton ("return"); Jb1.settooltiptext ("Point me back to login screen"); 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 ("Registration Interface"); This.setbounds (200, 100, 250 );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") {//Call registration Method This.zhuce ();}} public void Zhuce () {String regex1= "\\w{3,6}";//the user name must be a 3-6-bit boolean flag1=jtf1.gettext (). matches (REGEX1); String regex2= "\\w{6}"; The password must be a 6-bit Boolean flag2=jtf2.gettext (). matches (REGEX2); String regex3= "[\\u4e00-\\u9fa5]{2,4}"; Name must be kanji 2-4 characters boolean flag3=jtf3.gettext (). matches (REGEX3); String regex4= "\\d{3,6}"; The study number must be a 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, "username filled in error, must be 3-6-bit letter _ or number", "hint message", Joptionpane.warning_message); Jtf1.settext ("");} else if (flag2==false) {Joptionpane.showmessagedialog (null, "Password fill error, must be 6-bit letter _ or number", "hint message", Joptionpane.warNing_message); Jtf2.settext ("");} else if (flag3==false) {Joptionpane.showmessagedialog (null, "name fill error, must kanji 2-4 bits", "hint message", joptionpane.warning_message); Jtf3.settext ("");} else if (flag4==false) {Joptionpane.showmessagedialog (NULL, "study number filled in error, must be 3-6 digits", "hint message", joptionpane.warning_message) ; Jtf4.settext ("");} else{//Call registration Method/Check whether the username to be registered exists in 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 (""); } }}
Sqlserver.java implements various functions, such as connection to the database and query validation.
Package Package1;import Java.sql.*;import javax.swing.joptionpane;/* * Database-related operations, individually encapsulated into class */class SQL Server {Connection Ct PreparedStatement PS; ResultSet rs; String user,pwd;//encapsulates the method that connects the database as a method public void Connectsql () {try {class.forname ("sun.jdbc.odbc.JdbcOdbcDriver"); Load driver ct=drivermanager.getconnection ("Jdbc:odbc:ywq"); Get the connection System.out.println ("Database successfully connected ...");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Method for registering a user public void Userregis (String a,string b,string c,string D) {//create Rocket car try {ps=ct.preparestatement ("insert INTO Users values (?,?,?,?) "); Ps.setstring (1,a);p s.setstring (2,b);p s.setstring (3,c);p s.setstring (4,d);//execute int i=ps.executeupdate (); if (i==1) { Joptionpane.showmessagedialog (NULL, "registered successfully", "prompt Message", joptionpane.warning_message); }else{joptionpane.showmessagedialog (NULL, "Registration failed", "prompt Message", joptionpane.error_message);}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Login authentication method public void SQLVerify (String a,string b) {try {PS=CT.PReparestatement ("SELECT * from users where user name =?") and password =? ");p s.setstring (1, a);p s.setstring (2, b);//resultset result set, you can interpret resultset as a result set that returns a table row rs = Ps.executequery (); if ( Rs.next ()) {user = Rs.getstring (1);p WD = rs.getstring (2); Joptionpane.showmessagedialog (NULL, "Login successful!!! "," Prompt Message ", joptionpane.warning_message); SYSTEM.OUT.PRINTLN ("Successful access to password and user name 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 the authentication method to determine if the user name already exists public void zhuceverify (String a) {try {ps=ct.preparestatement ("select * from users where user name =?"); /SYSTEM.OUT.PRINTLN (PS);p s.setstring (1, a); Rs=ps.executequery (); if (Rs.next ()) {Joptionpane.showmessagedialog ( NULL, "The user name already exists", "hint message", joptionpane.warning_message);} else{//to register UI Ui=new UI (); Userregis (Ui.jtf1.getText (), Ui.jtf2.getText (), Ui.jtf3.getText (), Ui.jtf4.getText ());}} catch (SQLException e) {e.printstacktrace ();}}}
Java graphical interface development-Simple login Registration Applet