日期:2014-05-16  浏览次数:20512 次

jdbc 得到表的所有列名和类型
public String getAllFields(String tablename){
Connection conn=null;
Statement st=null;
ResultSet rs=null;
ResultSetMetaData rsm=null;

String userName="****";
String passwd=""****";
String url="jdbc:oracle:thin:@192.168.1.200:1521:****";
String driver="cracle.jdbc.driver.Orcale";

try{
  Class.forName(driver);
  conn=DriverManager.getConnection(url,userName,passwd);
  stmt=conn.createStatement();
  String sql="select * from mytable"; 
  rs=stmt.executeQuery(sql);
  rsm=rs.getMetaData();
  int num=rsm.getColumnCount();
  for(int i=1;i<=num;i++){
    String columnName=rsmd.getColumnName(i);
    int precision=rsmd.getPrecision(i);
    String type=rsmd.getColumnTypeName(i);
    System.out.println ("name:"+columnName+"precision:"+precision+"type"+type);
}catch(Excepiton e){
  e.printStackTrace();
}

}