import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.Arrays;
import java.util.Properties;
/**
* 演示select语句返回ResultSet,并取出结果
*/
@SuppressWarnings({"all"})
public class ResultSet_ {
public static void main(String[] args) throws Exception {
//通过Properties对象获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src/mysql.properties"));
//读取相关的值
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
//1.注册驱动
Class.forName(driver);
//2.得到连接
Connection connection = DriverManager.getConnection(url, user, password);
//3.得到Statement
Statement statement = connection.createStatement();
//4.组织sql
String sql = "select id, name, sex, borndate from actor";
//执行给定的sql语句,该语句返回单个ResultSet对象
ResultSet resultSet = statement.executeQuery(sql);
//5.使用while取出数据
while (resultSet.next()) { //让光标向后移动,如果没有更多行,则返回false
int id = resultSet.getInt(1); //获取该行的第1列
String name = resultSet.getString(2); //第2行
String sex = resultSet.getString(3);
Date borndate = resultSet.getDate(4);
System.out.println(id + "\t" + name + "\t" + sex + "\t" + borndate + "\t");
}
//6.关闭连接
resultSet.close();
statement.close();
connection.close();
}
}