package com.imau.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.imau.jdbc.DBHelper;
public class Preview {
public static void main(String[] args) throws Exception{
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
//static final String JDBC_DRIVER = “com.mysql.cj.jdbc.Driver”;
//static final String DB_URL = “jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC”;
String className=“com.mysql.cj.jdbc.Driver”;
Class.forName(className);
String url = “jdbc:mysql://localhost:3306/cms?useSSL=false&serverTimezone=UTC”;
String username = “root”;
String password = “666888”;
System.out.println(“连接数据库中…”);
Connection conn = DBHelper.getConnection();
System.out.println(“初始化中…”);
Statement stmt =conn.createStatement();
String sql = “select * from students”;
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
int id =rs.getInt(“id”);
String name = rs.getString(“name”);
int age = rs.getInt(“age”);
System.out.print(“学号:” + id + “”);
System.out.print(“姓名:” + name + " ");
System.out.println(“年龄:” + age);
}
rs.close();
stmt.close();
conn.close();
}
}