实验目的
- 掌握Java+MySQL数据库的连接及访问方式。
- 掌握Java+MySQL数据库的查询、添加、删除和修改等操作。
实验任务
- 创建一个数据库userdb,含user表,该表用于存放用户注册的信息:
user ( username nchar(20) not null primary key, psd char(32))。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a478980390a45e43d1883615575c7d17.png)
- 使用JDBC完成用户注册功能,界面如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/aa113097ba159d3740daad99a3f49d9f.png)
其中:
“*”表示必填,密码和确认密码要一致。
“注册”按钮:注册前要检测用户名是否存在,如果不存在,才将用户名和密码(要求使用32位md5加密)存入user表。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;
import java.io.*;
public class Window extends JFrame {
private JFrame jFrame = new JFrame("用户注册");
private Container c = jFrame.getContentPane();
private JLabel a1 = new JLabel("用户名:");
private JLabel b1 = new JLabel("*");
private JTextField name = new JTextField();
private JLabel a2 = new JLabel("密码:");
private JLabel b2 = new JLabel("*");
private JLabel a3 = new JLabel("请再次输入密码:");
private JLabel b3 = new JLabel("*");
private JButton submit = new JButton("注册");
private JButton afresh = new JButton("重输");
private JPasswordField passport;
private JPasswordField passport2;
public Window() {
jFrame.setBounds(200, 200, 380, 200);
c.setLayout(new BorderLayout());
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
jFrame.setVisible(true);
}
public void init() {
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
a1.setBounds(80, 20, 55, 20);
a2.setBounds(92, 50, 50, 20);
a3.setBounds(28, 80, 120, 20);
b1.setBounds(140, 22, 20, 20);
b1.setForeground(Color.red);
b2.setBounds(140, 52, 20, 20);
b2.setForeground(Color.red);
b3.setBounds(140, 82, 20, 20);
b3.setForeground(Color.red);
fieldPanel.add(a1);
fieldPanel.add(a2);
fieldPanel.add(a3);
fieldPanel.add(b1);
fieldPanel.add(b2);
fieldPanel.add(b3);
name.setBounds(150, 20, 160, 20);
fieldPanel.add(name);
c.add(fieldPanel, "Center");
passport = new JPasswordField();
passport.setBounds(150, 50, 160, 21);
fieldPanel.add(passport);
passport2 = new JPasswordField();
passport2.setBounds(150, 80, 160, 21);
fieldPanel.add(passport2);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(submit);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(name.getText().toString().equals("")||passport.getText().toString().equals("")||passport2.getText().toString().equals("")) {
JOptionPane.showMessageDialog(null, "请保证必填处有被填充");
}
else {
if (passport.getText().toString().equals(passport2.getText().toString())) {
User newUser = new User();
newUser.setUsername(name.getText());
newUser.setPassword(new MD5_Demo().MD5(passport.getText()));
CheckRegister newCheckRegister = new CheckRegister();
newCheckRegister.RegisterUser(newUser);
} else {
JOptionPane.showMessageDialog(null, "两次输入密码不同");
}
}
}
});
buttonPanel.add(afresh);
afresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
name.setText("");
passport.setText("");
passport2.setText("");
}
});
c.add(buttonPanel, "South");
}
public static void main(String[] args) {
Window stu = new Window();
}
}
public class User {
private String username = null;
private String userpassword = null;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return userpassword;
}
public void setPassword(String userpassword) {
this.userpassword = userpassword;
}
}
import java.sql.*;
import javax.swing.JOptionPane;
public class CheckRegister {
public static void RegisterUser(User u) {
try {
String url = "jdbc:mysql://localhost:3306/userdb?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, "root", "201813136045");
String sql = "select * from user where username=?";
PreparedStatement pStmt = conn.prepareStatement(sql);
pStmt.setString(1, u.getUsername());
ResultSet rs = pStmt.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(null, "注册名存在");
} else {
sql = "insert into user values(?,?)";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1, u.getUsername());
pStmt.setString(2, u.getPassword());
pStmt.executeUpdate();
JOptionPane.showMessageDialog(null, "注册成功");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "注册失败");
}
}
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5_Demo {
public final static String MD5(String s){
char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
try {
byte[] strTemp=s.getBytes();
MessageDigest mdTemp=MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md=mdTemp.digest();
int j=md.length;
char str[]=new char[j*2];
int k=0;
for(int i=0;i<j;i++){
byte byte0=md[i];
str[k++]=hexDigits[byte0>>>4 & 0xf];
str[k++]=hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
}