PreparedStatement.setObject(int i,Object obj) 无效的列类型

当往下面的方法传递参数时,传入了java.util.Date类型,因此报错
“无效的列类型”,需要把java.util.Date转换为java.sql.Date

query("select distinct bm.n_bh BH,bm.c_mc MC,bm.n_zgbmbh PARENT,bm.n_zgjcybh,bm.n_xssx from v_ywzc_wsjcjg v,t_zzjg_bm bm where v.cbbm=bm.n_bh and v.cbdw=? and v.jarq BETWEEN ? AND ? order by bm.n_xssx",new Object[]{new Long(dwbh),date1,date2});


public List query(final String sql,final Object[] params){

return (List)getJdbcTemplate().execute(sql, new PreparedStatementCallback(){
public Object doInPreparedStatement(PreparedStatement pst) throws SQLException{
if(log.isDebugEnabled()){
log.debug(sql);
}

//pst.execute(sql);
if(params!=null && params.length>0){
for(int i=1;i<=params.length;i++){
pst.setObject(i,params[i-1]);
}
}
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
List l = new ArrayList();
while(rs.next()){
Map m = new HashMap();
for(int i=1;i<=colCount;i++){
m.put(rsmd.getColumnName(i),rs.getObject(i));
}
l.add(m);
}
return l;
}
});
}

==========原因============================================
Class oracle.jdbc.driver.OraclePreparedStatement

setObject(int paramIndex,Object x) throws SQLException

Sets the value of the designated parameter with the given object. This method is like setObject above, except that it finds out the SQL type base on the given object's type.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
x - the object containing the input parameter value
targetSqlType - the SQL type (as defined in [color=red]java.sql.Types[/color]) to be sent to the database
Throws: SQLException
if a database access error occurs

你可能感兴趣的:(数据库)