JDBC中的元数据ResultSetMetadata

public interface ResultSetMetaData可用于获取关于 ResultSet  对象中列的类型和属性信息的对象。相当于反射Class

代码示例如下:
package com.ambow.day21.jdbc.NewFecture;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import com.ambow.day19.jdbc.util.MySqlDBConnection;
public class ResultMetadateTest {
	public static void main(String args[]) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs=null;
		ResultSetMetaData rsm = null;
		try {
			con = MySqlDBConnection.DBConnection();
			String sql = "select * from student order by id";
			pstm= con.prepareStatement(sql);
			rs=pstm.executeQuery();
			rsm=pstm.getMetaData();
			System.out.println("*****开始获取元数据*****");
			System.out.print("此 ResultSet 对象中一共有列数为————");
			System.out.println(rsm.getColumnCount());
			for(int i=1;i<=rsm.getColumnCount();i++){
				System.out.println("第"+i+"字段的列名为:"+rsm.getColumnName(i)+"  数据类型为:"+rsm.getColumnTypeName(i));
			}
			System.out.println("数据库名称为:"+rsm.getCatalogName(1));
			System.out.println("数据库表名称为:"+rsm.getTableName(1));
			System.out.println("是否自动为指定列进行编号 :"+rsm.isAutoIncrement(1));
			System.out.println("指定的列是否明确不可写入:"+rsm.isReadOnly(1));
			System.out.println("是否可以在 where 子句中使用指定的列:"+rsm.isSearchable(2));
			System.out.println("在指定的列上进行写操作是否可以获得成功:"+rsm.isWritable(1));
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			MySqlDBConnection.closeResultSet(rs);
			MySqlDBConnection.closePreparedStatement(pstm);
			MySqlDBConnection.closeConnection(con);
		}

	}
}

 

 

<!--EndFragment-->

你可能感兴趣的:(java,sql,jdbc)