ResultSetMetaData
标签: ResultSetMetaData
2013-09-21 19:54
1033人阅读
评论(0)
收藏
举报
分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
[java] view plaincopy
- package dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import cn.itcast.web.jdbc.util.JdbcUtil;
- //演示三种元数据库的用法 需要 配置tomcat服务器 还需要相关的jar包支持一些只是简单的元数据用法留着以后复习
- public class Demo1 {
- public static void main(String[] args) throws Exception {
- Connection conn = JdbcUtil.getMySqlConnection();
- String sql = "select * from user";
- PreparedStatement pstmt = conn.prepareStatement(sql);
- ResultSet rs = pstmt.executeQuery();
- //取得结果集的相关元数据
- ResultSetMetaData rsmd = rs.getMetaData();
- int size = rsmd.getColumnCount();
- for(int i=0;i<size;i++){
- //取得每列的列名
- String columnName = rsmd.getColumnName(i+1);
- //取得每列的类型
- int typeCode = rsmd.getColumnType(i+1);
- System.out.println(columnName+":"+typeCode);
- }
- /*取是有关SQL的相关元数据
- String sql = "insert into user(username,password,birthday,salary) values(?,?,?,?)";
- ParameterMetaData psmd = pstmt.getParameterMetaData();
- int size = psmd.getParameterCount();
- System.out.println("共有" + size+"个参数");
- */
- /*取得DataBaseMetaData
- DatabaseMetaData dbmd = conn.getMetaData();
- String driver = dbmd.getDriverName();
- String url = dbmd.getURL();
- int level = dbmd.getDefaultTransactionIsolation();
- String productName = dbmd.getDatabaseProductName();
- boolean flag = dbmd.isReadOnly();
- System.out.println("flag="+flag);
- System.out.println("driver="+driver);
- System.out.println("url="+url);
- System.out.println("level="+level);
- System.out.println("productName="+productName);
- */
- }
- }