用JDBC连接Oracle数据库 via Eclipse

来源:互联网 发布:你愿意做程序员么 编辑:程序博客网 时间:2024/06/07 21:58

老方法链接数据库是这样的:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection conn = DriverManager.getConnection("jdbc:odbc:testsp","system","123");

从Stack overflow得知,"For Java 8 you cannot use the JDBC-ODBC Bridge because it has been removed." Java 8 取消了JDBC-ODBC的链接桥。

返回Oracle官网,阅读相关文档并下载相关的组件。

链接:

"http://www.oracle.com/technetwork/apps-tech/jdbc-112010-090769.html"

组件名:

Downloadojdbc6.jar (2,739,670 bytes) - (SHA1 Checksum: a483a046eee2f404d864a6ff5b09dc0e1be3fe6c)
Certified with JDK 8, JDK 7 and JDK 6: It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types.

然后在Eclipse添加Build path


package com.William;import java.sql.*;public class TestOracle {public static void main(String[] args) {// 使用jdbc_odbc桥连接方式try{//加载驱动Class.forName("oracle.jdbc.driver.OracleDriver");//得到连接Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123");conn.setAutoCommit(false);Statement stmt = conn.createStatement();ResultSet rset = stmt.executeQuery("select ename from emp");while(rset.next()){System.out.println(rset.getString(1));}stmt.close();System.out.println("ok.");}catch (Exception e){e.printStackTrace();}}}

对于获取连接的解读:

 //    // or    // DriverManager.registerDriver    //        (new oracle.jdbc.driver.OracleDriver());        String url = "jdbc:oracle:thin:@//server.local:1521/prod";    //               jdbc:oracle:thin:@//host:port/service    // or    // String url = "jdbc:oracle:thin:@server.local:1521:prodsid";    //               jdbc:oracle:thin:@host:port:SID    //    //  SID  - System ID of the Oracle server database instance.我用的此方法。//         By default, Oracle Database 10g Express Edition//         creates one database instance called XE.在此用数据库实例名字//         ex : String url = "jdbc:oracle:thin:@myhost:1521:xe";
jdbc:oracle:<drivertype>:<username/password>@<database>The <drivertype> is one of    thin    oci    kprb

油管有视频,作者是Vishnu Kyatannawar,视频名称是:

How to connect to oracle database in java (using Eclipse)



0 0