当前位置:Gxlcms > 数据库问题 > JDBC连接Oracle

JDBC连接Oracle

时间:2021-07-01 10:21:17 帮助过:28人阅读

JDBC连接数据库过程就是:

1,加载驱动,建立连接

2,创建sql语句对象

3,执行sql语句

4,处理结果集

5,关闭连接

 

这五个步骤中主要了解4大知识点:

1,驱动管理DriverManager

  ClassForName("Oracle.jdbc.driver.OracleDriver")

2,连接对象

  Connection接口 :负责应用程序对数据库的连接,在加载驱动后,使用url,username,password三个参数创建具体的数据库连接1

3,sql语句对象接口

  Statement接口用来处理发送到数据库的SQL语句对象

4,结果集接口

  ResultSet接口:执行查询SQL语句后返回的结果集合。

 

public void findAll(){
  Connection con=null;
  Statement stmt=null;
  ResultSet rs=null;
  try {
    Class.forName("oracle.jdbc.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe";,"username","password");
    stmt=con.createStatement();
    String sql="select empno, ename, sal, hiredate from emp";
    rs=stmt.executeQuery(sql);
    while (rs.next()) {
      System.out.println(rs.getInt("empno") + ","
      + rs.getString("ename") + ","
      + rs.getDouble("sal") + "," + rs.getDate("hiredate"));
    }
  } catch (ClassNotFoundException e) {
    System.out.println("驱动类无法找到!");
    throw new RuntimeException(e);
  } catch (SQLException e) {
    System.out.println("数据库访问异常!");
    throw new RuntimeException(e);
  }finally {
    try {
    if (rs != null) {
      rs.close();
    }
    if (stmt != null) {
      stmt.close();
    }
    if (con != null) {
      con.close();
    }
    } catch (SQLException e) {
      System.out.println("关闭连接时发生异常");
    }
  }
}

JDBC连接Oracle

标签:数据库   row   run   for   dal   set   oid   应用程序   cti   

人气教程排行