package com.lc.test01;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test01 {

    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 where id = 1 ";

        //获取结果集
        ResultSet rs = statement.executeQuery(sql);

        //rs.next() 判断是否有数据
        while (rs.next()){
            //rs.getXXX(参数) 获取数据    参数 索引 或者是字段
            String name = rs.getString("name");
            int age = rs.getInt("age");

            System.out.println("name= " + name + "-age = " + age);
        }

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

    }
}
