package myTest;


import com.sf.entity.Student;

import java.sql.*;

/**
 * @author WanJl
 * @version 1.0
 * @title JdbcTest
 * @description
 * @create 2025/10/12
 */
public class JdbcTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String url="jdbc:mysql://localhost:3306/db01";
        String username="root";
        String password="123456";
        //1、加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2、获取数据库连接
        Connection conn = DriverManager.getConnection(url, username, password);
        //3、定义SQL
        String sql="select * from student";
        //4、获取Statement对象
        Statement statement = conn.createStatement();
        //5、执行SQL语句，返回执行结果
        ResultSet resultSet = statement.executeQuery(sql);
        //6、处理结果
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            String sex=resultSet.getString("sex");
            String address = resultSet.getString("address");
            double math = resultSet.getDouble("math");
            double english = resultSet.getDouble("english");
            Date hireDate = resultSet.getDate("hire_date");

            //手动的封装成Java实体类对象
            Student student=new Student(id,name,age,sex,address,math,english,hireDate);

            System.out.println(student);
        }

        //7、释放资源
        statement.close();
        conn.close();
    }
}
