【Java】通过 JDBC 连接 MySQL 数据库并使用预处理语句对数据库内容查询和更新

作业要求

(1)先用MySQL建立一个数据库Users,然后在数据库里建立一个表。表中每条记录,存储了用户名、密码、上次登录时间。请你在表中输入3条记录。请截图。
(2)编写一个带窗体的程序,用户输入用户名和密码,程序与数据库中的信息比对,用消息对话框输出结果:若正确,输出上次登录时间;若错误,提示用户重新输入。

步骤与源码

1、先在MySQL中按作业要求(1)创建实验数据。

create database Users;
use Users;
create table LoginTable (Login varchar(32) primary key, Password varchar(32), LastLogin datetime);
insert into LoginTable values("Hank Pym", "fheh0r23r3uwdj0i", "2020-06-04 10:46:32");
insert into LoginTable values("Scott Lang", "ur983rjwfjd230du20riu2", "2020-06-04 10:48:58");
insert into LoginTable values("Hope Pym", "r8432my88ryn03r34t9r2d", "2020-06-04 10:50:27");

2、在MySQL中修改登录密码。

在这里插入图片描述
3、执行如下代码,将数据库的三个用户各成功登录一次。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
	public static void main(String[] args) throws SQLException { G.ConnectToMySQL(); G.CreateUI(); }
}

class G {
	static JTextField textLogin; static JPasswordField textPassword; static Connection c;
	static void CreateUI() throws SQLException {
		JFrame frame = new JFrame("Login");
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setBounds(screen.width / 2 - 160, screen.height / 2 - 90, 320, 180); frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.addWindowListener(new WndClosingDetecter());
		JPanel panel = new JPanel(new FlowLayout());
		frame.setContentPane(panel);
		JLabel InputHints = new JLabel("Enter your login and password:");
		textLogin = new JTextField(27); textPassword = new JPasswordField(27);
		JButton buttonLogin = new JButton("Login");
		buttonLogin.addActionListener(new LoginQuestDetecter());
		panel.add(InputHints); panel.add(textLogin); panel.add(textPassword); panel.add(buttonLogin);
		frame.setVisible(true);
	}
	static void ConnectToMySQL() throws SQLException {
		String url = "jdbc:mysql://localhost:3306/Users?useSSL=true&serverTimezone=GMT%2B8", login = "Andy", password = "        ";
		c = DriverManager.getConnection(url, login, password);
	}
}

class LoginQuestDetecter implements ActionListener {
	PreparedStatement s, t; String Login, Password, LastLogin; ResultSet r; DateTimeFormatter f; LocalDateTime d;
	LoginQuestDetecter() throws SQLException {
		f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		s = G.c.prepareStatement("SELECT Password, LastLogin from LoginTable where Login = ?");
		t = G.c.prepareStatement("update LoginTable set LastLogin = ? where Login = ?");
	}
	public void actionPerformed(ActionEvent e) {
		try {
			Login = G.textLogin.getText(); s.setString(1, Login); r = s.executeQuery();
			if (!r.isBeforeFirst()) { JOptionPane.showMessageDialog(null, "该用户不存在。"); return; }
			r.next(); Password = r.getString(1);
			if (Password.equals(String.valueOf(G.textPassword.getPassword()))) {
				LastLogin = r.getString(2); d = LocalDateTime.now();
				JOptionPane.showMessageDialog(null, "登录成功。\n上次登录时间:" + LocalDateTime.parse(LastLogin, f) + "\n该记录将修改为本次登录时间:" + d.format(f) + "。");
				t.setString(1, d.format(f)); t.setString(2, Login); t.executeUpdate();
			}
			else { JOptionPane.showMessageDialog(null, "用户名或密码错误。", "登录失败", JOptionPane.WARNING_MESSAGE); }
		}
		catch (SQLException e1) { e1.printStackTrace(); }
	}
}

class WndClosingDetecter implements WindowListener {
	public void windowClosing(WindowEvent e) {
		try { G.c.close(); }
		catch (SQLException e1) { e1.printStackTrace(); }
	}
	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}
}

运行截图

登录前:

在这里插入图片描述登录过程:
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章