【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) {}
}

運行截圖

登錄前:

在這裏插入圖片描述登錄過程:
在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

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