用JDBC连接Oracle数据库 via Eclipse

5 篇文章 0 订阅

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

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"

组件名:

Download ojdbc6.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值