In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Java and MySQL database to achieve registration and login related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this article on how to use Java and MySQL database to achieve registration and login will have a harvest, let's take a look.
Design ideas
1. Database design-after understanding the needs of users, the database is designed according to the needs of users.
2. Data model-- that is, the structure of database tables is encapsulated in one class.
3. Data processing tool class-A tool class is designed to deal with data according to the requirements given by the data model.
4. View display-finally, a convenient view interface is provided to the user.
Concrete design
1. First of all, the table should be built in the database.
2. Make the corresponding data model according to the business requirements. This design is mainly about login and registration, so two data models are designed.
(1) Registration model
Package Chapter_14.Part_8;public class Register {/ / Registration Model String id; String password; public String getId () {return id;} public void setId (String id) {this.id = id;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;}}
(2) Login model
Package Chapter_14.Part_8;public class Login {/ / login model String id; String password; boolean loginSuccess = false; public String getId () {return id;} public void setId (String id) {this.id = id;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password } public boolean isLoginSuccess () {return loginSuccess;} public void setLoginSuccess (boolean loginSuccess) {this.loginSuccess = loginSuccess;}}
3. According to the two data models designed above, the corresponding data processing tool classes are designed respectively.
(1) Registration model processing tool class
Package Chapter_14.Part_8;import Chapter_14.GetDatabaseConnection;import javax.swing.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class registerHandle {/ / registered data processor Connection connection = null; PreparedStatement presql; / / preprocessing object String sql; public registerHandle () {connection = GetDatabaseConnection.connectDB ("book", "root", "") } public void writeRegister (Register register) {int ok = 0; try {sql = "insert into register value (?,?)"; / / pre-processing presql = connection.prepareStatement (sql); presql.setString (1) register.getId (); presql.setString (2) register.getPassword (); ok = presql.executeUpdate () / / the successful plug-in returns 1 connection.close ();} catch (SQLException e) {e.printStackTrace (); JOptionPane.showMessageDialog (null, "ID cannot be repeated", "warning", JOptionPane.WARNING_MESSAGE);} if (okalone 0) {JOptionPane.showMessageDialog (null, "registration successful", "congratulations", JOptionPane.WARNING_MESSAGE) }}}
(2) Login model processing tool class
Package Chapter_14.Part_8;import Chapter_14.GetDatabaseConnection;import javax.swing.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class loginHandle {Connection connection = null; PreparedStatement presql; String sql; ResultSet resultSet; boolean loginSuccess; public loginHandle () {connection = GetDatabaseConnection.connectDB ("book", "root", "") } public void readLogin (Login login) {try {sql = "select * from register where ID =? And password =? "; / / mainly use and presql = connection.prepareStatement (sql); presql.setString (1, login.getId ()); presql.setString (2, login.getPassword ()); resultSet = presql.executeQuery (); if (resultSet.next ()) {login.setLoginSuccess (true) JOptionPane.showMessageDialog (null, "login success", "congratulations", JOptionPane.WARNING_MESSAGE);} else {login.setLoginSuccess (false); JOptionPane.showMessageDialog (null, "login failure", "prompt", JOptionPane.WARNING_MESSAGE);} connection.close (); loginSuccess = login.isLoginSuccess () } catch (SQLException e) {e.printStackTrace ();}
4. A test class can be designed to verify whether the program can pass normally. If you can pass, you can start designing the view.
(1) Design of registered view
Package Chapter_14.Part_8;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class registerPanel extends JPanel implements ActionListener {Register register; registerHandle registerHandle; JTextField jTextFieldID; JTextField jTextFieldPassword; JButton jregister; public registerPanel () {/ / setLayout (new FlowLayout ()); / / setVisible (true); init (); validate () } void init () {jTextFieldID = new JTextField (12); add (new JLabel ("ID:")); add (jTextFieldID); jTextFieldPassword = new JTextField (12); jTextFieldPassword.addActionListener (this); add (new JLabel ("PASSWORD:")); add (jTextFieldPassword); jregister = new JButton ("Register"); jregister.addActionListener (this); add (jregister) } @ Override public void actionPerformed (ActionEvent actionEvent) {register = new Register (); register.setId (jTextFieldID.getText ()); register.setPassword (jTextFieldPassword.getText ()); registerHandle = new registerHandle (); registerHandle.writeRegister (register); jTextFieldPassword.setText (null);}}
(2) Design of login view
Package Chapter_14.Part_8;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class loginPanel extends JPanel implements ActionListener {Login login; loginHandle loginHandle; JTextField jTextFieldID; JPasswordField jTextFieldPassword; JButton jLogin; public loginPanel () {/ / setLayout (new FlowLayout ()); / / setVisible (true); init (); validate () } void init () {jTextFieldID = new JTextField (12); add (new JLabel ("ID:")); add (jTextFieldID); jTextFieldPassword = new JPasswordField (12); jTextFieldPassword.addActionListener (this); add (new JLabel ("PASSWORD:")); add (jTextFieldPassword); jLogin = new JButton ("Login"); jLogin.addActionListener (this); add (jLogin) } public boolean isLoignSuccess () {return loginHandle.loginSuccess;} @ Override public void actionPerformed (ActionEvent actionEvent) {login = new Login (); login.setId (jTextFieldID.getText ()); char [] p = jTextFieldPassword.getPassword (); login.setPassword (new String (p)); loginHandle = new loginHandle (); loginHandle.readLogin (login);}
5. Combine the above two view classes into one class to facilitate later maintenance and modification.
Package Chapter_14.Part_8;import javax.swing.*;import java.awt.*;public class combinePanel extends JPanel {JTabbedPane jTabbedPane; registerPanel registerPanel; loginPanel loginPanel; public combinePanel () {/ / setLayout (new BorderLayout ()); if not set, jTabbedPane = new JTabbedPane (); registerPanel = new registerPanel (); loginPanel = new loginPanel (); jTabbedPane.addTab ("Registration Interface", registerPanel) JTabbedPane.addTab (login interface, loginPanel); add (jTabbedPane, BorderLayout.CENTER); jTabbedPane.validate ();} public boolean isLoginSuccess () {return loginPanel.isLoignSuccess ();}}
6. After everything is designed, it can be run. Set the main entry of a program.
Package Chapter_14.Part_8;public class Example14_9 {public static void main (String [] args) {myWindow myWindow = new myWindow ();}} this is the end of the article on "how to register and log in with Java and MySQL databases". Thank you for reading! I believe that everyone has a certain understanding of "how to use Java and MySQL database to achieve registration and login" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.