数据库程序接口——JDBC——API解读第二篇——执行SQL的核心对象

核心对象                                                            

Statement

Statement主要用来执行SQL语句。它执行SQL语句的步骤为:

  1. 第一步:创建statement对象。
  2. 第二步:配置statement对象,此步骤可以忽略。
  3. 第三步:调用statement的executeXXX方法执行SQL语句。
  4. 第四步:处理结果集。示例中只是打印结果集。
  5. 第五步:关闭statement对象。

Statement执行SQL语句的方法有四种类型。

  1. execute执行返回结果包含一个或者多个结果集,或结果包含一个或多个更新记录数量的SQL语句。返回类型为布尔,当执行SQL语句返回结果为ResultSet对象时为true,其他情形为false。
  2. executeQuery执行返回结果为ResultSet对象的SQL语句,例如select语句。返回类型为ResultSet对象。
  3. executeUpdate执行返回结果为更新记录数量的SQL语句,例如update,Insert,delete语句。它也可以用来DDL语句。返回值类型为int。当执行update,insert,delete语句时,int值表示更新记录数量,其他情形返回0。
  4. executeLargeUpdate方法与executeUpdate方法类似。适用于更新数量较多的情况。返回值类型为Long

创建对象

     参考Connection对象

get&set

 1 /**----------Statement对象相关-----------**/
 2 // 设置支持Pooled
 3 statement.setPoolable(true);
 4 System.out.println("statement语句的是否支持Pooled:" + statement.isPoolable());
 5 System.out.println("statement语句是否已经关闭:" + statement.isClosed());
 6 /**----------SQL执行过程----------**/
 7 // 建议结果集的排序方式,类型有三种,FETCH_FORWARD,FETCH_REVERSE,FETCH_UNKONWN
 8 statement.setFetchDirection(ResultSet.FETCH_REVERSE);
 9 // 建议每一次接收结果集的条数
10 statement.setFetchSize(10);
11 // 设置ResultSet最大的条数
12 statement.setMaxRows(10);
13 // 设置字段内容最大的字节数
14 statement.setMaxFieldSize(100);
15 // 设置执行sql语句的超时时间
16 statement.setQueryTimeout(1);
17 // 设置是否执行Escape类型的SQL语法。
18 statement.setEscapeProcessing(false);
19 /**-----------结果集相关信息------------**/
20 System.out.println("statement语句的FetchDirection为:" + statement.getFetchDirection());
21 System.out.println("statement语句的FetchSize为:" + statement.getFetchSize());
22 System.out.println("statement语句的maxFieldSize:" + statement.getMaxFieldSize());
23 System.out.println("statement语句的maxRows为:" + statement.getMaxRows());
24 System.out.println("statement语句的queryTimeOut为:" + statement.getQueryTimeout());
25 System.out.println("statement语句的结果集更新方式:" + statement.getResultSetConcurrency());
26 System.out.println("statement语句的结果集关闭方式:" + statement.getResultSetHoldability());
27 System.out.println("statement语句的结果集的遍历方式:" + statement.getResultSetType());
28 System.out.println("statement语句的LargeMaxRows为:" + statement.getLargeMaxRows());

 

executeSQL

  1. execute(sql):参数sql表示一条sql语句。例如select 1 from dual;select 2 from dual;它包含两条sql语句。这种情形是不允许的
  2. execute(sql,autoGeneratedKeys):参数autoGneratedKeys,TODO
  3. execute(sql,columnIndexs):参数columnIndexs表示列的索引值,从1开始。类型为int数组
  4. execute(sql,columnNames):参数columnNames表示列名称,类型为String数组。

executeUpdate,executeQuery,executeLargeUpdate拥有和execute方法相同的参数和重载方法

