Java - ResultSet

MySQL

insert into actor
    values (null, '小红', '女', '2001-11-11', '1120');

Java - ResultSet_第1张图片

 

SELECT * FROM j_db01.actor;

Java - ResultSet_第2张图片

 

Run

Java - ResultSet_第3张图片

mysql> use j_db01;
        Database changed
mysql> select * from actor;
        +----+------+-----+---------------------+-------+
        | id | name | sex | borndate | phone |
        +----+------+-----+---------------------+-------+
        | 3 | 李华 | 男 | 2000-11-11 00:00:00 | 1100 |
        | 4 | 小红 | 女 | 2001-11-11 00:00:00 | 1120 |
        +----+------+-----+---------------------+-------+
        2 rows in set (0.00 sec)

Debug

Java - ResultSet_第4张图片 

F8

Java - ResultSet_第5张图片

 Find the rowData

Java - ResultSet_第6张图片

 

Code

package com.jdbc.resultSet_;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.*;
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"));
        properties.load(Files.newInputStream(Paths.get("src\\mysql.properties")));

        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //1.注册驱动
//        Class.forName("com.mysql.cj.jdbc.Driver");
        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()) {
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String sex = resultSet.getString(3);
            Date date = resultSet.getDate(4);
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + date);
        }

        //6.关闭连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}

你可能感兴趣的:(java,开发语言)