我正在使用MSDN网站上的批量复制代码,实际上没有任何改变。我在DB中有正确的列表(SQLServer 2008 R2 Management Studio 10)。但是当我尝试运行此代码时,在addColumnMetadata行上抛出异常:
com.microsoft.sqlserver.jdbc.SQLServerException:第17列无效。 请检查列映射。在 com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.validateColumnMappings(SQLServerBulkCopy.java:1747) 在 com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeToServer(SQLServerBulkCopy.java:1514) 在 com.microsoft.sqlserver.jdbc.SQLServerBulkCopy.writeToServer(SQLServerBulkCopy.java:628)
我找不到错误。谁能帮帮我吗?以下是从MSDN复制的代码的一部分。 for循环和文件路径是我改变的唯一内容。
public static void main(String[] args)
{
String connectionString = GetConnectionString();
SQLServerBulkCSVFileRecord fileRecord = null;
try
{
// Get data from the source file by loading it into a class that implements ISQLServerBulkRecord.
// Here we are using the SQLServerBulkCSVFileRecord implementation to import the example CSV file.
fileRecord = new SQLServerBulkCSVFileRecord("F:/test/test1.csv", true);
// Set the metadata for each column to be copied.
for(int i = 0;i < 17;i++)
{
fileRecord.addColumnMetadata(i+1, null, java.sql.Types.VARCHAR, 10, 0);
}
// Open a destinationConnectio to the AdventureWorks database.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
try (Connection destinationConnection = DriverManager.getConnection(connectionString))
{
try (Statement stmt = destinationConnection.createStatement())
{
// Perform an initial count on the destination table.
long countStart = 0;
try (ResultSet rsRowCount = stmt.executeQuery(
"SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns1;"))
{
rsRowCount.next();
countStart = rsRowCount.getInt(1);
System.out.println("Starting row count = " + countStart);
}
// Set up the bulk copy object.
// Note that the column positions in the source
// data reader match the column positions in
// the destination table so there is no need to
// map columns.
try (SQLServerBulkCopy bulkCopy =
new SQLServerBulkCopy(destinationConnection))
{
bulkCopy.setDestinationTableName("dbo.BulkCopyDemoDifferentColumns1");
try
{
// Write from the source to the destination.
bulkCopy.writeToServer(fileRecord);
}
catch (Exception e)
{
// Handle any errors that may have occurred.
e.printStackTrace();
}
}
// Perform a final count on the destination
// table to see how many rows were added.
try (ResultSet rsRowCount = stmt.executeQuery(
"SELECT COUNT(*) FROM dbo.BulkCopyDemoDifferentColumns1;"))
{
rsRowCount.next();
long countEnd = rsRowCount.getInt(1);
System.out.println("Ending row count = " + countEnd);
System.out.println((countEnd - countStart) + " rows were added.");
}
}
}
}
catch (Exception e)
{
// Handle any errors that may have occurred.
e.printStackTrace();
}
finally
{
if (fileRecord != null) try { fileRecord.close(); } catch(Exception e) {}
}
}
少于17列,一切正常。完整代码位于msdn
这是表结构:
创建表BulkCopyDemoDifferentColumns1
(
name1 VARCHAR(10),
name2 VARCHAR(10),
name3 VARCHAR(10),
name4 VARCHAR(10),
name5 VARCHAR(10),
name6 VARCHAR(10),
name7 VARCHAR(10),
name8 VARCHAR(10),
name9 VARCHAR(10),
name10 VARCHAR(10),
name11 VARCHAR(10),
name12 VARCHAR(10),
name13 VARCHAR(10),
name14 VARCHAR(10),
name15 VARCHAR(10),
name16 VARCHAR(10),
name17 VARCHAR(10)
)
答案 0 :(得分:0)
您提到csv文件有2行,第一行是标题。
SQLServerBulkCopy对象要求该行以\r\n
终止,而不仅仅\n
。确保您的CSV是这种情况。
答案 1 :(得分:0)
使用jdk 1.8可以正常工作。 如果您使用jre7,即使更改为最后一个驱动程序mssql-jdbc 6.4.0.jre7也无法使用:(