在Windows上配置MySql
我们先进入MySql官网
在官网中选择MySQL Installer for Windows
进入后选择第一个下载
接下来安装即可,在安装时,可以只安装MySql Server(默认选项),选择Full也可,这样会同时安装workbench以及其他组件。
安装完成后会弹出终端
输入 connect root@localhost(如果默认设置的话)连接本地Sql服务器
输入密码后,
命令提示符左边出现灰色,即成功。
一些命令的使用这里不再提及,读者可以自行查找。
导入JDBC包
同样的,在mysql官网下载驱动
这里以Java为例
进入后,Windows用户会发现并没有Windows选项,我们这里选择
下载第二个即可。
当然,配置环境变量也是需要的,我们在c盘的Program Files中找到MySql,选择MySQL Server 8.0->bin,然后复制路径,右键此电脑->选择新建环境变量,设置名字后将路径复制到值中,然后同样在下面的Path中添加,确定即可。
导入JDBC的Jar包
我们创建一个Java项目后(ctrl+shift+p后输入create Java project)直接将下载好的JDBC中的jar后缀放入lib中
导入我们写的代码后,按f5运行。
接下来展示一个简单的项目:
设计用户注册和登录界面,实现用户注册和登录操作。
1、设计用户注册/登录界面;
2、使用工具在MySQL中创建user表,包括学号、姓名、密码、专业、班级;
3、实现注册操作:在user表中插入一条新纪录,但学号不能重复;
4、实现登录操作:使用输入的学号和密码在user表中查找,查找成功,弹出消息框显示“登录成功”,不成功,则回到输入界面;使用对象文件记住在本机登录的用户名,显示在用户名组合框中。
sql代码
USE student;
CREATE TABLE IF NOT EXISTS user (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id VARCHAR(10) UNIQUE,
name VARCHAR(50),
password VARCHAR(50),
major VARCHAR(50),
class_name VARCHAR(50)
);
Java代码
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class reglog extends JFrame {
private JTextField studentIdField;
private JTextField nameField;
private JPasswordField passwordField;
private JTextField majorField;
private JTextField classField;
public reglog() {
setTitle("注册与登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(null);
JLabel studentIdLabel = new JLabel("学号:");
studentIdLabel.setBounds(20, 20, 80, 25);
add(studentIdLabel);
studentIdField = new JTextField();
studentIdField.setBounds(120, 20, 200, 25);
add(studentIdField);
JLabel nameLabel = new JLabel("姓名:");
nameLabel.setBounds(20, 50, 80, 25);
add(nameLabel);
nameField = new JTextField();
nameField.setBounds(120, 50, 200, 25);
add(nameField);
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setBounds(20, 80, 80, 25);
add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setBounds(120, 80, 200, 25);
add(passwordField);
JLabel majorLabel = new JLabel("专业:");
majorLabel.setBounds(20, 110, 80, 25);
add(majorLabel);
majorField = new JTextField();
majorField.setBounds(120, 110, 200, 25);
add(majorField);
JLabel classLabel = new JLabel("班级:");
classLabel.setBounds(20, 140, 80, 25);
add(classLabel);
classField = new JTextField();
classField.setBounds(120, 140, 200, 25);
add(classField);
JButton registerButton = new JButton("注册");
registerButton.setBounds(20, 180, 100, 25);
add(registerButton);
JButton loginButton = new JButton("登录");
loginButton.setBounds(140, 180, 100, 25);
add(loginButton);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
registerUser();
}
});
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginUser();
}
});
}
private void registerUser() {
String studentId = studentIdField.getText();
String name = nameField.getText();
String password = new String(passwordField.getPassword());
String major = majorField.getText();
String clazz = classField.getText();
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "*****"))//不方便展示 {
String checkStudentIdSQL = "SELECT * FROM user WHERE student_id = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(checkStudentIdSQL)) {
preparedStatement.setString(1, studentId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
JOptionPane.showMessageDialog(this, "学号已存在,注册失败");
} else {
String insertUserSQL = "INSERT INTO user (student_id, name, password, major, class_name) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement insertStatement = connection.prepareStatement(insertUserSQL)) {
insertStatement.setString(1, studentId);
insertStatement.setString(2, name);
insertStatement.setString(3, password);
insertStatement.setString(4, major);
insertStatement.setString(5, clazz);
int rowsAffected = insertStatement.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "注册成功");
} else {
JOptionPane.showMessageDialog(this, "注册失败");
}
}
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void loginUser() {
String studentId = studentIdField.getText();
String password = new String(passwordField.getPassword());
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "*****")) {
String loginUserSQL = "SELECT * FROM user WHERE student_id = ? AND password = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(loginUserSQL)) {
preparedStatement.setString(1, studentId);
preparedStatement.setString(2, password);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
JOptionPane.showMessageDialog(this, "登录成功");
System.out.println("登入: " + resultSet.getString("name"));
} else {
JOptionPane.showMessageDialog(this, "学号或密码错误,登录失败");
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new reglog().setVisible(true);
}
});
}
}