JDBC初识

JDBC

  1. JDBC介绍

    数据库驱动(Java语言对应的是一个jar包)

  2. Jdbc使用步骤

    1. 导包

    2. 注册驱动 (仅仅做一次)

    3. 建立连接(Connection)

    4. 创建运行SQL的语句对象

    5. 运行语句

    6. 处理运行结果(ResultSet)

    7. 释放资源

      其中:如果是添加、删除、更新操作可以没有处理运行结果这一步,查询可定会有这一步

      1. 注册驱动、建立连接

                String sql = "select * from student";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                ResultSet resultSet = preparedStatement.executeQuery();

      2. 创建运行SQL的语句

                String sql = "select * from student";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                ResultSet resultSet = preparedStatement.executeQuery();

      3. 运行语句、处理运行结果

                while (resultSet.next()) {
                    System.out.println(resultSet.getString("name"));
                }

      4. 释放资源

            public static void closeConnection(AutoCloseable autoCloseable){
                try {
                    if (autoCloseable != null){
                        autoCloseable.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

  3. PreparedStatement

    Statement 和 PreparedStatement 的区别:

    1. Statement用于执行静态SQL语句

    2. PreparedStatement 是预编译的SQL语句对象并保存在对象中

    3. 每次执行同一个PreparedStatement对象时,它就会被解析一次,但不会被再次编译,可以重复使用

    4. 能够避免SQL注入,相对安全

  4. SQL注入

    1. 通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令

  5. 封装工具类

    public class JdbcUtils {
        public static Connection getConnection(){
            try {
    //            1.注册驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
    //            2.建立连接
                Properties properties = new Properties();
                properties.load(JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
                String url = properties.getProperty("url");
                String username = properties.getProperty("username");
                String password = properties.getProperty("password");
                return DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    ​
        public static void close(AutoCloseable autoCloseable){
            try {
                if (autoCloseable != null) {
                    autoCloseable.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

  6. 事务(面试题)

    四大特性

    1. 原子性:一个事务是一个不可分割的工作单位,事务中包括的操作要么都做,要么都不做

    2. 一致性:事务必须是使数据库从一个一致性状态变成另一个一致性状态,一致性与原子性是密切相关的

    3. 隔离性:一个事务的执行不能被其他事务干扰

    4. 持久性:持久性也称永久性,指的是一个事务一旦修改,它对数据库的改变就应该是永久性的

  7. 连接池

    1. 数据库连接池负责分配、管理和释放数据库俩姐,允许重复使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值