package com.zhan.lesson01;
import java.sql.*;
public class JdbcDemo01 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.用户信息和URL
String url = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String password = "1234";
//3.连接成功,数据库对象 Connection 代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//4.执行SQL的对象 Statement 执行sql的对象
Statement statement = connection.createStatement();
//5.执行SQL的对象去执行SQL ,可能存在的结果
String sql = "SELECT * FROM jdbcstudy.users";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println("id="+ resultSet.getObject("id"));
System.out.println("name="+ resultSet.getObject("NAME"));
System.out.println("pwd="+ resultSet.getObject("PASSWORD"));
System.out.println("birth="+ resultSet.getObject("birthday"));
}
//释放连接
resultSet.close();
statement.close();
connection.close();
}
}
JDBC连接数据库
最新推荐文章于 2023-05-28 10:59:20 发布