package com.lc.test01;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Test05 {

    public static void main(String[] args) throws Exception {
        //注册驱动
        // Class.forName("com.mysql.jdbc.Driver");

        //获取连接
        String url = "jdbc:mysql:///db2";
        String user = "root";
        String pass = "123456";

        Connection connection = DriverManager.getConnection(url, user, pass);

        //sql执行对象
        Statement statement = connection.createStatement();

        //sql
        String sql = " select * from t_user ";

        //获取结果集
        ResultSet rs = statement.executeQuery(sql);
        /*
            rs
                next方法 判断是否有下一个数据
                getXXX(参数) 获取 数据库中的数据  参数可以是字段 也可以是 索引 表中字段的索引 索引从1开始
         */
        List<User> userList = new ArrayList<>();

        while (rs.next()){
            //封装
            User u = new User();
            u.setId(rs.getInt(1));
            u.setName(rs.getString("name"));
            u.setId(rs.getInt("age"));
            u.setAddress(rs.getString("address"));
            u.setMoney(rs.getInt("money"));

            userList.add(u);
        }

        //循环 lambda
        userList.forEach(System.out::println);

        //释放
        rs.close();
        statement.close();
        connection.close();

    }

}
