日期:2014-05-18  浏览次数:20889 次

怎样将Object中的数据转换成Int?
我想将一批数据插入到数据库
这一批数据是在OBJECT中


public   static   int   creatVector(Vector   v)throws   Exception{
int   i=0;
Connection   conn   =   null;
PreparedStatement   pstmt=null;
try   {
conn   =   ConnectionHandler.getConnection();
conn.setAutoCommit(false);
pstmt   =   conn.prepareStatement(CREATE);

  Enumeration   e=v.elements();
  while(e.hasMoreElements())
  {
        Object[]   obj=(Object[])e.nextElement();
                pstmt.setInt(1,   Integer.parseInt((String)obj[0]));
                pstmt.setTimestamp(2,   (Timestamp)   obj[1]);
                pstmt.execute();
                i++;
  }

conn.commit();
}   finally   {
conn.rollback();
ConnectionHandler.close(conn,pstmt);


}
return   i;
}


pstmt.setInt(1,   Integer.parseInt((String)obj[0]));
这句有问题
提示是:
java.lang.ClassCastException:   java.lang.Integer
at   com.microthink.web.sitestat.home.AccessIpNumHome.creatVector

------解决方案--------------------
这种结构强制转换的问题我也见到过,而且我的是时好时坏很奇怪。

既然你有了Vector结构,当初在Vector中存放的对象是什么类型?假如是String类型,不如遍历试一下,不用Enumeration呢?

for(int i=0;i <v.size();i++){
String vGet = (String)v.get(i);
int yourNeedInt=Integer.pauseInt(vGet);
}
------解决方案--------------------
要看能不恩能够转换过去也就是最开始是什么类型的