三.使用PrepareStatement实现CRUD操作

3.1操作和访问数据库

image.png

3.2使用StateMent操作数据表的弊端

sql注入.png

3.3PrepareStatement的使用

  • Statement接口的子接口PrepareStatement:代表一个预编译的sql语句,Connection的PrepareStatement(Stirng sql)返回该类型对象


    image.png
3.3.1介绍
@Test
    public void update() throws IOException, ClassNotFoundException, SQLException, ParseException {
        //向custom表添加一条记录

        InputStream is = PrepareStatementTest.class.getClassLoader().getResourceAsStream("" +
                "jdbc.properties");
        Properties properties = new Properties();
        properties.load(is);
        String driverClass = properties.getProperty("driverClass");
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        Class.forName(driverClass);

        //3.连接,mysql重写的方法
        Connection conn = DriverManager.getConnection(
                url, user, password
        );
//        System.out.println(conn);

        //4.预编译sql语句,返回prepareStatement实例
        String sql = "insert into customers(name," +
                "email,birth)values(?,?,?)";//?占位符
        PreparedStatement ps = conn.prepareStatement(sql);
        //5.填充占位符,1开始
        ps.setString(1,"哪吒");
        ps.setString(2,"nezha@gmail.com");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = sdf.parse("1000-01-01");
        ps.setDate(3,new Date(date.getTime()));

        //6.执行
        ps.execute();

        //7.资源关闭
        ps.close();
        conn.close();
    }
3.3.2封装数据库连接和关闭操作以及增删改
  • 封装数据库连接和关闭集成到工具类

 //1.连接数据库
        Connection conn = util.JDBCUtils.getConnection();

        //2.预编译sql语句,返回prepareStatement实例
        String sql = "insert into customers(name," +
                "email,birth)values(?,?,?)";//?占位符
        PreparedStatement ps = conn.prepareStatement(sql);
        //3.填充占位符,1开始
        ps.setString(1,"哪吒");
        ps.setString(2,"nezha@gmail.com");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date = sdf.parse("1000-01-01");
        ps.setDate(3,new Date(date.getTime()));

        //4.执行
        ps.execute();

        //5.资源关闭
        util.JDBCUtils.closeResource(conn,ps);
  • 改进的更新操作
Connection conn = null;
        PreparedStatement ps = null;

        try {
            //1.连接数据库
            conn = util.JDBCUtils.getConnection();

            //2.预编译sql语句,返回prepareStatement实例
            String sql = "update customers set name = ? " +
                    "where id = ?";//?占位符
            ps = conn.prepareStatement(sql);
            //3.填充占位符,1开始
            ps.setObject(1,"莫扎特");
            ps.setObject(2,18);

            //4.执行
            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //5.资源关闭
            util.JDBCUtils.closeResource(conn,ps);

        }
  • 实现通用的增删改操作
    sql表名最好单引号修饰,避免歧义
insert into 表名(列名,。。。)
values(值,。。)

update 表名
set 列=新值,。。。
where 筛选条件;

语法:delete from 表名 where 条件;
@Test
    public void update3() {
        String sql = "delete from customers where id = ?";
        update(sql, 3);
    }
    public static void update(String sql,Object ...args){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1.连接数据库
            conn = util.JDBCUtils.getConnection();
            //2.预编译sql语句,返回prepareStatement实例
            ps = conn.prepareStatement(sql);
            //3.填充占位符,1开始
            for(int i = 0;i<args.length; i++)
            {
                ps.setObject(i+1, args[i]);
            }
            //4.执行
            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //5.资源关闭
            util.JDBCUtils.closeResource(conn, ps);
        }
    }
image.png
3.3.3实现查询操作
  • 增删改不需要返回,而查询需要
  • 单表查询初版
@Test
    public void test1(){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //1.连接数据库
            conn = util.JDBCUtils.getConnection();
            //2.预编译sql语句,返回prepareStatement实例
            String sql = "select id,name,email,birth " +
                    "from customers where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, 1);
            //3 执行并返回结果集
            resultSet = ps.executeQuery();
            //4 处理结果集
            if(resultSet.next())
            {//判断结果集的下一条是否有数据,如果有返回true,并指针下移
                //返回false,直接结束

                //得到当前数据各自字段的值
                int id  = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String email = resultSet.getString(3);
                java.sql.Date birth = resultSet.getDate(4);

    //            封装为一个对象
                Customer customer = new Customer(id, name, email, birth);
                System.out.println(customer);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            util.JDBCUtils.closeResource(conn, ps, resultSet);
        }
    }
  • 单表通用查询
@Test
    public void test2(){
        String sql = "select id,name,email " +
                "from customers where id = ?";
        System.out.println(queryCustomers(sql, 13));
    }
//单表通用查询
    public static Customer queryCustomers(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            rs = ps.executeQuery();
            //获取rs元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            //获取结果集列数
            int col = rsmd.getColumnCount();
            if (rs.next()) {
                Customer cust = new Customer();
                //对每一列赋值给对象对应的成员属性
                for (int i = 0; i < col; i++) {
                    Object value = rs.getObject(i + 1);
                    //获取每个列的别名+反射
                    String columnName = rsmd.getColumnLabel(i + 1);
                    Field field = Customer.class.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(cust, value);
                }
                return cust;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, ps, rs);

        }
        return null;
    }
  • 创建表对应的类时候,属性名要和字段名完全相同,否则反射设置不成功,所以sql字段名要用类的属性名起别名,而且需要用rsmd的getColumnLabel()获取列的别名
  • 针对不同表的通用查询
@Test
    public void test2(){
        String sql = "select id,name,email " +
                "from customers where id = ? or id = ?";
        System.out.println(getInstance(Customer.class, sql, 13, 1).toString());
    }
//针对任意表的通用查询
    public static <T> List<T> getInstance(Class<T> clazz, String sql, Object... args){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<T> list = null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            //得到结果集
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int col = rsmd.getColumnCount();
            list = new ArrayList<>();
            while(rs.next()){
                //对每一行
                T t = clazz.newInstance();
                for(int i = 0; i < args.length; i++){
                    Object value = rs.getObject(i + 1);
                    String fieldName = rsmd.getColumnLabel(i + 1);
                    Field field = clazz.getDeclaredField(fieldName);
                    field.setAccessible(true);
                    field.set(t, value);
                }
                list.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, ps, rs);
        }

        return list;

    }
  • 注意泛型方法的格式 返回值前<E, .....>

3.4ResultSet与ResultSetMetaData

image.png

image.png

image.png
  • 结论:
    用ResultSet得到各行和每行各列的值
    用ResultSetMataData得到列数和每列对应的成员变量名,用反射设置对象对应Field

3.5资源释放

image.png

image.png

3.7 PrepareStatement解决sql注入问题

  • 原理:创建对象时候已经预编译了,sql逻辑关系不会改变
  • 好处:
    1)可以操作blob类型数据
    2)实现更高效的批量操作
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357