一.JDBC 实例
//1. 加载一个Driver驱动
Class.forName("com.mysql.cj.jdbc.Driver"); // 8.0的驱动类
//Class.forName("com.mysql.jdbc.Driver"); // 5.X 驱动类
//2. 创建数据库连接(Connection)
String url = "jdbc:mysql://localhost:3306/i-news?serverTimezone=Asia/Shanghai";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url,user,password); // 多态
//3. 创建SQL命令发送器Statement
Statement st = conn.createStatement();
//4. 通过Statement发送SQL命令并得到结果
String sql = "select * from admin";
ResultSet rs = st.executeQuery(sql);
//5. 处理SQL结果(select语句)
while (rs.next()) {
System.out.print(rs.getString("id") + "\t");
System.out.println(rs.getString("username"));
}
//6. 关闭数据库资源
rs.close();
st.close();
conn.close();