您好,登錄后才能下訂單哦!
這篇文章主要介紹了怎么用Java和MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)注冊(cè)和登錄的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇怎么用Java和MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)注冊(cè)和登錄文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
1、數(shù)據(jù)庫(kù)設(shè)計(jì)——在了解用戶的需求之后,根據(jù)用戶的這些需求進(jìn)行數(shù)據(jù)庫(kù)的設(shè)計(jì)。
2、數(shù)據(jù)模型——就是把數(shù)據(jù)庫(kù)表的結(jié)構(gòu)全都封裝在一個(gè)類當(dāng)中。
3、數(shù)據(jù)處理工具類——根據(jù)數(shù)據(jù)模型給出的需求設(shè)計(jì)一個(gè)工具類來(lái)處理數(shù)據(jù)。
4、視圖展示——最后提供給用戶一個(gè)交互方便的視圖界面。
1、首先要在數(shù)據(jù)庫(kù)中建好表。
2、根據(jù)業(yè)務(wù)需求,制定相應(yīng)的數(shù)據(jù)模型。本次設(shè)計(jì)主要是登錄和注冊(cè)兩個(gè)內(nèi)容,因此設(shè)計(jì)了兩個(gè)數(shù)據(jù)模型。
(1)注冊(cè)模型
package Chapter_14.Part_8;
public class Register { //注冊(cè)模型
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)登錄模型
package Chapter_14.Part_8;
public class Login { //登錄模型
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、根據(jù)以上設(shè)計(jì)好的兩個(gè)數(shù)據(jù)模型,分別設(shè)計(jì)相應(yīng)的數(shù)據(jù)處理的工具類。
(1)注冊(cè)模型處理工具類
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 { //注冊(cè)數(shù)據(jù)處理者
Connection connection = null;
PreparedStatement presql; //預(yù)處理對(duì)象
String sql;
public registerHandle() {
connection = GetDatabaseConnection.connectDB("book","root","");
}
public void writeRegister(Register register){
int ok = 0;
try {
sql = "insert into register value (?,?)"; //預(yù)處理
presql = connection.prepareStatement(sql);
presql.setString(1,register.getId());
presql.setString(2,register.getPassword());
ok = presql.executeUpdate(); //成功插入式返回1
connection.close();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"ID不能重復(fù)","警告",JOptionPane.WARNING_MESSAGE);
}
if (ok!=0){
JOptionPane.showMessageDialog(null,"注冊(cè)成功","恭喜",JOptionPane.WARNING_MESSAGE);
}
}
}
(2)登錄模型處理工具類
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 = ? "; //主要用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, "登錄成功", "恭喜", JOptionPane.WARNING_MESSAGE);
} else {
login.setLoginSuccess(false);
JOptionPane.showMessageDialog(null, "登錄失敗", "提示", JOptionPane.WARNING_MESSAGE);
}
connection.close();
loginSuccess = login.isLoginSuccess();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4、可以設(shè)計(jì)一個(gè)測(cè)試類,用來(lái)校驗(yàn)程序能否正常通過(guò)。能通過(guò),就可以開(kāi)始設(shè)計(jì)視圖了。
(1)注冊(cè)視圖的設(shè)計(jì)
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)登錄視圖的設(shè)計(jì)
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、把以上兩個(gè)視圖類結(jié)合在一個(gè)類當(dāng)中,方便之后的維護(hù)和修改。
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()); 不設(shè)置時(shí)不全框顯示
jTabbedPane = new JTabbedPane();
registerPanel = new registerPanel();
loginPanel = new loginPanel();
jTabbedPane.addTab("注冊(cè)界面", registerPanel);
jTabbedPane.addTab("登錄界面", loginPanel);
add(jTabbedPane, BorderLayout.CENTER);
jTabbedPane.validate();
}
public boolean isLoginSuccess() {
return loginPanel.isLoignSuccess();
}
}
6、一切都設(shè)計(jì)完畢了,就可以運(yùn)行了。設(shè)置一個(gè)程序的主入口。
package Chapter_14.Part_8;
public class Example14_9 {
public static void main(String[] args) {
myWindow myWindow = new myWindow();
}
}
關(guān)于“怎么用Java和MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)注冊(cè)和登錄”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“怎么用Java和MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)注冊(cè)和登錄”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。