Skip to content

Commit 79d9f7a

Browse files
committed
JDBC parameter binding uses JDBC 3.0 ParameterMetaData (if available) for type determination
1 parent 9d3cfcd commit 79d9f7a

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -235,10 +235,10 @@ public PreparedStatement createPreparedStatement(Connection con) throws SQLExcep
235235
ps = con.prepareStatement(this.actualSql, PreparedStatement.RETURN_GENERATED_KEYS);
236236
}
237237
}
238-
catch (AbstractMethodError ex) {
238+
catch (AbstractMethodError err) {
239239
throw new InvalidDataAccessResourceUsageException(
240-
"The JDBC driver is not compliant to JDBC 3.0 and thus " +
241-
"does not support retrieval of auto-generated keys", ex);
240+
"Your JDBC driver is not compliant with JDBC 3.0 - " +
241+
"it does not support retrieval of auto-generated keys", err);
242242
}
243243
}
244244
else if (resultSetType == ResultSet.TYPE_FORWARD_ONLY && !updatableResults) {

org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java

+27-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.sql.Blob;
2323
import java.sql.Clob;
2424
import java.sql.DatabaseMetaData;
25+
import java.sql.ParameterMetaData;
2526
import java.sql.PreparedStatement;
2627
import java.sql.SQLException;
2728
import java.sql.Types;
@@ -189,7 +190,7 @@ private static void setParameterValueInternal(
189190
if (inValue instanceof SqlParameterValue) {
190191
SqlParameterValue parameterValue = (SqlParameterValue) inValue;
191192
if (logger.isDebugEnabled()) {
192-
logger.debug("Overriding typeinfo with runtime info from SqlParameterValue: column index " + paramIndex +
193+
logger.debug("Overriding type info with runtime info from SqlParameterValue: column index " + paramIndex +
193194
", SQL type " + parameterValue.getSqlType() +
194195
", Type name " + parameterValue.getTypeName());
195196
}
@@ -228,18 +229,31 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, S
228229
boolean useSetObject = false;
229230
sqlType = Types.NULL;
230231
try {
231-
DatabaseMetaData dbmd = ps.getConnection().getMetaData();
232-
String databaseProductName = dbmd.getDatabaseProductName();
233-
String jdbcDriverName = dbmd.getDriverName();
234-
if (databaseProductName.startsWith("Informix") ||
235-
jdbcDriverName.startsWith("Microsoft SQL Server")) {
236-
useSetObject = true;
232+
ParameterMetaData pmd = null;
233+
try {
234+
pmd = ps.getParameterMetaData();
237235
}
238-
else if (databaseProductName.startsWith("DB2") ||
239-
jdbcDriverName.startsWith("jConnect") ||
240-
jdbcDriverName.startsWith("SQLServer")||
241-
jdbcDriverName.startsWith("Apache Derby")) {
242-
sqlType = Types.VARCHAR;
236+
catch (AbstractMethodError err) {
237+
// JDBC driver not compliant with JDBC 3.0
238+
// -> proceed with database-specific checks
239+
}
240+
if (pmd != null) {
241+
sqlType = pmd.getParameterType(paramIndex);
242+
}
243+
else {
244+
DatabaseMetaData dbmd = ps.getConnection().getMetaData();
245+
String databaseProductName = dbmd.getDatabaseProductName();
246+
String jdbcDriverName = dbmd.getDriverName();
247+
if (databaseProductName.startsWith("Informix") ||
248+
jdbcDriverName.startsWith("Microsoft SQL Server")) {
249+
useSetObject = true;
250+
}
251+
else if (databaseProductName.startsWith("DB2") ||
252+
jdbcDriverName.startsWith("jConnect") ||
253+
jdbcDriverName.startsWith("SQLServer")||
254+
jdbcDriverName.startsWith("Apache Derby")) {
255+
sqlType = Types.VARCHAR;
256+
}
243257
}
244258
}
245259
catch (Throwable ex) {

0 commit comments

Comments
 (0)