当前位置: 移动技术网 > IT编程>开发语言>Java > java基于JDBC连接Oracle 11g Release2实例分析

java基于JDBC连接Oracle 11g Release2实例分析

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java基于jdbc连接oracle 11g release2的方法。分享给大家供大家参考。具体如下:

oracle 11g release 2 的 jdbc 连接似乎有所不同 ,如果你收到下面的异常:
listener refused the connection with the following error:ora-12505, tns:listener does not currently know of sid given in connect descriptor.
那么你必须用下面的连接方式:

/*******************************************************
* created on nov, 2011 copyright(c) http://vigilance.co.in all rights reserved.
********************************************************/
package com.vigilance.java.sample;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
/**
 * @author http://vigilance.co.in
 */
public class connectjdbcoracle11g {
/**
 * this class demonstrates the code for connecting oracle 11g database using jdbc.
 * @param args
*/
public static void main(string[] args) {
  string jdbc_driver = "oracle.jdbc.driver.oracledriver";
  string jdbc_string = "jdbc:odbc:thin:@hostname:portnumber/sid";
  // in case of 11g use '/' instead of :
  string user_name = "user_name";
  string passwd = "password";
  connection conn = null;
  resultset rs = null;
  statement stmt = null;
  try{
    class.forname(jdbc_driver);
    conn = drivermanager.getconnection(jdbc_string, user_name, passwd);
    stmt = conn.createstatement();
    string query = "select * from table tbl";
    rs = stmt.executequery(query);
  }catch(sqlexception sqlex){
    sqlex.printstacktrace();
  } catch (classnotfoundexception e) {
    e.printstacktrace();
  } finally{
    try {
      if(rs!=null) rs.close();
      if(stmt !=null) stmt.close();
      if(conn!=null) conn.close();
    } catch (sqlexception e) {
      e.printstacktrace();
    }
  }
}
}

希望本文所述对大家的java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网