八、JDBC——2-执行SQL语句的方式

2-执行SQL语句的方式

1.使用Statement执行SQL语句

(1)executeQuery(String sql):

  该方法只能执行查询语句,返回一个ResultSet对象;

(2)executeUpdate(String sql)

  该方法可以执行DML语句,并返回受影响的行数;

(3)execute(String sql)

  该方法可以执行任何SQL语句,如果执行后的第一个结果为ResultSet对象,则返回true;如果执行后的第一个结果为受影响的行数或没有结果,则返回false;使用execute(String sql)方法执行SQL语句的返回值只是boolean类型,当需要使用获得执行SQL语句得到的ResultSet时,需要使用Statement提供的如下两个方法:

  getResultSet():获取该Statement执行SQL语句后得到的ResultSet;

  getUpdateCount():获取该Statement执行DML语句所影响的记录行数;

  使用execute()执行各种SQL语句:

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.ResultSet;
 4 import java.sql.Statement;
 5 
 6 public class ExecuteTest {
 7     public static void main(String[] args) throws Exception {
 8         //加载数据库驱动
 9         Class.forName("com.mysql.jdbc.Driver");
10         //使用DriverManager获取Connection
11         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test",
12                 "root",
13                 "stx12345");
14         //通过Connection获取Statement对象
15         Statement stat = conn.createStatement();
16         //使用Statement的execute()方法执行DDL语句:
17         stat.execute("create table test2("
18                 + "id int primary key auto_increment,"
19                 + "name varchar(255) not null,"
20                 + "age int not null"
21                 + ")");
22         //使用Statement的execute()方法执行DML语句:
23         stat.execute("insert into test2 (name,age) values('testName1',18)");
24         stat.execute("insert into test2 (name,age) values('testName2',19)");
25         stat.execute("insert into test2 (name,age) values('testName3',20)");
26         //使用Statement的execute()方法执行查询语句
27         stat.execute("select * from test2");
28         //获取结果集
29         ResultSet rs = stat.getResultSet();
30         while(rs.next()){
31             System.out.println(rs.getInt(1)+"|"+rs.getString(2)+"|"+rs.getInt(3));
32         }
33     }
34 }

2.使用PreparedStatement执行SQL语句

(1)PreparedStatement是Statement的子接口,除了可以实现与Statement相同的作用以外,还可以实现预编译SQL语句,预编译后的SQL语句被存储在PreparedStatement对象中,然后可以使用该对象多次执行预编译的SQL语句;使用PreparedStatement的效率要比Statement的效率高;

(2)使用PreparedStatement的示例:

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.PreparedStatement;
 4 
 5 public class PreparedStatementTest {
 6     public static void main(String[] args) throws Exception {
 7         //加载数据库驱动
 8         Class.forName("com.mysql.jdbc.Driver");
 9         //通过DriverManager获取Connection
10         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test",
11                 "root",
12                 "stx12345");
13         //通过Connection获取PreparedStatement对象
14         //预编译一条SQL语句
15         //所有的SQL都可以在PreparedStatement中预编译
16         PreparedStatement prep = conn.prepareStatement("insert into test2 (name,age) values (?,?)");
17         //使用PreparedStatement插入记录
18         prep.setString(1, "test3");
19         prep.setInt(2, 21);
20         prep.execute();
21         //再插入一条记录
22         prep.setString(1, "test4");
23         prep.setInt(2, 25);
24         prep.execute();
25         
26     }
27 }

3.使用CallableStatement调用存储过程

(1)在mysql中创建一个存储过程:

  delimiter //

  create procedure add_pro(a int,b int,out sum int)

  begin

  set sum = a + b;

  end;

  //

(2)调用存储过程示例:

 1 import java.sql.CallableStatement;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.Types;
 5 
 6 
 7 public class CallableStatementTest {
 8     public static void main(String[] args)throws Exception{
 9         //加载数据库驱动
10         Class.forName("com.mysql.jdbc.Driver");
11         //通过DriverManager获取Connection
12         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test",
13                     "root",
14                     "stx12345");
15         
16         //使用Connection创建一个CallableStatement对象
17         CallableStatement call = conn.prepareCall("{call add_pro(?,?,?)}");
18         
19         //传入参数
20         call.setInt(1, 5);
21         call.setInt(2, 8);
22         
23         //注册CallableStatement的第三个参数是Int类型
24         call.registerOutParameter(3, Types.INTEGER);
25         //执行存储过程
26         call.execute();
27         //获取并输出存储过程传出参数的值
28         System.out.println("执行结果是:"+call.getInt(3));
29     }
30 }

 

  

posted @ 2017-08-02 20:22  丶theDawn  阅读(413)  评论(0编辑  收藏  举报