JDBC的基本使用

导包

基础代码

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false";
        String username = "root";
        String password = "root123";
        Connection conn = DriverManager.getConnection(url,username,password);
        String sql = "update users set email = 'hello666' where name = 'zhangsan';";
        Statement stmt = conn.createStatement();
        int count = stmt.executeUpdate(sql);
        System.out.println(count);
        stmt.close();
        conn.close();

jdbc事务

connection

获取执行sql的对象,管理事务

conn.setAutoCommit(false);

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false";
        String username = "root";
        String password = "root123";
        Connection conn = DriverManager.getConnection(url,username,password);
        String sql = "update users set email = '66666' where name = 'zhangsan';";
        String sql2 = "update users set email = '666666' where name = 'wangwu';";
        Statement stmt = conn.createStatement();
        try{
            conn.setAutoCommit(false);
            int count1 = stmt.executeUpdate(sql);
            System.out.println(count1);
            int i = 3/0;
            int count2 = stmt.executeUpdate(sql2);
            System.out.println(count2);
            conn.commit();
        }catch (Exception e){
            conn.rollback();
            System.out.println(e);
        }finally {
            stmt.close();
            conn.close();
        }
    }

statement

执行sql语句

int executeUpdate(sql):执行DML、DDL语句

返回值:DML影响的行数,DDL执行成功后也可能返回0

ResultSet executeQuery(sql):执行DQL语句

返回值:ResultSet结果集对象

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false";
        String username = "root";
        String password = "root123";
        Connection conn = DriverManager.getConnection(url,username,password);
        //String ddl = "drop table  if exists book;";
        String ddl="create table book (\n" +
                "id int primary key auto_increment,\n" +
                "name varchar(20) not null,\n" +
                "price DECIMAL \n" +
                ")";//一个sql只能包含一个动作。如不能把删除表和创建表的动作写在一个sql中。
        Statement stmt = conn.createStatement();
        int count = stmt.executeUpdate(ddl);
        System.out.println(count);
        stmt.close();
        conn.close();
    }

ResultSet

boolean next();将光标向前移一行,判断当前行是否有效行

XXX get数据类型(int 列编号从1开始/String 列名称) 

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false";
        String username = "root";
        String password = "root123";
        Connection conn = DriverManager.getConnection(url,username,password);
        String dql = "select * from users";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(dql);
        while(rs.next()){
            int id = rs.getInt(1);
            String name = rs.getString(2);
            String pwd = rs.getString("password");
            System.out.println("id:"+id+";name:"+name+";pwd:"+pwd);
        }
        stmt.close();
        conn.close();

 PrepareStatement

传统的statement执行sql占位的时候,会出现sql注入问题:

如当密码为   'or'1'='1  的时候就会拼接后变成了select * from users where name = 'zhangsan' and password = ''or'1'='1';

        String inpurUame = "zhangsan";
        String inputPassword ="'or'1'='1";//传统的statement执行sql容易出现sql注入问题,比如:密码为'or'1'='1
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false";
        String username = "root";
        String password = "root123";
        Connection conn = DriverManager.getConnection(url,username,password);
        String sql = "select * from users where name = '"+inpurUame+"' and password = '"+inputPassword+"';";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        if (rs.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        stmt.close();
        conn.close();

PrepareStatement可预编译sql提升性能,还可对敏感字符转义来防止sql注入。

pstmt.setXxx(int index从1开始,Xxx x);

ResultSet rs = pstmt.executeQuery();

        String inpurUame = "zhangsan";
        String inputPassword ="123456";//传统的statement执行sql容易出现sql注入问题,比如:密码为'or'1'='1
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false&useServerPreStmts=true";
        String username = "root";
        String password = "root123";
        Connection conn = DriverManager.getConnection(url,username,password);
        String sql = "select * from users where name = ? and password = ?;";
        System.out.println(sql);
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,inpurUame);
        pstmt.setString(2,inputPassword);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        pstmt.close();
        conn.close();

PrepareStatement开启预编译的方式

先加上url后缀参数useServerPreStmts=true就可以开启了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值