package com.cqust;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//处理查询结果集
public class JDBCTest04 {
public static void main(String[] args) throws Exception {
String url = "jdbc:mysql://localhost:3306/cqust_db";
String user = "root";
String password = "****";
//1.注册驱动
//这里会调用静态代码快执行驱动注册
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
Connection connection = DriverManager.getConnection(url,user,password);
//3.获取数据库操作对象
Statement statement = connection.createStatement();
//4.执行sql
String sql = "select id,user2_name from t_user2 where id = 1";
ResultSet resultSet = statement.executeQuery(sql);
//5.处理查询结果集(针对select语句,如果不是则不需要这一步)
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("user2_name");
System.out.println(id+"| "+name);
}
//6.关闭资源
if (resultSet !=null){
resultSet.close();
}
if (statement !=null){
statement.close();
}
if (connection !=null){
connection.close();
}
}
}