-
java.sql.Connection类中有个方法 10
java.sql.Connection类中有个方法
PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
这个方法中参数的第二个作用是什么,代表什么意思?
下面是org.springframework.jdbc.core.PreparedStatementCreatorFactory中的一段代码
[code=java]
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement ps = null;
if (generatedKeysColumnNames != null || returnGeneratedKeys) {
try {
if (generatedKeysColumnNames != null) {
ps = con.prepareStatement(this.actualSql, generatedKeysColumnNames); // TODO
}else {
ps = con.prepareStatement(this.actualSql, PreparedStatement.RETURN_GENERATED_KEYS);
}
}catch (AbstractMethodError ex) {
throw new InvalidDataAccessResourceUsageException(
"The JDBC driver is not compliant to JDBC 3.0 and thus " +
"does not support retrieval of auto-generated keys", ex);
}
}
else if (resultSetType == ResultSet.TYPE_FORWARD_ONLY && !updatableResults) {
ps = con.prepareStatement(this.actualSql);
}
else {
ps = con.prepareStatement(this.actualSql, resultSetType,
updatableResults ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY);
}
setValues(ps);
return ps;
}
呵呵,这个问题困惑我许久了!
问题补充
kimmking 写道finallygo 写道ms在联合主键的时候用
jdbc规范里,insert时,获取自动添加的keys用的。
但是翻了下mysql 5.1.13 驱动的实现
第二个参数传 什么都可以,只要length > 0就可以。
/**
* @see Connection#prepareStatement(String, String[])
*/
public java.sql.PreparedStatement prepareStatement(String sql,
String[] autoGenKeyColNames) throws SQLException {
java.sql.PreparedStatement pStmt = prepareStatement(sql);
((com.mysql.jdbc.PreparedStatement) pStmt)
.setRetrieveGeneratedKeys((autoGenKeyColNames != null)
&& (autoGenKeyColNames.length > 0));
return pStmt;
}
我说为啥,传什么都可以运行的通,呵呵,API说的不是很明白!2011年3月30日 18:25
3个答案按时间排序按投票排序
-
采纳的答案
finallygo 写道ms在联合主键的时候用
jdbc规范里,insert时,获取自动添加的keys用的。
但是翻了下mysql 5.1.13 驱动的实现
第二个参数传 什么都可以,只要length > 0就可以。
/**
* @see Connection#prepareStatement(String, String[])
*/
public java.sql.PreparedStatement prepareStatement(String sql,
String[] autoGenKeyColNames) throws SQLException {
java.sql.PreparedStatement pStmt = prepareStatement(sql);
((com.mysql.jdbc.PreparedStatement) pStmt)
.setRetrieveGeneratedKeys((autoGenKeyColNames != null)
&& (autoGenKeyColNames.length > 0));
return pStmt;
}
2011年3月30日 18:25
-
/**
* Creates a default <code>PreparedStatement</code> object capable
* of returning the auto-generated keys designated by the given array.
* This array contains the names of the columns in the target
* table that contain the auto-generated keys that should be returned.
* This array is ignored if the SQL
* statement is not an <code>INSERT</code> statement.
* <P>
* An SQL statement with or without IN parameters can be
* pre-compiled and stored in a <code>PreparedStatement</code> object. This
* object can then be used to efficiently execute this statement
* multiple times.
* <P>
* <B>Note:</B> This method is optimized for handling
* parametric SQL statements that benefit from precompilation. If
* the driver supports precompilation,
* the method <code>prepareStatement</code> will send
* the statement to the database for precompilation. Some drivers
* may not support precompilation. In this case, the statement may
* not be sent to the database until the <code>PreparedStatement</code>
* object is executed. This has no direct effect on users; however, it does
* affect which methods throw certain SQLExceptions.
* <P>
* Result sets created using the returned <code>PreparedStatement</code>
* object will by default be type <code>TYPE_FORWARD_ONLY</code>
* and have a concurrency level of <code>CONCUR_READ_ONLY</code>.
*
* @param sql an SQL statement that may contain one or more '?' IN
* parameter placeholders
* @param columnNames an array of column names indicating the columns
* that should be returned from the inserted row or rows
* @return a new <code>PreparedStatement</code> object, containing the
* pre-compiled statement, that is capable of returning the
* auto-generated keys designated by the given array of column
* names
* @exception SQLException if a database access error occurs
*
* @since 1.4
*/
PreparedStatement prepareStatement(String sql, String columnNames[])
throws SQLException;