JDBC parameter binding uses JDBC 3.0 ParameterMetaData (if available) for type determination

This commit is contained in:
Juergen Hoeller
2012-03-14 16:07:39 +01:00
parent 9d3cfcd8e6
commit 79d9f7a5f7
2 changed files with 31 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -235,10 +235,10 @@ public class PreparedStatementCreatorFactory {
ps = con.prepareStatement(this.actualSql, PreparedStatement.RETURN_GENERATED_KEYS);
}
}
catch (AbstractMethodError ex) {
catch (AbstractMethodError err) {
throw new InvalidDataAccessResourceUsageException(
"The JDBC driver is not compliant to JDBC 3.0 and thus " +
"does not support retrieval of auto-generated keys", ex);
"Your JDBC driver is not compliant with JDBC 3.0 - " +
"it does not support retrieval of auto-generated keys", err);
}
}
else if (resultSetType == ResultSet.TYPE_FORWARD_ONLY && !updatableResults) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.DatabaseMetaData;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
@@ -189,7 +190,7 @@ public abstract class StatementCreatorUtils {
if (inValue instanceof SqlParameterValue) {
SqlParameterValue parameterValue = (SqlParameterValue) inValue;
if (logger.isDebugEnabled()) {
logger.debug("Overriding typeinfo with runtime info from SqlParameterValue: column index " + paramIndex +
logger.debug("Overriding type info with runtime info from SqlParameterValue: column index " + paramIndex +
", SQL type " + parameterValue.getSqlType() +
", Type name " + parameterValue.getTypeName());
}
@@ -228,18 +229,31 @@ public abstract class StatementCreatorUtils {
boolean useSetObject = false;
sqlType = Types.NULL;
try {
DatabaseMetaData dbmd = ps.getConnection().getMetaData();
String databaseProductName = dbmd.getDatabaseProductName();
String jdbcDriverName = dbmd.getDriverName();
if (databaseProductName.startsWith("Informix") ||
jdbcDriverName.startsWith("Microsoft SQL Server")) {
useSetObject = true;
ParameterMetaData pmd = null;
try {
pmd = ps.getParameterMetaData();
}
else if (databaseProductName.startsWith("DB2") ||
jdbcDriverName.startsWith("jConnect") ||
jdbcDriverName.startsWith("SQLServer")||
jdbcDriverName.startsWith("Apache Derby")) {
sqlType = Types.VARCHAR;
catch (AbstractMethodError err) {
// JDBC driver not compliant with JDBC 3.0
// -> proceed with database-specific checks
}
if (pmd != null) {
sqlType = pmd.getParameterType(paramIndex);
}
else {
DatabaseMetaData dbmd = ps.getConnection().getMetaData();
String databaseProductName = dbmd.getDatabaseProductName();
String jdbcDriverName = dbmd.getDriverName();
if (databaseProductName.startsWith("Informix") ||
jdbcDriverName.startsWith("Microsoft SQL Server")) {
useSetObject = true;
}
else if (databaseProductName.startsWith("DB2") ||
jdbcDriverName.startsWith("jConnect") ||
jdbcDriverName.startsWith("SQLServer")||
jdbcDriverName.startsWith("Apache Derby")) {
sqlType = Types.VARCHAR;
}
}
}
catch (Throwable ex) {