如何使用 JDBC 從 java.sql.Type 程式碼中獲取資料型別名稱?
java.sql.Types 類以整數格式表示 SQL 資料型別。列舉 JDBCType 的 valueOf() 方法接受表示 java.sql.Type 的整數值,並返回與指定的值對應的 JDBC 型別。
示例
讓我們使用 CREATE 語句在 MySQL 資料庫中建立一個名為 MyPlayers 的表,如下所示 −
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
以下 JDBC 程式與 MySQL 資料庫建立連線,將 MyPlayers 表的內容檢索到一個 ResultSet 物件中,獲取其元資料,獲取表中所有列的資料型別為 java.sql.Types 值(整數),並使用 valueOf() 方法檢索相應型別的名稱。
示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.JDBCType;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLTypeName {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql:///mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Query to retrieve records
String query = "Select * from MyPlayers";
//Executing the query
ResultSet rs = stmt.executeQuery(query);
//Getting the ResultSetMetadata object
ResultSetMetaData mertadata = rs.getMetaData();
//Returns the specified column's java.sql.Type name
//Retrieving the data type of a column
int ID_Type = mertadata.getColumnType(1);
int FirstName_Type = mertadata.getColumnType(2);
int LastName_Type = mertadata.getColumnType(3);
int DateOfBirth_Type = mertadata.getColumnType(4);
int PlaceOfBirth_Type = mertadata.getColumnType(5);
int Country_Type = mertadata.getColumnType(6);
System.out.println("Data type of the column ID: "+JDBCType.valueOf(ID_Type));
System.out.println("Data type of the column First_Name: "+JDBCType.valueOf(FirstName_Type));
System.out.println("Data type of the column Last_Name: "+JDBCType.valueOf(LastName_Type));
System.out.println("Data type of the column Date_Of_Birth: "+JDBCType.valueOf(DateOfBirth_Type));
System.out.println("Data type of the column Place_Of_Birth: "+JDBCType.valueOf(PlaceOfBirth_Type));
System.out.println("Data type of the column Country: "+JDBCType.valueOf(Country_Type));
}
}輸出
Connection established...... Price values updated ...... Contents of the Sales table after the update: Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 11:00:00, Price: 8500, Location: Hyderabad Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 11:00:00, Price: 2000, Location: Vishakhapatnam Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 10:59:59, Price: 4500, Location: Vijayawada Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 10:10:52, Price: 9000, Location: Chennai Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 11:08:59, Price: 7500, Location: Goa
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP