public static void importList2DB(List list) throws IOException {
String sql="insert into ocs_feeitem(fee_id,description_text,product_line_descr,first_subject,cqd_cdr_type) values(";
String sql2="insert into ocs_feeitem(fee_id,description_text,product_line_descr,first_subject,cqd_cdr_type) values(?,?,?,?,?)";
PreparedStatement ps=null;
Connection con =null;
Statement stmt=null;
try{
System.out.println("before con");
con=ConnectionFactory.getConnection();
con.setAutoCommit(false);
ps=con.prepareStatement(sql2);
System.out.println("before stmt");
stmt=con.createStatement();
for(int i=0;i<list.size();i++){
System.out.println("---------------in for");
TransferItem ti=(TransferItem)list.get(i);
sql=sql+ti.getFee_id()+",'"+ti.getDescription_text()+"','"+ti.getProduct_line_descr()+"','"+ti.getFirst_subject()+"',"+ti.getCqd_cdr_type()+")";
/*ps.setInt(0, ti.getFee_id());
ps.setString(0, ti.getDescription_text());
ps.setString(1, ti.getProduct_line_descr());
ps.setString(2, ti.getFirst_subject());
ps.setInt(1, ti.getCqd_cdr_type());
ps.executeUpdate();
*/
stmt.executeUpdate(sql);
System.out.println("-------------");
}
con.commit();
}catch(SQLException ex){
ex.printStackTrace();
}finally{
ConnectionFactory.close(con, ps, null);
}
}
public static void main(String[] args) throws IOException{
List list=readExcel("D:\\bak\\transfer.xls");
//list中大概1000多条记录,但是执行第二条时报错无法插入
importList2DB(list);
}
String sql="insert into ocs_feeitem(fee_id,description_text,product_line_descr,first_subject,cqd_cdr_type) values(";
String sql2="insert into ocs_feeitem(fee_id,description_text,product_line_descr,first_subject,cqd_cdr_type) values(?,?,?,?,?)";
PreparedStatement ps=null;
Connection con =null;
Statement stmt=null;
try{
System.out.println("before con");
con=ConnectionFactory.getConnection();
con.setAutoCommit(false);
ps=con.prepareStatement(sql2);
System.out.println("before stmt");
stmt=con.createStatement();
for(int i=0;i<list.size();i++){
System.out.println("---------------in for");
TransferItem ti=(TransferItem)list.get(i);
sql=sql+ti.getFee_id()+",'"+ti.getDescription_text()+"','"+ti.getProduct_line_descr()+"','"+ti.getFirst_subject()+"',"+ti.getCqd_cdr_type()+")";
/*ps.setInt(0, ti.getFee_id());
ps.setString(0, ti.getDescription_text());
ps.setString(1, ti.getProduct_line_descr());
ps.setString(2, ti.getFirst_subject());
ps.setInt(1, ti.getCqd_cdr_type());
ps.executeUpdate();
*/
stmt.executeUpdate(sql);
System.out.println("-------------");
}
con.commit();
}catch(SQLException ex){
ex.printStackTrace();
}finally{
ConnectionFactory.close(con, ps, null);
}
}
public static void main(String[] args) throws IOException{
List list=readExcel("D:\\bak\\transfer.xls");
//list中大概1000多条记录,但是执行第二条时报错无法插入
importList2DB(list);
}
解决方案 »
- 怎样在jsp页面中显示doc文件中的内容(doc文件中包含图片)?
- Groovy 到底比java好在哪儿?
- jsp怎样实现后台动态维护,前台生成静态页面技术呀,谢谢了?
- JSP中session标记的问题?
- 关于struts+hibernate+spring的问题~~~~~~~~~~~~~~~~~!帮忙解决一下啊,谢谢了!
- statement 的问题!!求助!
- 大家帮我登陆看看,这个网站的“add photo”功能用什么写的?
- 我晕了,这也有错:【 if(rs.getString(1)==1)】
- 在JSP中如何表示空格?
- jsp支持javascript吗?
- jsf标签 选项被选中,如何知道被选中哪个
- eclipse中 给xml 添加 dtd 的步骤!
应该是这方面出问题,调试可以看看SQL语句是否正确
应该是这个设置错了吧,如果用PreparedStatement对象这个好像可以。
如果用Statement对象你最好把这个设置成true。
LZ要做的是批量更新吧?!批量更新 需要使用 PreparedStatement 来做,并且其语法应该是:String sql2="insert into ocs_feeitem(fee_id,description_text,product_line_descr,first_subject,cqd_cdr_type) values(?,?,?,?,?)"; ps=con.prepareStatement(sql2);
for(int i=0;i <list.size();i++){ //建议一次更新100条左右
ps.setInt(1, ti.getFee_id()); //JDBC是从1开始的
...
ps.setInt(1, ti.getCqd_cdr_type());
ps.addBatch(); //和非指更新的特别之处
//ps.clearParameters(); //可改善效屡 }ps.executeBatch();