JDBC查询列名

来源:互联网 发布:如何卸载mac版office 编辑:程序博客网 时间:2024/04/27 23:12

st = conn.createStatement();
rs = st.executeQuery(sqlString);
ResultSetMetaData meta=rs.getMetaData();
tdlength = meta.getColumnCount();//取得查询结果的列数

//遍历列数,判断各列类型然后取值
   for(int j=1;j<=tdlength;j++){
     int type = meta.getColumnType(j);
     //System.out.println(Types.DATE+"==="+type);
     if(Types.VARCHAR==type){
      res.put("tr"+i+"td"+j, rs.getString(j));
     }else if(Types.DATE==type){
      if("00:00:00".equals(rs.getTime(j).toString())){
       res.put("tr"+i+"td"+j, MESFormat.dateFormat(rs.getDate(j)));
      }else {
       res.put("tr"+i+"td"+j, MESFormat.dateTosFormat(rs.getTimestamp(j)));
      }
     }else if(Types.INTEGER==type){
      res.put("tr"+i+"td"+j, rs.getInt(j));
     }else if(Types.CHAR==type){
      res.put("tr"+i+"td"+j, rs.getString(j));
     }else if(Types.DOUBLE==type){
      res.put("tr"+i+"td"+j, rs.getDouble(j));
     }else if(Types.BOOLEAN==type){
      res.put("tr"+i+"td"+j, rs.getBoolean(j));
     }else if(Types.FLOAT==type){
      res.put("tr"+i+"td"+j, rs.getFloat(j));
     }else if(Types.NCHAR==type){
      res.put("tr"+i+"td"+j, rs.getString(j));
     }else if(Types.TIMESTAMP==type){
      res.put("tr"+i+"td"+j, MESFormat.dateTosFormat(rs.getTimestamp(j)));
     }else if(Types.NUMERIC==type){
      res.put("tr"+i+"td"+j, rs.getBigDecimal(j));
     }else if(Types.NULL==type){
      res.put("tr"+i+"td"+j, "");
     }else if(Types.OTHER==type){
      res.put("tr"+i+"td"+j, "-");
     }
    }

原创粉丝点击