ใช้ ResultSetMetaData ใน Java ดึงข้อมูลเมตาของ ResultSet
ResultSetMetaData ให้ข้อมูลเกี่ยวกับคอลัมน์ของ ResultSet: จำนวนคอลัมน์, ชื่อ, ชนิดข้อมูล, ความแม่นยำ, เป็นต้น
สร้างตารางตัวอย่าง
mysql> CREATE TABLE DemoTable (
-> StudentId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> StudentFirstName VARCHAR(20),
-> StudentLastName VARCHAR(20)
-> );
Query OK, 0 rows affected (0.58 sec)
โค้ด Java 完整 (JDBC)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mysql.jdbc.ResultSetMetaData; -- MySQL Specific (หรือ java.sql.ResultSetMetaData)
public class ResultSetDemo {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
Statement st = null;
ResultSet rs = null;
try {
// 1. เชื่อมต่อ Database
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/web?useSSL=false",
"root", "123456");
// 2. สร้าง Statement และ Execute Query
String query = "SELECT StudentId, StudentFirstName, StudentLastName FROM DemoTable";
st = con.createStatement();
rs = st.executeQuery(query);
// 3. ดึง MetaData
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
// 4. จำนวนคอลัมน์
int numberOfColumns = rsmd.getColumnCount();
System.out.println("Number Of Columns: " + numberOfColumns);
// 5. รายละเอียดแต่ละคอลัมน์
System.out.println("All Details Of Columns:");
for (int i = 1; i <= numberOfColumns; i++) {
String columnName = rsmd.getColumnName(i);
String dataTypeName = rsmd.getColumnTypeName(i);
int columnType = rsmd.getColumnType(i); // java.sql.Types constant
int precision = rsmd.getPrecision(i); -- ความแม่นยำ (Precision)
int scale = rsmd.getScale(i); -- Scale (ทศนิยม)
boolean isNullable = rsmd.isNullable(i) == ResultSetMetaData.columnNullable;
boolean isAutoIncrement = rsmd.isAutoIncrement(i);
System.out.println(columnName + " | Type: " + dataTypeName
+ " | Precision: " + precision + " | Scale: " + scale
+ " | Nullable: " + isNullable + " | AutoInc: " + isAutoIncrement);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 6. ปิด Resource (ลำดับย้อนกลับ)
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (st != null) st.close(); } catch (Exception e) {}
try { if (con != null) con.close(); } catch (Exception e) {}
}
}
}
ผลลัพธ์ตัวอย่าง
Number Of Columns: 3 All Details Of Columns: StudentId | Type: INT | Precision: 10 | Scale: 0 | Nullable: false | AutoInc: true StudentFirstName | Type: VARCHAR | Precision: 20 | Scale: 0 | Nullable: true | AutoInc: false StudentLastName | Type: VARCHAR | Precision: 20 | Scale: 0 | Nullable: true | AutoInc: false
Method สำคัญของ ResultSetMetaData
| Method | คืนค่า |
|---|---|
getColumnCount() | int - จำนวนคอลัมน์ |
getColumnName(int) | String - ชื่อคอลัมน์ |
getColumnTypeName(int) | String - ชนิดข้อมูลเฉพาะ DB (VARCHAR, INT) |
getColumnType(int) | int - java.sql.Types constant (VARCHAR=12, INTEGER=4) |
getPrecision(int) | int - Precision (ความยาวสูงสุด) |
getScale(int) | int - Scale (ทศนิยม) |
isNullable(int) | int - columnNoNulls (0), columnNullable (1), columnNullableUnknown (2) |
isAutoIncrement(int) | boolean |
getTableName(int) | String - ชื่อตาราง |
getSchemaName(int) | String - ชื่อ Schema/Database |