DataBaseMeataData--元数据

一、定义

元数据:数据库,表,列的定义信息。

Connection.getMetaData()

1.DataBaseMetaData对象:数据库元数据

相关方法:

a. getURL()

b. getUserName()

c. getDriverName()

<span style="white-space:pre">		</span>Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ComboPooledDataSource source = new ComboPooledDataSource();
		try{
			conn = source.getConnection();
			//--获取当前数据库的元数据
			DatabaseMetaData metaData = conn.getMetaData();
			//----获取数据库连接时使用的URL
			String url = metaData.getURL();
			System.out.println(url);
			//----获取数据库的用户名
			String username = metaData.getUserName();
			System.out.println(username);
			//----获取驱动的名称
			String driverName = metaData.getDriverName();
			System.out.println(driverName);
			//----获取数据库中指定表的主键信息
			rs = metaData.getPrimaryKeys(null, null, "account");
			while(rs.next()){
				short cseq = rs.getShort("KEY_SEQ");
				String cname = rs.getString("COLUMN_NAME");
				System.out.println(cseq+":"+cname);
			}
			//----获取表
			rs = metaData.getTables(null, null, "%", new String[]{"TABLE"});
			while(rs.next()){
				String tabName = rs.getString("TABLE_NAME");
				System.out.println(tabName);
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbUtils.closeQuietly(conn, ps, rs);
		}

2.ParameterMetaData 参数元数据

<span style="white-space:pre">			</span>conn = source.getConnection();
			ps = conn.prepareStatement("select * from account where name=? and money=?");
			//--获取参数元数据
			ParameterMetaData metaData = ps.getParameterMetaData();
			//----参数的个数
			int count = metaData.getParameterCount();
			System.out.println(count);

3.ResultSetMetaData 结果集元数据

<span style="white-space:pre">			</span>conn = source.getConnection();
			ps = conn.prepareStatement("select * from account");
			rs = ps.executeQuery();
			//--获取结果集元数据
			ResultSetMetaData metaData = rs.getMetaData();
			//----获取结果集中的列数
			int cc = metaData.getColumnCount();
			System.out.println(cc);
			//----获取结果集中指定列的名称
			String cn = metaData.getColumnName(2);
			System.out.println(cn);
			//----获取结果集中指定列的类型的名称
			String ct = metaData.getColumnTypeName(3);
			System.out.println(ct);
4.结果集Demo

<span style="white-space:pre">		</span>Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ComboPooledDataSource source = new ComboPooledDataSource();
		try{
			conn = source.getConnection();
			ps = conn.prepareStatement("select * from account");
			rs = ps.executeQuery();
			//--获取结果集元数据
			ResultSetMetaData metaData = rs.getMetaData();
			//----获取结果集中的列数
			int cc = metaData.getColumnCount();
			
			System.out.println("-------------------------------------------------------");
			for(int i = 1;i<=cc;i++){
				String column_name = metaData.getColumnName(i);
				String column_type = metaData.getColumnTypeName(i);
				System.out.print(column_name+":"+column_type+"\t\t");
			}
			System.out.println();
			System.out.println("-------------------------------------------------------");
			while(rs.next()){
				for(int i =1 ;i<=cc;i++){
					Object obj = rs.getObject(i);
					System.out.print(obj+"\t\t\t");
				}
				System.out.println();
			}
			
			System.out.println("-------------------------------------------------------");
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbUtils.closeQuietly(conn, ps, rs);
		}


你可能感兴趣的:(database)