如何使用 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://127.0.0.1/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
广告