Skip to content

Commit a65116e

Browse files
committed
Add a test for PreparedStatement
1 parent b57799b commit a65116e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -2749,6 +2749,31 @@ public void testExecuteUpdateInsertAndGenKeys() {
27492749
}
27502750
}
27512751

2752+
/**
2753+
* Tests executeUpdate using PreparedStatement for Insert followed by getGenerateKeys
2754+
*
2755+
* @throws Exception
2756+
*/
2757+
@Test
2758+
public void testPrepStmtExecuteUpdateInsertAndGenKeys() {
2759+
try (Connection con = getConnection()) {
2760+
String sql = "INSERT INTO " + tableName + " (NAME) VALUES('test')";
2761+
List<String> lst = Arrays.asList("ID");
2762+
String[] arr = lst.toArray(new String[0]);
2763+
try(PreparedStatement stmt = con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS)) {
2764+
stmt.executeUpdate();
2765+
try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
2766+
if (generatedKeys.next()) {
2767+
int id = generatedKeys.getInt(1);
2768+
assertEquals(id, 4, "id should have been 4, but received : " + id);
2769+
}
2770+
}
2771+
}
2772+
} catch (SQLException e) {
2773+
fail(TestResource.getResource("R_unexpectedException") + e.getMessage());
2774+
}
2775+
}
2776+
27522777
/**
27532778
* Tests execute for Insert followed by getGenerateKeys
27542779
*

0 commit comments

Comments
 (0)