ResultSetMetaData是返回结果集的元集合。 结果是记录的集合,元集合是记录各项的集合;
公共类结果集道{
CoffeeDemo coffee=new CoffeeDemo (;
publicvoidviewtabel (字符串表) throws ClassNotFoundException { )。
//1负载驱动器
class.forname (com.MySQL.JDBC.driver );
String str='select * from ' table;
try (连接con=connection demo.connection (; //连接到数据库另外创建的连接功能ConnectionDemo.java;
preparedstatementpatmt=con.preparestatement (str;
resultsetset=pat mt.execute query (; (//结果集
resultsetmetadatarsmd=set.get metadata (; //元集合
int in=rsmd.getColumnCount (;
for(intx=1; x=in; x () /打印表table中的列名;
string column=rsmd.getcolumn name (x;
system.out.print(column't ';
}
System.out.println (' ';
与wile(set.next ) () /打印表table中的列名对应的元素;
for(intx=1; x=in; x )
{
system.out.print (set.getstring (rsmd.getcolumn name (x ) ) (t ) );
}
}
}catch(sqlexceptionex ) {
System.out.println (“无法连接”
ex.printStackTrace (;
}
}
}