jdbc preparestatement 批量提交问题

 public static void addRecord(String tableName,String title,List<Object> contents){
        long count = 0;
        Integer time = 0;
        Connection conn = getConn();
        try {
            conn.setAutoCommit(false);
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        PreparedStatement ps = null;
        StringBuffer sql = new StringBuffer();
        StringBuffer sb = new StringBuffer();
        try {
        for (int i = 0; i < contents.size(); i++) {
            sql.setLength(0);
            sb.setLength(0);
            sb.append(((List)contents.get(i)).get(0).toString());
        //sql.append("insert into "+tableName+" ("+title+") values ( "+sb.toString().replaceAll("\"","'")+" )");
        sql.append("insert into "+tableName+" ("+title+") values ( "+sb.toString()+" )");
            ps = conn.prepareStatement(sql.toString());
            ps.addBatch();
            count++;
            if(time<=(count/10000)){
                if(count%10000==0){
                    time++;
                    ps.executeBatch();
                    conn.commit();
                    //conn.setAutoCommit(true);
                }
            }
            if((time==(contents.size()/10000))&&(count%10000)!=0&&count==contents.size()){
                System.out.println("提交数据");
                int[] executeBatch = ps.executeBatch();
                System.out.println(executeBatch.toString());
                conn.commit();
                //conn.setAutoCommit(true);
            }

        }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            Close(null, ps, null);
        }

数据不能全部提交而且只有一条不是最后的记录,是中间的记录,为什么??

commit不是应该在for循环外面吗?

你逻辑有问题,使用预编译的,语法不是这个写的!

这个是我修改之后的使用了statement


 @SuppressWarnings("unchecked")
    public static void addRecord(String tableName,String title,List<Object> contents){
        long count = 0;
        Integer time = 0;
        Connection conn = getConn();
        try {
            conn.setAutoCommit(false);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        Statement sm = null;
        StringBuffer sb = new StringBuffer();
        try {
            sm = conn.createStatement();
        for (int i = 0; i < contents.size(); i++) {
            sb.setLength(0);
            sb.append(((List<Object>)contents.get(i)).get(0).toString());
            sm.addBatch("insert into "+tableName+" ("+title+") values ("+sb.toString()+")");
            count++;
            if(time<=(count/10000)){
                if(count%10000==0){
                    time++;
                    sm.executeBatch();
                    conn.commit();
                    //conn.setAutoCommit(true);
                }
            }
            if((time==(contents.size()/10000))&&(count%10000)!=0&&count==contents.size()){
                sm.executeBatch();
                conn.commit();
                //conn.setAutoCommit(true);
            }
        }

        } catch (SQLException e) {
            //数据插入失败
            e.printStackTrace();
        }finally{
            Close(null,null,sm, null);
        }

    }


如果少一条数据 ,看报错不 是不是数据库有唯一索引 或着联合索引

conn.commit();提交后把 要StringBuffer sb = new StringBuffer();

已经解决了,使用了statement