java讀取oracle表欄位資訊存到列表(LIST)中

來源:互聯網
上載者:User

今天在研究如何將oracle表欄位的資訊存到列表數組中,做了一點實驗,是在java1.5下,用myeclips做的,代碼如下:

public static List<Map<String,String>> gocilist(Connection conn,String table)
{
 ResultSet rs = null;
 Statement stmt=null;
 //Connection conn = null;
 String sqlm="select COLUMN_NAME,DATA_TYPE,DATA_LENGTH,DATA_PRECISION,DATA_SCALE,NULLABLE,COLUMN_ID from user_tab_columns where table_name =UPPER('"+table+"')";
 List<Map<String,String>> fv1 = new ArrayList<Map<String,String>>();
 Map<String,String> fmap = new HashMap<String,String>();
 try{
//     conn = dbcon.dbconn();
     stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
     rs= stmt.executeQuery(sqlm);
      while(rs.next()){
     fmap = new HashMap<String,String>();
        fmap.put("COLUMN_NAME", rs.getString("COLUMN_NAME"));//擷取欄位名
        fmap.put("DATA_TYPE",rs.getString("DATA_TYPE"));    //擷取資料類型
        fmap.put("DATA_LENGTH",rs.getString("DATA_LENGTH"));//擷取資料長度
        if(rs.getString("DATA_PRECISION") == null )
        {
         fmap.put("DATA_PRECISION","");
        }
        else
         fmap.put("DATA_PRECISION",rs.getString("DATA_PRECISION"));//擷取資料長度
        if(rs.getString("DATA_SCALE") == null)
        {
         fmap.put("DATA_SCALE","");
        }
        else
         fmap.put("DATA_SCALE",rs.getString("DATA_SCALE"));//擷取資料精度
        fmap.put("NULLABLE",rs.getString("NULLABLE"));    //擷取是否為空白
        fmap.put("COLUMN_ID",rs.getString("COLUMN_ID"));    //欄位序號
        fv1.add(fmap);
     }
      conn.close();
 }catch (Exception e) {
  e.printStackTrace();  // TODO: handle exception
 }
 return fv1;
}

測試語句如下:

     fcl=tablist.gocilist("SURVEY");
     while(i1 < fcl.size())
     {
      System.out.println("COLUMN_NAME=="+fcl.get(i1).get("COLUMN_NAME"));
      System.out.println("DATA_TYPE=="+fcl.get(i1).get("DATA_TYPE"));
      System.out.println("DATA_LENGTH=="+fcl.get(i1).get("DATA_LENGTH"));
      System.out.println("COLUMN_ID=="+fcl.get(i1).get("COLUMN_ID"));
      i1=i1+1;
     }
    

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.