`
patrick212
  • 浏览: 4697 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

PreparedStatement prepareStatement()

阅读更多
传送给数据库的 SQL 语句通过一个包含两个步骤的过程来返回结果。首先准备它们,然后处理它们。借助 Statement 对象,这两个阶段对应用程序而言变成一个阶段。

PreparedStatement 允许将这两个步骤分开。准备步骤在创建对象时发生,而处理步骤在对 PreparedStatement 对象调用 executeQuery、executeUpdate 或 execute 方法时发生。

prepareStatement
PreparedStatement prepareStatement(String sql,
                                   int autoGeneratedKeys)
                                   throws SQLException
Creates a default PreparedStatement object that has the capability to retrieve auto-generated keys. The given constant tells the driver whether it should make auto-generated keys available for retrieval. This parameter is ignored if the SQL statement is not an INSERT statement, or an SQL statement able to return auto-generated keys (the list of such statements is vendor-specific).
Note: This method is optimized for handling parametric SQL statements that benefit from precompilation. If the driver supports precompilation, the method prepareStatement 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 PreparedStatement object is executed. This has no direct effect on users; however, it does affect which methods throw certain SQLExceptions.

Result sets created using the returned PreparedStatement object will by default be type TYPE_FORWARD_ONLY and have a concurrency level of CONCUR_READ_ONLY. The holdability of the created result sets can be determined by calling getHoldability().


Parameters:
sql - an SQL statement that may contain one or more '?' IN parameter placeholders
autoGeneratedKeys - a flag indicating whether auto-generated keys should be returned; one of Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS
Returns:
a new PreparedStatement object, containing the pre-compiled SQL statement, that will have the capability of returning auto-generated keys
Throws:
SQLException - if a database access error occurs, this method is called on a closed connection or the given parameter is not a Statement constant indicating whether auto-generated keys should be returned
SQLFeatureNotSupportedException - if the JDBC driver does not support this method with a constant of Statement.RETURN_GENERATED_KEYS
Since:
1.4

----------------------------------------------------------------------

prepareStatement
PreparedStatement prepareStatement(String sql,
                                   int[] columnIndexes)
                                   throws SQLException
Creates a default PreparedStatement object capable of returning the auto-generated keys designated by the given array. This array contains the indexes of the columns in the target table that contain the auto-generated keys that should be made available. The driver will ignore the array if the SQL statement is not an INSERT statement, or an SQL statement able to return auto-generated keys (the list of such statements is vendor-specific).
An SQL statement with or without IN parameters can be pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

Note: This method is optimized for handling parametric SQL statements that benefit from precompilation. If the driver supports precompilation, the method prepareStatement 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 PreparedStatement object is executed. This has no direct effect on users; however, it does affect which methods throw certain SQLExceptions.

Result sets created using the returned PreparedStatement object will by default be type TYPE_FORWARD_ONLY and have a concurrency level of CONCUR_READ_ONLY. The holdability of the created result sets can be determined by calling getHoldability().


Parameters:
sql - an SQL statement that may contain one or more '?' IN parameter placeholders
columnIndexes - an array of column indexes indicating the columns that should be returned from the inserted row or rows
Returns:
a new PreparedStatement object, containing the pre-compiled statement, that is capable of returning the auto-generated keys designated by the given array of column indexes
Throws:
SQLException - if a database access error occurs or this method is called on a closed connection
SQLFeatureNotSupportedException - if the JDBC driver does not support this method
Since:
1.4

----------------------------------------------------------------------

prepareStatement
PreparedStatement prepareStatement(String sql,
                                   String[] columnNames)
                                   throws SQLException
Creates a default PreparedStatement 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. The driver will ignore the array if the SQL statement is not an INSERT statement, or an SQL statement able to return auto-generated keys (the list of such statements is vendor-specific).
An SQL statement with or without IN parameters can be pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

Note: This method is optimized for handling parametric SQL statements that benefit from precompilation. If the driver supports precompilation, the method prepareStatement 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 PreparedStatement object is executed. This has no direct effect on users; however, it does affect which methods throw certain SQLExceptions.

Result sets created using the returned PreparedStatement object will by default be type TYPE_FORWARD_ONLY and have a concurrency level of CONCUR_READ_ONLY. The holdability of the created result sets can be determined by calling getHoldability().


Parameters:
sql - an SQL statement that may contain one or more '?' IN parameter placeholders
columnNames - an array of column names indicating the columns that should be returned from the inserted row or rows
Returns:
a new PreparedStatement object, containing the pre-compiled statement, that is capable of returning the auto-generated keys designated by the given array of column names
Throws:
SQLException - if a database access error occurs or this method is called on a closed connection
SQLFeatureNotSupportedException - if the JDBC driver does not support this method
Since:
1.4


分享到:
评论

相关推荐

    prepareStatement和Statement的区别

    首先,从创建时的区别开始,Statement 需要通过 Connection 对象的 createStatement() 方法创建,而 PreparedStatement 需要通过 Connection 对象的 prepareStatement() 方法创建,并且需要带有 SQL 语句。...

    java中PreparedStatement和Statement的区别

    perstmt = con.prepareStatement("insert into tb_name (col1,col2,col2,col4) values (?,?,?,?)"); perstmt.setString(1,var1); perstmt.setString(2,var2); perstmt.setString(3,var3); perstmt.setString(4,var4)...

    JDBC PrepareStatement 使用(附各种场景 demo)

    PrepareStatement是JDBC提供的一种预编译的SQL语句,它可以提高数据库操作的效率和安全性。本资源主要涵盖了使用JDBC PrepareStatement进行MySQL数据库操作的各种场景,包括基本的查询、更新以及批量处理。 首先,...

    JDBC(用PrepareStatement实现)

    创建PreparedStatement对象需要通过Connection接口的prepareStatement()方法,传入SQL语句作为参数。例如: ```java Connection conn = DriverManager.getConnection(url, username, password); ...

    java业务层框架开发ibatis(java源码)

    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public CallableStatement prepareCall...

    java数据库连接PrepareStatement

    要创建一个 `PreparedStatement` 对象,首先需要通过 `Connection` 对象调用 `prepareStatement` 方法,并传入一个 SQL 语句字符串。例如: ```java // 假设 con 是已建立的 Connection 对象 String sql = "UPDATE ...

    JDBC学习文档

    5. **创建带有类型、并发性和保持性的PreparedStatement**:`PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)` - **用途**:创建一个带...

    抽象工厂之数据库转换例子

    PreparedStatement prepareStatement(Connection conn, String sql); } ``` 然后,针对MySQL和Oracle数据库,实现对应的工厂类: ```java public class MysqlFactory implements DatabaseFactory { @Override ...

    利用JDBC的PrepareStatement打印真实SQL的方法详解

    PreparedStatement ps = connection.prepareStatement(sql); ps.setInt(1, 10); ``` 在这个例子中,我们想知道实际执行的SQL语句是"SELECT * FROM table WHERE id = 10"。为了实现这个需求,我们可以自定义一个辅助...

    PreparedStatement和Statement

    PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "John Doe"); pstmt.setInt(2, 30); pstmt.executeUpdate(); ``` 3. **比较与选择** - **性能**:由于预编译,`...

    关于PreparedStatement插入Date类型值的方法.txt

    PreparedStatement pstmt = connection.prepareStatement("INSERT INTO my_table (date_column) VALUES (?)"); pstmt.setDate(1, new java.sql.Date(System.currentTimeMillis())); // 使用当前时间创建Date对象 ...

    常用的JDBC类与方法

    * PreparedStatement prepareStatement(String sql) throws SQLException:建立 PreparedStatement 类对象 * boolean getAutoCommit() throws SQLException:返回 Connection 类对象的AutoCommit 状态 * void ...

    java连接oracle数据库———sqlHelper类

    PreparedStatement pstmt = helper.prepareStatement("SELECT * FROM table_name WHERE condition"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { // 处理结果集 } rs.close(); pstmt.close(); ...

    利用POI解析excel并存入数据库demo

    private PreparedStatement prepareStatement(Connection conn) { // 创建并设置 SQL 语句,例如:INSERT INTO table (col1, col2, ...) VALUES (?, ?, ...) // ... } private void insertIntoDatabase...

    使用JAVA中的动态代理实现数据库连接池

    public PreparedStatement prepareStatement(String sql) throws SQLException { return realConn.prepareStatement(sql); } } ``` 3. **创建代理类**:使用动态代理技术创建一个代理类,该类负责管理连接的...

    06丨数据库原理:为什么PrepareStatement性能更好更安全?.pdf

    【数据库原理:为什么PrepareStatement性能更好更安全】 在数据库编程中,我们经常遇到两种执行SQL语句的方法:Statement和PreparedStatement。尽管Statement看起来更简洁,但在实际应用中,尤其是在使用ORM框架如...

    JDBC访问数据库步骤详解.doc

    - `PreparedStatement prepareStatement(String sql)`:创建一个预编译的`PreparedStatement`对象,用于执行含有参数的SQL语句。 #### 四、操作ResultSet结果集 通过`Statement`对象的`executeQuery(String sql)`...

    第7章-JSP中使用数据库.ppt

    - `public PreparedStatement prepareStatement(String sql)`:创建一个用于执行预编译SQL语句的PreparedStatement对象。 - `public CallableStatement prepareCall(String sql)`:创建一个用于执行存储过程的...

Global site tag (gtag.js) - Google Analytics