Click the Register button, the registration user panel appears, the mobile phone number and the same password to add to the MySQL database
Working principle:
Unlike a stand-alone software, this chat login box does not add registration information directly into the database
Instead, the registration information should be sent to the server
When the server receives the registration information, it adds the registration information to the database on the service side.
First, make the connection database ready
Connecting to a database requires a driver package that connects to the database--mysql-connector-java-5.1.7-bin.jar
If you forget to pour the package connected to the MySQL database, there will be a java.lang.ClassNotFoundException:com.mysql.jdbc.Driver problem
: Link: https://pan.baidu.com/s/1geBRqqn Password: 8JXM
Then, the statement that connects the database is made into a tool class, convenient to call
Dbutil class, responsible for connecting the existing database Sw_database (with the Dbutil.getconn () method)
Shut down various resources (using the Dbutil.closeall () method) after connecting to the database
Package Com.swift.jdbc;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.PreparedStatement; Import Java.sql.resultset;import Java.sql.sqlexception;public class Dbutil {public static Connection Getconn () { Connection Conn=null; try {class.forname ("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); } try {String url= "jdbc:mysql://localhost:3306/sw_database"; String user= "root"; String password= "root"; Conn=drivermanager.getconnection (URL, user, password); } catch (SQLException e) {e.printstacktrace (); } return conn; } public static void CloseAll (Connection conn,preparedstatement ps,resultset rs) {if (conn!=null) { try {conn.close (); } catch (SQLException e) {E.printstackTrace (); }} if (Ps!=null) {try {ps.close (); } catch (SQLException e) {e.printstacktrace (); }} if (Rs!=null) {try {rs.close (); } catch (SQLException e) {e.printstacktrace (); } } }}
Next, you need to add the user registration information into the Sw_user table of the database
Therefore, build a tool class dbadd, responsible for adding user registration information to the table (with the Dbadd.add (user user) method)
Package Com.swift.jdbc;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.SQLException; public class Dbadd {public static void Add (user user) { String phone = User.getusername (); String password = User.getpassword (); Connection conn = Dbutil.getconn (); PreparedStatement Ps=null; try { PS = conn.preparestatement ("INSERT into Sw_user (Username,password) VALUES (?,?)"); Ps.setstring (1, phone); Ps.setstring (2, password); int count = Ps.executeupdate (); if (count = = 1) { System.out.println ("user added Success"); } else { System.out.println ("user added Failed");} } catch (SQLException e) { e.printstacktrace (); } finally { Dbutil.closeall (conn, PS, NULL);}}}
The user class code is as follows: responsible for the packaging of users ' information
package Com.swift.jdbc;public class User {private int id; Private String username; private String password; public int getId () {return id; } public void setId (int id) {this.id = ID; } public String GetUserName () {return username; } public void Setusername (String username) {this.username = username; } public String GetPassword () {return password; } public void SetPassword (String password) {this.password = password; The public User (int ID, string username, string password) {this.id = ID; This.username = Username; This.password = password; Public User () {super (); The public User (string Username, string password) {this.username = username; This.password = password; } @Override Public String toString () {return "User [id=" + ID + ", username=" + Username + ", password=" + p Assword + "]"; } }
Next, in the Logindialog login window "Register user" button to add monitoring, when the click button to send the message to the server
Information sent to the server, need to establish a connection with the server, and close the window to disconnect
Send phone numbers and verified passwords separately after connecting
The specific code is as follows:
Package Com.swift.frame;import Java.awt.eventqueue;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;import Java.io.datainputstream;import Java.io.dataoutputstream;import Java.io.ioexception;import Java.net.connectexception;import Java.net.socket;import Java.sql.connection;import java.sql.PreparedStatement; Import Java.sql.sqlexception;import javax.swing.jbutton;import javax.swing.jdialog;import javax.swing.JFrame; Import Javax.swing.jlabel;import Javax.swing.jpanel;import Javax.swing.jpasswordfield;import Javax.swing.jtextfield;import Javax.swing.uimanager;import Javax.swing.unsupportedlookandfeelexception;import Javax.swing.border.titledborder;import Com.swift.jdbc.dbadd;import Com.swift.jdbc.dbutil;import Com.swift.jdbc.user;import Com.swift.util.center;public class Logindialog extends JDialog {private JPasswordField Pass Wordfield_2; Private JPasswordField passwordfield_1; Private JtexTfield textfield_2; Private JTextField TextField; Socket s; DataOutputStream dos; DataInputStream dis; public static void Main (String args[]) {jframe.setdefaultlookandfeeldecorated (true); Jdialog.setdefaultlookandfeeldecorated (TRUE); try {uimanager.setlookandfeel ("Javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (ClassNotFoundException E1) {e1.printstacktrace (); } catch (Instantiationexception E1) {e1.printstacktrace (); } catch (Illegalaccessexception E1) {e1.printstacktrace (); } catch (Unsupportedlookandfeelexception E1) {e1.printstacktrace (); } eventqueue.invokelater (New Runnable () {public void run () {try {Lo Gindialog dialog = new Logindialog (); Dialog.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) { System.exit (0); } }); Dialog.setvisible (TRUE); } catch (Exception e) {e.printstacktrace (); } } }); } public Logindialog () {super (); Setresizable (FALSE); Settitle ("Online chat login box"); Getcontentpane (). setlayout (NULL); SetBounds (100, 100, 427, 301);//Register height is 578, do not register is 301//Set Window Center This.setlocation (center.getpoint (This.getsize () )); Final JButton button_1 = new JButton (); Button_1.settext ("login"); Button_1.setbounds (230, 222, 106, 36); Getcontentpane (). Add (button_1); Final JTextField textfield_1 = new JTextField (); Textfield_1.setbounds (148, 90, 192, 42); Getcontentpane (). Add (Textfield_1); Final JLabel label = new JLabel (); Label.settext ("account number"); Label.setbounds (76, 102, 66, 18); Getcontentpane (). Add (label); Final JLabel Label_1 = new JLabel (); Label_1.settext ("password"); Label_1.setbounds (76, 167, 66, 18); Getcontentpane (). Add (Label_1); Final JPasswordField Passwordfield = new JPasswordField (); Passwordfield.setbounds (148, 155, 192, 42); Getcontentpane (). Add (Passwordfield); Final JButton button = new JButton (); Button.addactionlistener (new ActionListener () {public void actionperformed (final ActionEvent e) { if (LoginDialog.this.getHeight () = = 301) {LoginDialog.this.setSize (427, 578); } else {LoginDialog.this.setSize (427, 301); }//Settings window is continuously centered LoginDialog.this.setLocation (Center.getpoint (LoginDialog.this.getSize ())); } }); Button.settext ("registration"); Button.setbounds (76, 222, 106, 36); Getcontentpane (). Add (button); Final JPanel panel = new JPanel (); Panel.setlayout (NULL); Panel. SetBorder (new Titledborder (NULL, "Registered user", Titledborder.default_justification, Titledborder.default_position , NULL, NULL)); Panel.setbounds (10, 278, 401, 226); Getcontentpane (). Add (panel); Final JLabel label_2 = new JLabel (); Label_2.setbounds (44, 41, 65, 18); Label_2.settext ("Mobile phone Number:"); Panel.add (label_2); TextField = new JTextField (); Textfield.setbounds (115, 35, 225, 30); Panel.add (TextField); Final JButton button_2 = new JButton (); Button_2.settext ("Send Verification"); Button_2.setbounds (243, 75, 97, 30); Panel.add (button_2); Textfield_2 = new JTextField (); Textfield_2.setbounds (115, 104, 95, 30); Panel.add (textfield_2); Final JLabel label_3 = new JLabel (); Label_3.settext ("Verification Code:"); Label_3.setbounds (44, 110, 65, 18); Panel.add (Label_3); Passwordfield_1 = new JPasswordField (); Passwordfield_1.setbounds (115, 143, 231, 30); PanEl.add (passwordfield_1); Passwordfield_2 = new JPasswordField (); Passwordfield_2.setbounds (115, 175, 231, 30); Panel.add (passwordfield_2); Final JLabel label_4 = new JLabel (); Label_4.settext ("Password:"); Label_4.setbounds (44, 149, 65, 18); Panel.add (Label_4); Final JLabel label_5 = new JLabel (); Label_5.settext ("Verify password:"); Label_5.setbounds (44, 181, 65, 18); Panel.add (label_5); Final JButton button_3 = new JButton (); Button_3.setbounds (47, 510, 97, 30); Getcontentpane (). Add (Button_3); Button_3.settext ("waiver"); Final JButton button_4 = new JButton (); Button_4.addactionlistener (new ActionListener () {public void actionperformed (final ActionEvent e) { String phone = Textfield.gettext (); String password = null; String Str1=new string (Passwordfield_1.getpassword ()). Trim (); String str2=new String (Passwordfield_2.getpassword ()). Trim (); if (Str1.equals (str2)) {password = new String (Passwordfield_2.getpassword ()). Trim (); } else {System.out.println ("Enter password inconsistent ..."); System.exit (0); } try {Dos.writeutf (phone); Dos.writeutf (password); } catch (IOException E1) {e1.printstacktrace (); } } }); Button_4.setbounds (262, 510, 97, 30); Getcontentpane (). Add (Button_4); Button_4.settext ("Registered user"); Connect (); This.addwindowlistener (New Windowadapter () {@Override public void windowclosed (WindowEvent e) { Disconnect (); } }); } public void Connect () {try {s = new Socket ("127.0.0.1", 8888); System.out.println ("a customerEnd of the landing ....!!); DOS = new DataOutputStream (S.getoutputstream ()); dis = new DataInputStream (S.getinputstream ()); } catch (Connectexception e) {System.out.println ("service-side exception ..."); SYSTEM.OUT.PRINTLN ("Please verify that the server is open ..."); } catch (IOException e) {e.printstacktrace (); }} public void disconnect () {try {if (Dos! = null) dos.close (); if (s! = null) s.close (); } catch (IOException e) {e.printstacktrace (); } } }
Server to accept the client login window to send the mobile phone number and password
The information is then packaged into user and added to the database via Dbadd.add (user)
Create a Loginserver class that takes care of receiving information and adding it into the database
The specific code is as follows:
Package Com.swift;import Java.io.datainputstream;import Java.io.dataoutputstream;import java.io.EOFException; Import Java.io.ioexception;import java.net.bindexception;import java.net.serversocket;import java.net.Socket; Import Java.net.socketexception;import Com.swift.jdbc.dbadd;import Com.swift.jdbc.user;public class LoginServer {b Oolean started = false; ServerSocket SS = null; Socket s = null; public static void Main (string[] args) {new Loginserver (). Fun (); } private void Fun () {try {ss = new ServerSocket (8888); started = true; } catch (Bindexception e) {System.out.println ("Port in Use ..."); } catch (IOException E1) {e1.printstacktrace (); } try {while (started) {s = ss.accept (); SYSTEM.OUT.PRINTLN ("A Client connected success"); Client C = new client (s); New Thread (c). Start (); }} CatCH (eofexception e) {System.out.println ("client has closed."); } catch (Exception e) {e.printstacktrace (); } finally {try {ss.close (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }}}} class Client implements Runnable {private Socket s; Private DataInputStream dis; Private DataOutputStream dos; Private Boolean connected = false; Public Client (Socket s) {this.s = s; try {This.dis = new DataInputStream (S.getinputstream ()); This.dos = new DataOutputStream (S.getoutputstream ()); connected = true; } catch (IOException e) {e.printstacktrace (); }} @Override public void Run () {try {//Note: to include a while loop if the try is in a while loop, a socket closed exception occurs while (COnnected) {String phone = Dis.readutf (); String password = Dis.readutf (); SYSTEM.OUT.PRINTLN (phone); SYSTEM.OUT.PRINTLN (password); User User=new User (Phone,password); Dbadd.add (user); }} catch (SocketException e) {System.out.println ("A login window has been closed ...."); } catch (IOException e) {e.printstacktrace (); } finally {if (s! = null) {try {s.close (); } catch (IOException e) {e.printstacktrace (); }} if (dis! = null) {try {dis.close (); } catch (IOException e) {e.printstacktrace (); } } if (dos!=null) {try {dos.close (); } catch (IOException e) {e.printstacktrace (); } } } } }}
:
Open more than one login window, and then close after registering the user separately, the server prompt message is as follows:
Java Online Chat Project client Login window Logindialog registered user function