Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

ResultSet ใน JDBC มีกี่ประเภท? คืออะไรบ้าง?


มี 2 ประเภท คือ Forward only และ Bidirectional

Forward only ResultSet: cursor เคลื่อนที่ได้เพียงทิศทางเดียว เป็นค่าเริ่มต้นของ JDBC ใช้ next() วนลูป

while(rs.next()) { }

ตัวอย่าง Forward Only

ตาราง dataset:

+--------------+-----------+
| mobile_brand | unit_sale |
+--------------+-----------+
| Iphone       | 3000      |
| Samsung      | 4000      |
| Nokia        | 5000      |
| Vivo         | 1500      |
+--------------+-----------+

โปรแกรมดึงข้อมูล:

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

public class RetrievingData {
    public static void main(String args[]) throws Exception {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/TestDB";
        Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
        System.out.println("Connection established......");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from Dataset");
        System.out.println("Contents of the table");
        while(rs.next()) {
            System.out.print("Brand: " + rs.getString("Mobile_Brand") + ", ");
            System.out.print("Sale: " + rs.getString("Unit_Sale"));
            System.out.println();
        }
    }
}
Connection established......
Contents of the table
Brand: Iphone, Sale: 3000
Brand: Samsung, Sale: 4000
Brand: Nokia, Sale: 5000
Brand: Vivo, Sale: 1500

Bidirectional ResultSet: cursor เคลื่อนที่ได้ทั้งหน้า-หลัง สร้างด้วย createStatement() ที่รับ resultSetType และ concurrency:

Statement createStatement(int resultSetType, int resultSetConcurrency)

สร้าง bidirectional result set:

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

ตัวอย่าง Bidirectional

ใช้ previous() พิมพ์จากหลังมาหน้า:

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

public class BidirectionalResultSet {
    public static void main(String args[]) throws Exception {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/TestDB";
        Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
        System.out.println("Connection established......");
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = stmt.executeQuery("select * from Dataset");
        rs.afterLast();
        System.out.println("Contents of the table");
        while(rs.previous()) {
            System.out.print("Brand: " + rs.getString("Mobile_Brand") + ", ");
            System.out.print("Sale: " + rs.getString("Unit_Sale"));
            System.out.println();
        }
    }
}
Connection established......
Contents of the table
Brand: Vivo, Sale: 1500
Brand: Nokia, Sale: 5000
Brand: Samsung, Sale: 4000
Brand: IPhone, Sale: 3000