重拾JDBC(二)通过Statement执行更新操作

通过Statement执行更新操作

    /**
     * 通过 JDBC 向指定的数据表中插入一条记录. 
     * 
     * 1. Statement: 用于执行 SQL 语句的对象
     * 1). 通过 Connection 的 createStatement() 方法来获取
     * 2). 通过 executeUpdate(sql) 可以执行 SQL 语句.
     * 3). 传入的 SQL 可以是 INSRET, UPDATE 或 DELETE. 但不能是 SELECT
     * 
     * 2. Connection、Statement 都是应用程序和数据库服务器的连接资源. 使用后一定要关闭.
     * 需要在 finally 中关闭 Connection 和 Statement 对象. 
     * 
     * 3. 关闭的顺序是: 先关闭后获取的. 即先关闭 Statement 后关闭 Connection
     */
    @Test
    public void testStatement() throws Exception{
        //1. 获取数据库连接
        Connection conn = null;
        Statement statement = null;

        try {
            conn = getConnection2();

            //3. 准备插入的 SQL 语句
            String sql = null;

//          sql = "INSERT INTO customers (NAME, EMAIL, BIRTH) " +
//                  "VALUES('XYZ', 'xyz@atguigu.com', '1990-12-12')";
//          sql = "DELETE FROM customers WHERE id = 1";
            sql = "UPDATE customers SET name = 'TOM' " +
                    "WHERE id = 4";
            System.out.println(sql);

            //4. 执行插入. 
            //1). 获取操作 SQL 语句的 Statement 对象: 
            //调用 Connection 的 createStatement() 方法来获取
            statement = conn.createStatement();

            //2). 调用 Statement 对象的 executeUpdate(sql) 执行 SQL 语句进行插入
            statement.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                //5. 关闭 Statement 对象.
                if(statement != null)
                    statement.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                //2. 关闭连接
                if(conn != null)
                    conn.close();                           
            }
        }

    }


    public Connection getConnection2() throws Exception{
        //1. 准备连接数据库的 4 个字符串. 
        //1). 创建 Properties 对象
        Properties properties = new Properties();

        //2). 获取 jdbc.properties 对应的输入流
        InputStream in = 
                this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

        //3). 加载 2) 对应的输入流
        properties.load(in);

        //4). 具体决定 user, password 等4 个字符串. 
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String driver = properties.getProperty("driver");

        //2. 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)
        Class.forName(driver);

        //3. 通过 DriverManager 的 getConnection() 方法获取数据库连接. 
        return DriverManager.getConnection(jdbcUrl, user, password);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值