JDBC编程的三个接口

Connection

    Connection接口用于表示应用程序和数据库系统之间的静态连接,提供了针对事务处理的方法以及创建执行sql语句和存储过程的方法,同时提供了一些基本的错误处理方法

  • createStatement():Statement 创建用于执行sql语句的语句对象
  • prepareStatement(String sql):PreparedStatement 创建PreparedStatement对象,用于实现数据库的动态访问
  • prepareCall(String sql):CallableStatement 用于创建执行存储过程的CallableStatement对象
  • close():void结束Connection对象数据库连接,务必注意:数据库连接属于稀有资源,必须保证及时关闭
  • isClose():boolean测试是否已经关闭数据库连接
  • setAutoCommit(boolean):void设置事务是否自动提交
  • commit()在连接上提交事务
  • rollback()回滚撤销事务

Statement接口

Statement接口用于提交执行静态的SQL语句,并返回SQL的执行结果

Statement:用于发送简单的静态SQL语句,不带参数。不支持预编译、有sql注入的风险。

PreparedStatement:Statement的子接口,用于发送含有一个或者多个参数的sql语句。PreparedStatement比Statement执行效率高,因为它支持预编译功能,同时在一定程度上可以防止SQL注意,适合执行连续多次相同结构的SQL语句

CallableStatement:继承于PreparedStatement接口,主要用于调用执行存储过程。

客户端利用jdbc statement的缺点,传入非法参数,从而使jdbc返回不合法的值,通常称为sql注入

ResultSet接口

         ResultSet接口用于得到包含了执行SQL查询结果的结果集,要获取表中任何一个字段项都要先找到该字段项所处于的行,再获取对应的列,获取数据要依靠一个指向当前行的指针,开始时指向满足条件的第一行之前,每次执行next方法,则指针后移,到达目标行后就可以使用getXxx方法获取对应列的值。

boolean next()指针后移,如果有数据则返回true,否则false

getXxx(int索引序号/String列名称):Xxx 获取对应列值

JDBC编程一个简单的查询过程

mysql> select * from tb_student;
+----+------+------------+------+
| id | name | birth      | sex  |
+----+------+------------+------+
|  1 | 张三 | 1999-12-27 |    1 |
|  2 | 李四 | 2000-04-01 |    1 |
|  3 | 王五 | 2001-03-15 |    0 |
|  4 | 赵六 | 1999-05-19 |    1 |
|  5 | 田七 | 2000-07-23 |    0 |
|  6 | 田七 | 1998-12-02 |    0 |
+----+------+------------+------+
6 rows in set (0.01 sec)

public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet res  = null;
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql:///test?serverTimezone=UTC","root","123456");
            stmt=conn.createStatement();
/*            int num=stmt.executeUpdate("insert into tb_student(id,name,birth,sex) values(null,'田七','1998-12-2',0)");
            if (num>0) {
                System.out.println("插入成功");
            }*/
            res=stmt.executeQuery("select  * from tb_student");
            while (res.next()){
                int id=res.getInt("id");
                String name=res.getString("name");
                Date birth=res.getDate("birth");
                Boolean sex = res.getBoolean("sex");
                System.out.println(id+"\t"+name+"\t"+ birth+"\t"+(sex?"男":"女"));
            }
        }catch (Exception e){
           e.printStackTrace();
        }finally {
           try {
               if (res != null) {
                   res.close();
               }
           }catch (Exception e){
               e.printStackTrace();
           }
           try {
               if (stmt != null) {
                   stmt.close();
               }
           }catch (Exception e){
               e.printStackTrace();
           }
           try {
               if (conn != null) {
                   conn.close();
               }
           }catch (Exception e){
               e.printStackTrace();
           }
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值