批量

  1. addBatch(sql):在批量执行的SQL语句中添加sql语句。参数sql表示一条sql语句。sql语句的类型可以是delete,update,insert的任意一种。执行每条SQL返回值类型为int。如果批量执行其他类型的SQL语句,可以使用存储过程。
  2. executeBatch:批量执行SQL,返回值为int数组。数组的每个值表示每条SQL语句更新记录数量的值
  3. executeLargeBatch:含义与executeBatch相同。返回值类型为Long数组。
  4. clearBatch:清空批量执行的SQL。
  5. cancel:取消正在执行的SQL语句。

示例如下:

 1 /**
 2  * 演示Statement对象的方法。 本示例中使用到的数据库表为student,该表字段为名称,学号,班级,主修课,
 3  * 本示例中创建Statement调用的方法参考Connection,DriverManager,Driver等对象。
 4  * 
 5  */
 6 public class StatementSample {
 7     public static void main(String[] args) throws SQLException {
 8         // 创建Statement对象
 9         Statement statement = createStatement();
10         // 使用execute方法执行sql语句
11         executeSQL(statement);
12         // 使用executeQuery方法执行sql语句
13         executeQuerySQL(statement);
14         // 使用executeUpdate方法执行sql语句
15         executeUpdateSQL(statement);
16     }
17 
18     /**
19      * 创建Statement对象
20      * 
21      * @return Statement对象
22      * @throws SQLException
23      */
24     public static Statement createStatement() throws SQLException {
25         // 创建Connection对象
26         Connection conn = DataSourceUtil.getConnectionByDriver();
27         // 创建Statement对象
28         Statement statement = conn.createStatement();
29         return statement;
30     }
31 
32     /**
33      * 调用Statement对象的execute方法,示例为:select 1 from dual 测试语句
34      * 
35      * @return ResultSet 结果集
36      * @throws SQLException
37      */
38     public static boolean executeSQL(Statement statement) throws SQLException {
39         // 测试的SQL语句
40         String testSQL = "select 1 from dual";
41         // 执行SQL语句,如果执行返回结果为ResultSet对象则为true,其他为false。
42         boolean isSuccess = statement.execute(testSQL);
43         System.out.println("执行 \t" + testSQL + " 的结果为:" + isSuccess);
44         return isSuccess;
45     }
46 
47     /**
48      * 调用statement对象的executeQuery方法执行SQL语句,返回类型为ResultSet
49      * 
50      * @param statement
51      * @return ResultSet对象,执行SQL语句返回的结果集
52      * @throws SQLException
53      */
54     public static ResultSet executeQuerySQL(Statement statement) throws SQLException {
55         // SQL语句
56         String querySql = "select '#'|| name ,stu_num , stu_class, stu_major from student";
57         // DDL语句
58         String ddlSql = "alter table student add birth date";
59         // 结果集
60         ResultSet rs = statement.executeQuery(querySql);
61         System.out.println("执行\t" + querySql + " 的结果为:");
62         ResultUtil.printResultSet(rs);
63         return rs;
64     }
65 
66 
67     /**
68      * 调用statement对象的executeUpdate方法。示例:更新表
69      * 
70      * @param statement
71      * @return
72      * @throws SQLException
73      */
74     public static int executeUpdateSQL(Statement statement) throws SQLException {
75         // 更新语句
76         String sql = "update student set birth = null";
77         // 更新记录数量
78         int updateRowNum = statement.executeUpdate(sql);
79         System.out.println("执行\t" + sql + ",更新行数为:" + updateRowNum);
80         return updateRowNum;
81     }
82 
83     /**
84      * 调用statement对象的executeBatch方法。示例为:批量插入表
85      * 
86      * @param statement
87      * @return
88      * @throws SQLException
89      */
90     public static int[] executeBatchSQL(Statement statement) throws SQLException {
91         for (int i = 101; i < 200; i++) {
92             String batchSql = "insert into student values (" + "'rain" + i + "'," + i + ","
93                     + "'287班" + "'," + "'math" + "'," + "null)";
94             statement.addBatch(batchSql);
95         }
96         int[] count = statement.executeBatch();
97         System.out.println(Arrays.toString(count));
98         return count;
99     }
View Code

相关文章:

  • 2022-12-23
  • 2021-04-27
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2021-04-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案