1、数据库设计——在了解用户的需求之后,根据用户的这些需求进行数据库的设计。
2、数据模型——就是把数据库表的结构全都封装在一个类当中。
3、数据处理工具类——根据数据模型给出的需求设计一个工具类来处理数据。
4、视图展示——最后提供给用户一个交互方便的视图界面。
1、首先要在数据库中建好表。
2、根据业务需求,制定相应的数据模型。本次设计主要是登录和注册两个内容,因此设计了两个数据模型。
(1)注册模型
package Chapter_14.Part_8;
public class Register { //注册模型
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、根据以上设计好的两个数据模型,分别设计相应的数据处理的工具类。
(1)注册模型处理工具类
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 { //注册数据处理者
Connection connection = null;
PreparedStatement presql; //预处理对象
String sql;
public registerHandle() {
connection = GetDatabaseConnection.connectDB("book","root","");
}
public void writeRegister(Register register){
int ok = 0;
try {
sql = "insert into register value (?,?)"; //预处理
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不能重复","警告",JOptionPane.WARNING_MESSAGE);
}
if (ok!=0){
JOptionPane.showMessageDialog(null,"注册成功","恭喜",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、可以设计一个测试类,用来校验程序能否正常通过。能通过,就可以开始设计视图了。
(1)注册视图的设计
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)登录视图的设计
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、把以上两个视图类结合在一个类当中,方便之后的维护和修改。
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()); 不设置时不全框显示
jTabbedPane = new JTabbedPane();
registerPanel = new registerPanel();
loginPanel = new loginPanel();
jTabbedPane.addTab("注册界面", registerPanel);
jTabbedPane.addTab("登录界面", loginPanel);
add(jTabbedPane, BorderLayout.CENTER);
jTabbedPane.validate();
}
public boolean isLoginSuccess() {
return loginPanel.isLoignSuccess();
}
}
6、一切都设计完毕了,就可以运行了。设置一个程序的主入口。
package Chapter_14.Part_8;
public class Example14_9 {
public static void main(String[] args) {
myWindow myWindow = new myWindow();
}
}
以上是怎么用Java和MySQL数据库实现注册和登录的详细内容。更多信息请关注PHP中文网其他相关文章!