用java连接数据库(mysql)且建立一个swing与数据库的内容匹配
数据库的连接
数据库中的值和输入的值比较
import java.sql.*;
class A{
int t;
String h;
String p;
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/STUDENTS?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
static final String USER = "root";
static final String PASS = "mn123456";
public A() {
Connection conn = null;
Statement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开链接
// System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
// 执行查询
// System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT students_id, students_title, students_author FROM students";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while(rs.next()){
// 通过字段检索
t = rs.getInt("students_id");
h = rs.getString("students_title");
p = rs.getString("studentS_author");
// 输出数据
// System.out.print("ID: " + t);
// System.out.print(", 站点名称: " +h);
// System.out.print(", 站点 URL: " + p);
// System.out.print("\n");
}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forNam