jdbc连接

package com.song.testjdbc;

import java.sql.*;

public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//导入驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接信息 用户信息url
String url = "jdbc:mysql://localhost:3306/java_test?useUnicode=true&characterEncoding=utf8&useSSL=true";
String name = "root";
String pwd = "123456";
//连接成功 返回数据库对象 connection代表数据
Connection connection = DriverManager.getConnection(url, name, pwd);
//执行sql statement执行sql对象
Statement statement = connection.createStatement();
String sql = "SELECT * FROM tb_user";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println("id" + resultSet.getObject("userid"));
System.out.println("id" + resultSet.getObject("username"));
System.out.println("id" + resultSet.getObject("userPwd"));
System.out.println("id" + resultSet.getObject("userAge"));
}
//释放连接
resultSet.close();
statement.close();
connection.close();
}
}
posted @ 2022-06-07 13:50  小松2739  阅读(18)  评论(0编辑  收藏  举报