从sql server使用jdbc获取成功批量插入的行数

时间:2020-12-22 09:37:58

I have Java code to bulk-insert tab file to SQL Server. I want to get the count of how many records were inserted. I tried using @@rowcount but I'm getting an error that "Statement did not return a result set". If I run the bulk insert statement in management studio, I can get the count.

我有Java代码批量插入选项卡文件到SQL Server。我想知道插入了多少条记录。我尝试使用@@ rowcount但是我收到一条错误“Statement没有返回结果集”。如果我在管理工作室中运行批量插入语句,我可以得到计数。

Statement stmt = sqlConnection.createStatement();         
ResultSet rs = stmt.executeQuery ("BULK INSERT schema1.table1 FROM 'd:\temp1\file1.tab' SELECT @@rowcount");

Is there any way to get the inserted count?

有没有办法获得插入计数?

1 个解决方案

#1


3  

I'm not familiar with SQL Server but it seems like you'll want to issue an executeUpdate instead of an executeQuery

我不熟悉SQL Server,但似乎你想要发出executeUpdate而不是executeQuery

Statement stmt = sqlConnection.createStatement();         
int insertedRowCount = stmt.executeUpdate("BULK INSERT schema1.table1 FROM 'd:\temp1\file1.tab'");

#1


3  

I'm not familiar with SQL Server but it seems like you'll want to issue an executeUpdate instead of an executeQuery

我不熟悉SQL Server,但似乎你想要发出executeUpdate而不是executeQuery

Statement stmt = sqlConnection.createStatement();         
int insertedRowCount = stmt.executeUpdate("BULK INSERT schema1.table1 FROM 'd:\temp1\file1.tab'");