| 方法名称 | 功能描述 |
| — | — |
| getMetaData() | 该方法用于返回数据库的元数据的 DatabaseMetaData 对象 |
| createStatement | 用于创建一个 Statement 对象来将 SQL 语句发送到数据库 |
| preparedStatement(String sql) | 用于创建一个 PreparedStatement 对象来将参数化的SQL语句发送到数据库 |
| prepareCall(String sql) | 用于创建一个 CallableStatement 对象来调用数据库存储过程 |
2、Statement 接口
| 方法名称 | 功能描述 |
| — | — |
| boolean execute(String sql) | 用于执行各种 SQL 语句,该方法返回一个 boolean 类型的值。如果为 true,表示所执行的 SQL 语句具备查询结果,可通过 Statement 的getResultSet() 方法查询结果 |
| int executeUpdate(String sql) | 用于执行 SQL 中的 insert、update 和 delete 语句,该方法返回一个 int 类型的值,表示影响数据库中的行数 |
| ResultSet executeQuery(String sql) | 用于执行 SQL 中的 select 语句(查询,遍历),该方法返回一个表示查询结果的 ResultSet 对象 |
execute是executeQuery和executeUpdate的综合.
通常我们没有必要使用execute方法来执行SQL语句,而是使用 executeQuery 或 executeUpdate 更适合。
- execute、executeUpdate、executeQuery三者的区别:
executeUpdate() 这是 PreparedStatement 接口中的方法
executeUpdate(String sql) 这是 PreparedStatement 从父接口 Statement 中继承过来的方法
executeUpdate() 中执行 SQL 语句需要在创建 PerparedStatement 时通过 Connection 的
prepareStatement(String sql) 方法中写出,因为 PerparedStatement 中的 SQL 语句数据库需要进行预编译和缓存,因此要在创建 PerparedStatement 对象时给出 SQL 语句。
而 executeUpdate(String sql) 是 Statement 中的方法,参数中的 SQL 语句只是提交给数据库去执行,并不需要预编译。
3、PreparedStatement 接口
| 方法名称 | 功能描述 |
| — | — |
| executeUpdate() | 在此 PreparedStatement 对象中执行 SQL 语句,该语句必须是一个 DML 语句,或者无返回内容的 SQL 语句,比如 DDL 语句 |
| executeQuery() | 在此 PreparedStatement 对象中执行 SQL 语句,该方法返回的是 ResultSet 对象 |
| setInt(int parameterIndex, int x) | 将指定的参数设置为 int 值 |
| setFloat(int parameterIndex, float x) | 将指定的参数设置为 Float 值 |
| setString(int parameterIndex, String x) | 将指定参数设置的给定的 Date 值 |
| setDate(int parameterIndex, Date x) | 将指定参数设置给定的 Date 值 |
| addBatch() | 将一组参数添加到此 PreparedStatement 对象的批处理命令中 |
| setCharacterStream(parameterIndex, reader, length) | 将指定的输入流写入数据库的文本字段 |
| setBinaryStream(parameterIndex, x, length) | 将二进制的输入流数据写入到二进制的字段中 |
DML 语句:SELECT、UPDATE、INSERT、DELETE
DLL 语句:CREATE DROP ALERT
具体参考: SQL的单表查询
4、ResultSet 接口
| 方法名称 | 功能描述 |
| — | — |
| getString(int columnIndex) | 用于获取 指定字段的 String 类型的值,参数 columnIndex 代表字段的索引 |
| getString(String columnName) | 用于获取指定字段的 String 类型的值,参数 columnIndex 代表字段名称 |
| getInt(int columnIndex) | 用于获取指定字段的 int 类型的值,参数 columnIndex 代表字段的索引 |
| getInt(String columnName) | 用于获取指定字段的 int 类型的值,参数 columnIndex 代表字段名称 |
| getDate(int columnIndex) | 用于获取指定字段的 Date类型的值,参数 columnIndex 代表字段索引 |
| getDate(String columnName) | 用于获取指定字段的 Date类型的值,参数 columnIndex 代表字段名称 |
| next() | 将游标从当前位置移到下一位置 |
| absolute(int row) | 将游标移到此 ResultSet 对象的指定行 |<