22计应04

package com.yanyu;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest01 {
    //    第三次作业
//    读取配置文件
//    psvm

//       =   右边有了  ,左边的    ;  crtl  alt  v
    public static void main(String[] args) {
//        RB  获取  资源绑定对象
        //       =   右边有了  ,左边的    ;  crtl  alt  v
        ResourceBundle bundle = ResourceBundle.getBundle("com/yanyu/db");//ctrl
//        ;  后面   ctrl  alt  v
//        ba   .properties  删掉
//      用资源绑定对象 读取文件     bundle  的 前两个字母   bu
        String driver = bundle.getString("driver");//  ctrl   alt   v
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
//        String driver1 = bundle.getString("driver");//  ctrl  alt  v
//        放大作用域
        Connection con = null;
//        int   num1 = 10;
        PreparedStatement ps = null;
        ResultSet rs = null;
//        注册驱动
        try {
            Class.forName(driver);
//            获取链接 对象
            con = DriverManager.getConnection(url, user, password);
//            关闭事务提交
            con.setAutoCommit(false);
//            操作对象
            ps = con.prepareStatement("insert into user values(?,?)");//预编译
            ps.setInt(1,10);
            ps.setString(2,"yy");
            ps.executeUpdate();


            con.commit();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {

            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
//            关闭数据流
//            rs    ps  con
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }





        }
//        alt   enter






    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值