程序
近日学了MySQL的连接以及GUI的使用,制作了一个简易的注册登录程序,将注册的相关信息存储在对应的MySQL表中,登录时使用通配符进行查询。
此功能将在寒假具体应用到网页注册方面,尽情期待!
代码
Register.java
package exam1;
public class Register {
String id;
String password;
String birth;
public void setID(String id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getID() {
return id;
}
public String getPassword() {
return password;
}
public String getBirth() {
return birth;
}
}
Login.java
package exam1;
public class Login {
boolean loginSuccess = false;
String id;
String password;
public void setID(String id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public String getID() {
return id;
}
public String getPassword() {
return password;
}
public void setLoginSuccess(boolean bo) {
loginSuccess = bo;
}
public boolean getLoginSuccess() {
return loginSuccess;
}
}
HandleInsertDat.java
package exam1;
import java.sql.*;
import javax.swing.JOptionPane;
public class HandleInsertData {
Connection con;
PreparedStatement presql;
public HandleInsertData() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {}
String uri = "jdbc:mysql://localhost:3306/user?serverTimezone=GMT&useSSL=true";
try {
con = DriverManager.getConnection(uri,"root","123456");
}catch(SQLException e) {}
}
public void writeRegisterModel(Register register) {
String sqlStr = "insert into register values(?,?,?)";
int ok = 0;
try {
presql = con.prepareStatement(sqlStr);
presql.setString(1,register.getID());
presql.setString(2,register.getPassword());
presql.setString(3,register.getBirth());
ok = presql.executeUpdate();
con.close();
}catch(SQLException e) {
JOptionPane.showMessageDialog(null,"id不能重复","Warning",JOptionPane.WARNING_MESSAGE);
}
if(ok!=0) {
JOptionPane.showMessageDialog(null, "注册成功","Congradulation",JOptionPane.WARNING_MESSAGE);
}
}
}
HandleLogin.java
package exam1;
import java.sql.*;
import javax.swing.JOptionPane;
public class HandleLogin {
Connection con;
PreparedStatement presql;
ResultSet rs;
public HandleLogin() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {}
String uri = "jdbc:mysql://localhost:3306/user?serverTimezone=GMT&useSSL=true";
try {
con = DriverManager.getConnection(uri,"root","123456");
}catch(SQLException e) {}
}
public Login queryVerify(Login loginModel) {
String id = loginModel.getID();
String pw = loginModel.getPassword();
String sqlstr = "select id,password from register where "+"id = ? and password = ?";
try {
presql = con.prepareStatement(sqlstr);
presql.setString(1,id);
presql.setString(2,pw);
rs = presql.executeQuery();
if(rs.next() == true) {
loginModel.setLoginSuccess(true);
JOptionPane.showMessageDialog(null,"登录成功","Congradulation",JOptionPane.WARNING_MESSAGE);
}
else {
loginModel.setLoginSuccess(false);
JOptionPane.showMessageDialog(null,"登录失败","登录失败,请重试",JOptionPane.WARNING_MESSAGE);
}
con.close();
}catch(SQLException e) {}
return loginModel;
}
}
Register.java
package exam1;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.*;
import javax.swing.JPasswordField;
public class RegisterView implements ActionListener{
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JPasswordField textField_4;
private JButton btnNewButton;
private JButton btnNewButton_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RegisterView window = new RegisterView();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public RegisterView() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize(){
frame = new JFrame();
frame.setTitle("\u534E\u5BB9\u9053");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
btnNewButton = new JButton(" \u767B\u5F55\u2192");
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 18));
btnNewButton.setBounds(135, 200, 153, 53);
frame.getContentPane().add(btnNewButton);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(10, 33, 416, 139);
frame.getContentPane().add(tabbedPane);
JPanel panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel.setToolTipText("");
tabbedPane.addTab("我要注册", null, panel, null);
panel.setLayout(null);
textField = new JTextField();
textField.setBounds(115, 10, 216, 21);
panel.add(textField);
textField.setColumns(10);
JLabel lblNewLabel = new JLabel("ID\uFF1A");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel.setBounds(63, 12, 58, 15);
panel.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_1.setBounds(52, 40, 58, 21);
panel.add(lblNewLabel_1);
textField_1 = new JTextField();
textField_1.setBounds(115, 41, 216, 21);
panel.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(115, 72, 216, 21);
panel.add(textField_2);
textField_2.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("\u751F\u65E5\uFF1A");
lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_2.setBounds(52, 72, 58, 18);
panel.add(lblNewLabel_2);
btnNewButton_1 = new JButton("OK");
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 14));
btnNewButton_1.setBounds(341, 71, 60, 23);
panel.add(btnNewButton_1);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
tabbedPane.addTab("我要登陆", null, panel_1, null);
panel_1.setLayout(null);
JLabel lblNewLabel_3 = new JLabel("ID:");
lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_3.setBounds(80, 22, 58, 15);
panel_1.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("\u5BC6\u7801:");
lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel_4.setBounds(68, 61, 58, 18);
panel_1.add(lblNewLabel_4);
textField_3 = new JTextField();
textField_3.setBounds(127, 20, 190, 21);
panel_1.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JPasswordField();
textField_4.setBounds(127, 61, 190, 21);
panel_1.add(textField_4);
textField_4.setColumns(10);
btnNewButton.addActionListener(this);
btnNewButton_1.addActionListener(this);
textField.addActionListener(this);
textField_1.addActionListener(this);
textField_2.addActionListener(this);
textField_3.addActionListener(this);
textField_4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnNewButton) { //登录按钮
Login login = new Login();
if(textField_3.getText() != null) { //判断是否成功登录
login.setID(textField_3.getText());
}
else {
JOptionPane.showMessageDialog(null, "ID不能为空! ","Warning",JOptionPane.WARNING_MESSAGE);
}
if(textField_4.getPassword() != null) {
login.setPassword(String.valueOf(textField_4.getPassword()));
}else {
JOptionPane.showMessageDialog(null, "密码不能为空! ","Warning",JOptionPane.WARNING_MESSAGE);
}
HandleLogin handleLogin = new HandleLogin();
login = handleLogin.queryVerify(login);
if(login.getLoginSuccess()==true) {
frame.dispose();
new game(); //其他的功能函数
}
}
else if(e.getSource() == btnNewButton_1) {
Register user = new Register();
user.setID(textField.getText());
user.setPassword(textField_1.getText());
user.setBirth(textField_2.getText());
HandleInsertData handleRegister = new HandleInsertData();
handleRegister.writeRegisterModel(user);
}
}
}
测试
注册界面:
登陆界面:
数据库展示:
因为技术有限,只是简单的实现其相应功能(__) 嘻嘻。