Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] PreparedStatement.executeBatch() When the Insert Sql Column Name Case Mismatch, Throws SQLServerException "Unable to retrieve column metadata." #2588

Open
wooln opened this issue Jan 21, 2025 · 0 comments · May be fixed by #2589

Comments

@wooln
Copy link

wooln commented Jan 21, 2025

Driver version

12.8.1

SQL Server version

Microsoft SQL Server 2019 (RTM-CU29) (KB5046365) - 15.0.4405.4 (X64) 
	Oct 23 2024 08:45:19 
	Copyright (C) 2019 Microsoft Corporation
	Developer Edition (64-bit) on Linux (Ubuntu 20.04.6 LTS) <X64>

Client Operating System

openSUSE Leap 15.6 x86_64

JAVA/JVM version

java version 22

Problem description

PreparedStatement.executeBatch(), when the insert sql column name case mismatch, Throws SQLServerException "Unable to retrieve column metadata."

  1. create table, c1 lower case.
    CREATE TABLE varchartable1 (c1 varchar(10))

  2. executeBatch

    Column name use lower case c1, succeed
    con.prepareStatement("INSERT INTO varchartable1 (c1) VALUES(?)")

    Column name use upper case C1, throws SQLServerException "Unable to retrieve column metadata."
    con.prepareStatement("INSERT INTO varchartable1 (C1) VALUES(?)")

The unit tests.

  1. succeed
@Test
public void testExecuteBatchColumnCaseMatching() throws Exception {
    String varcharTable1 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("varchartable1"));
    // Insert Timestamp using prepared statement when useBulkCopyForBatchInsert=true
    try (Connection con = DriverManager.getConnection(connectionString
            + ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) {
        try (Statement statement = con.createStatement()) {
            TestUtils.dropTableIfExists(varcharTable1, statement);
            String createSql = "CREATE TABLE" + varcharTable1 + " (c1 varchar(10))";
            statement.execute(createSql);
        }
        try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + varcharTable1 + "(c1) VALUES(?)")) {
            preparedStatement.setObject(1, "value1");
            preparedStatement.addBatch();
            preparedStatement.setObject(1, "value2");
            preparedStatement.addBatch();
            preparedStatement.executeBatch();
        }
    }
}
  1. throws SQLServerException "Unable to retrieve column metadata."
@Test
public void testExecuteBatchColumnCaseMismatch() throws Exception {
    String varcharTable1 = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("varchartable1"));
    // Insert Timestamp using prepared statement when useBulkCopyForBatchInsert=true
    try (Connection con = DriverManager.getConnection(connectionString
            + ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) {
        try (Statement statement = con.createStatement()) {
            TestUtils.dropTableIfExists(varcharTable1, statement);
            String createSql = "CREATE TABLE" + varcharTable1 + " (c1 varchar(10))";
            statement.execute(createSql);
        }
        // upper case C1
        try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + varcharTable1 + "(C1) VALUES(?)")) {
            preparedStatement.setObject(1, "value1");
            preparedStatement.addBatch();
            preparedStatement.setObject(1, "value2");
            preparedStatement.addBatch();
            try {
                preparedStatement.executeBatch();
                fail("Should have failed");
            } catch (Exception ex) {
                assertInstanceOf(SQLServerException.class, ex);
                assertEquals("Unable to retrieve column metadata.", ex.getMessage());
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: To be triaged
1 participant