本文实例讲述了java基于odbc连接oracle的实现方法。分享给大家供大家参考,具体如下:
1、加载 oracle驱动
oracle.jdbc.driver.OracleDriver
2、建立到给定数据库 URL 的连接
jdbc:oracle:thin:@localhost:1521:orcl
3、默认的用户名是system,密码是自己设定的,不能用sys或者是sysdba,这个只是角色.
public void testOracle() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl", "system", "admin"); System.out.println("连接成功!"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select *fromstu_info_tab"); while (rs.next()) { System.out.println("ok"); System.out.println(rs.getInt("student_id")); } rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { System.out.println("ClassNotFoundException"); } catch (SQLException e) { System.out.println("SQLException"); } }
希望本文所述对大家java程序设计有所帮助。