java.sql.Connection类中有个方法

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个答案按时间排序按投票排序

0 0

采纳的答案

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
0 0

ms在联合主键的时候用

2011年3月30日 18:25
0 0

/** 
     * 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; 

package com.hexiang.utils.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.log4j.Logger; public class DBConnection { /** * 获得与数据库的连接 * * @param path * @return Connection */ public static Connection getConn(String classDriver, String url, String user, String pwd) { try { Class.forName(classDriver); return DriverManager.getConnection(url, user, pwd); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(DataSource dataSource) { try { return dataSource.getConnection(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(String jndiName) { try { Context ctx; ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + jndiName); return dataSource.getConnection(); } catch (NamingException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(Properties properties) { try { String driver = properties.getProperty("jdbc.driverClassName"); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); Class.forName(driver); return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } /** * oracle连接 * * @param path * @return Connection */ public static Connection getOracleConn(String
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值