Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Insert语句包含1000多个元素_Java_Sql_String_Jdbc_Set - Fatal编程技术网

Java Insert语句包含1000多个元素

Java Insert语句包含1000多个元素,java,sql,string,jdbc,set,Java,Sql,String,Jdbc,Set,我正在尝试使用JDBC向数据库添加100000个名称。我知道MS SQL不允许大规模插入超过1000个elmts,所以我通过将主集合分解为包含1000个或更少的集合来实现这一点。以下是我的代码: StringJoiner joiner = new StringJoiner("\'), (\'", "INSERT INTO Names (Name) VALUES (\'", "\');"); ExecutorService threadpool = Executors.newFixedTh

我正在尝试使用JDBC向数据库添加100000个名称。我知道MS SQL不允许大规模插入超过1000个elmts,所以我通过将主集合分解为包含1000个或更少的集合来实现这一点。以下是我的代码:

StringJoiner joiner = new StringJoiner("\'), (\'", "INSERT INTO Names (Name) VALUES (\'", "\');");
    ExecutorService threadpool = Executors.newFixedThreadPool(100);
    while(names.size() > 0) {

        int count = Math.min(1000, names.size());
        HashSet<String> set = new HashSet();
        Iterator iterator = names.iterator();
        for(int i = 0; i < count; i++) {
           set.add((String) iterator.next());
        }
        names.removeAll(set);
        for(String s: set)
        {
            joiner.add(s);
        }
       // System.out.println(joiner.toString());

        threadpool.submit(() -> {
            PreparedStatement query = null;
            try {
                query = connect.prepareStatement(joiner.toString());
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                query.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });

    }


}
StringJoiner-joiner=新的StringJoiner(\'),(\'),“插入名称(名称)值(\'),“\');”;
ExecutorService线程池=Executors.newFixedThreadPool(100);
while(names.size()>0){
int count=Math.min(1000,names.size());
HashSet=newhashset();
迭代器迭代器=names.Iterator();
for(int i=0;i{
PreparedStatement查询=null;
试一试{
query=connect.prepareStatement(joiner.toString());
}捕获(SQLE异常){
e、 printStackTrace();
}
试一试{
query.executeUpdate();
}捕获(SQLE异常){
e、 printStackTrace();
}
});
}
}
这会引发以下异常:
com.microsoft.sqlserver.jdbc.SQLServerException:INSERT语句中的行值表达式数超过了允许的最大1000行值数。


为什么要添加1000多行?

为什么不使用批插入?定义批次大小1000,并在每次1000个查询的批次中插入查询

String sql = "insert into names(name) values (?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);

final int batchSize = 1000;
int count = 0;

for (Employee employee: employees) {

    ps.setString(1, employee.getName());
    ps.addBatch();

    if(++count % batchSize == 0) {
        ps.executeBatch();
    }
}
ps.executeBatch(); // insert remaining records
ps.close();
connection.close();

这是干什么用的如果(++count%batchSize==0){ps.executeBatch();}它只在计数为1000、2000、3000等时执行批处理。不是每一个计数。调试您的代码。。。检查你的电视机的尺寸。。。在循环中放置计数器,依此类推。完成后,集合为1000在添加1000 get之前,原始查询字符串是否已经有1?