加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle 数据库的连接方式实现方法

发布时间:2020-12-12 14:35:25 所属栏目:百科 来源:网络整理
导读:/** * Oracle 数据库的连接方式实现方法 */public void testJDBCCommon() { // 建立数据库连接对象 Connection conn = null; Statement st = null; ResultSet rst = null; Driver driver = null; try { //step 1: 注册驱动到jvm driver = new oracle.jdbc.d
/**
 * Oracle 数据库的连接方式实现方法
 */
public void testJDBCCommon() {
    // 建立数据库连接对象
    Connection conn = null;
    Statement st = null;
    ResultSet rst = null;
    Driver driver = null;
    try {
        //step 1: 注册驱动到jvm
        driver = new oracle.jdbc.driver.OracleDriver();
        DriverManager.registerDriver(driver);
        //step 2:获取数据库连接; 
        conn = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:XE","hr","hr");
        //step 3:创建Statement;
        st = conn.createStatement();    
        //step 4:执行查询语句,获取结果集;
        rst = st.executeQuery("select * from employees");
         //step 5:处理结果集—输出结果集中保存的查询结果; 
        while (rst.next()) {
            System.out.println(rst.getString(1) + "  " + rst.getString(2)
                    + " " + rst.getString("LAST_NAME"));
 
        }
 
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {// 无论运行会不会进入到try。。catch代码块,最终都会运行finally代码块
        // 关闭对象,防止多用户环境下挂起问题和锁定问题
        try {
            if (rst != null)
                rst.close();
            if (st != null)
                st.close();
            if (conn != null)
                conn.close();
            driver = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读