PreparedStatement数据操作接口
利用PreparedStatement接口是可以解决Statement接口所存在的问题的,但是如果要想使用PreparedStatement之前首先需要观察一下其继承的结构以及相关的对象实例化的处理操作。
public interface PreparedStatementextends Statement
通过继承结构可以发现PreparedStatement属于Staterment 子接口,而要想获取PreparedStatement子接口的对象实例,则依然需要通过Connection接口所提供的方法来完成:
PreparedStatement prepareStatement(String sql) throws SQLException(少一个d)
通过此方法的对比可以发现与创建Statement接口对象实例的方法不同,在创建PreparedStatement接口实例的时候需要明确的传入一个SQL,因为这个SQL需要进行预处理,所谓的预处理就是指针对于要执行的sql语句的格式进行解析执行,但是对于要处理的数据还不能够操作,在获取了PreparedStatement接口对象实力之后再考虑获取相关的数据内容。
PreparedStatement有如下常用方法:
NO. | 方法 | 类型 | 描述 |
01 | void set数据类型(int parameterIndex, 数据类型 x) throws SQLException | 普通 | 明确的在某一个指定的占位符索引上设置指定的数据内容 |
02 | void setObject(int parameterIndex, Object x) throws SQLException | 普通 | 直接在指定指定的占位符索引上设置数据内容 |
03 | int executeUpdate() throws SQLException | 普通 | 执行更新操作,返回数据行数。 |
04 | ResultSet executeQuery() throws SQLException | 普通 | 执行数据的查询操作 |
PreparedStatement 接口中所有要执行的SQL的语句内容实际上已经全部都在创建PreparedStatement接口的时候就已经定义完成了,而之所获得PreparedStatement接口对象实例,主要就是为了设置占位符的数据。