JDBC获取结果集与数据库的元数据
连接.getMetaData()获取数据库的元对象
结果集.getMetaData()获取结果集的元对象
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; /** * 结果集的元数据 * * @author Administrator * */ public class TestResultMetaData { public static void main(String[] args) throws SQLException { Connection connection = DBUtil.getConnection(); DatabaseMetaData dm = connection.getMetaData();//获取数据库的元对象 System.out.println(dm.getDriverVersion());//输出数据库版本 String sql = "select empno,ename,job,sal,deptno from emp"; PreparedStatement ps = connection.prepareStatement(sql); ResultSet rs = ps.executeQuery(); // 获得结果集元数据对象 ResultSetMetaData rsmd = rs.getMetaData(); // 获得结果集列数 int count = rsmd.getColumnCount(); System.out.println(count); for (int i = 1; i <= count; i++) { System.out.println(rsmd.getColumnName(i) + ":" + rsmd.getColumnTypeName(i)); } } }