欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JDBC的ResultSet使用說(shuō)明

 更新時(shí)間:2019年02月21日 11:48:03   作者:鴨鴨老板  
今天小編就為大家分享一篇JDBC的ResultSet使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

一、ResultSet[結(jié)果集]

1.表示數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表,通常通過(guò)執(zhí)行查詢(xún)數(shù)據(jù)庫(kù)的語(yǔ)句生成。

2.ResultSet對(duì)象保持一個(gè)光標(biāo)指向其當(dāng)前的數(shù)據(jù)行,最開(kāi)始光標(biāo)在第一行。

3.next方法將光標(biāo)移動(dòng)到下一行,由于在ResultSet對(duì)象中沒(méi)有更多行時(shí)返回false,可以在while循環(huán)中使用循環(huán)來(lái)遍歷結(jié)果集。

package com.jun.jdbc.resultset;
 
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
 
/**
 * select語(yǔ)句返回ResultSet,取出結(jié)果
 */
public class ResultSet01 {
    public static void main(String[] args) throws Exception {
        //通過(guò)Properties對(duì)象獲取配置文件信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //獲取到相關(guān)值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        //得到Statement
        Statement statement = connection.createStatement();
        //sql語(yǔ)句
        String sql ="select id,name,sex,borndate from actor";
        //執(zhí)行sql語(yǔ)句,該語(yǔ)句返回單個(gè),ResultSet對(duì)象
        /*
        +----+------+-----+---------------------+
        | id | name | sex | borndate            |
       +----+------+-----+---------------------+
       |  1 | tom  | 男  | 1945-05-06 00:00:00 |
       |  2 | jack | 男  | 1986-06-07 00:00:00 |
       +----+------+-----+---------------------+
         */
        ResultSet resultSet = statement.executeQuery(sql);
        //使用while取出數(shù)據(jù)
        while (resultSet.next()){//讓光標(biāo)向后移動(dòng),沒(méi)有更多就返回false
            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);
        }
        //關(guān)閉連接
        resultSet.close();
        statement.close();
        connection.close();
    }
}

二、ResultSet分析

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論