查询表字段类型

/*
* 根据表明查询字段类型
* */
private Map<String, Integer> queryFieldMap(String tableName, String dbName){
if(tableName == null || dbName == null){
return null;
}

Map<String, Integer> resultMap = new HashMap<String, Integer>();

Connection conn = null;
Statement stat = null;
ResultSet rs = null;


try {
conn = ConnectionPool.getInstance().getConnection(dbName);
stat = conn.createStatement();
rs = stat.executeQuery("select * from " + tableName + " where 1 = 0");
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
for(int index = 1; index <= count; index++){
String colName = rsmd.getColumnName(index).toLowerCase();
int colType = rsmd.getColumnType(index);

resultMap.put(colName, colType);
}
} catch (SQLException e) {
log.error(e.getMessage());
e.printStackTrace();
resultMap = null;
} finally {
try { rs.close(); } catch (Exception e) { e.printStackTrace(); }
try { stat.close(); } catch (Exception e) { e.printStackTrace(); }
try { conn.close(); } catch (Exception e) { e.printStackTrace(); }
}

return resultMap;

}

2.引用的地方:

fieldType = fieldTypeMap.get(fieldName.toLowerCase());

if(fieldType == java.sql.Types.VARCHAR){ //String
PreStatSetValue.setString(pstmt, index + 1, fieldValue);
} else if(fieldType == java.sql.Types.INTEGER || fieldType == java.sql.Types.NUMERIC || fieldType == java.sql.Types.SMALLINT){ //int
try{
PreStatSetValue.setInt(pstmt, index + 1, Integer.parseInt(fieldValue));
}catch(Exception e){
log.error("更新cssf_ts_statistics, 字段值设置错误:fieldName = " + fieldName + ", 要求类型:" + fieldType + ", 设置值:" + fieldValue);
PreStatSetValue.setInt(pstmt, index + 1, 0);
}

} else if(fieldType == java.sql.Types.DATE || fieldType == java.sql.Types.TIMESTAMP){ //date
PreStatSetValue.setTimestamp(pstmt, index + 1, fieldValue);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java海洋

你的鼓励,是我写下去的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值