public static void main(String[] args) throws Exception {
//1、加载驱动 固定写法
//MySQL8中com.mysql.cj.jdbc.Driver
Class.forName("com.mysql.jdbc.Driver");
//2、用户信息和URL
//useUnicode=true支持中文编码&characterEncoding=utf8设置字符集&useSSL=true使用安全的连接
String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String password = "123456";
//3、连接成功,数据库对象Connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//4、执行sql的对象
Statement statement = connection.createStatement();
//5、执行sql的对象 去 执行sql语句 可能存在结果,查询返回结果
String sql = "select * from student";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
System.out.println("studentname="+resultSet.getString("studentname"));
}
//6、释放连接
resultSet.close();
statement.close();
connection.close();
}
jdbc原生连接数据库
最新推荐文章于 2022-11-10 10:42:46 发布