java基于JDBC连接Oracle 11g Release2实例分析
程序员文章站
2024-03-05 12:08:00
本文实例讲述了java基于jdbc连接oracle 11g release2的方法。分享给大家供大家参考。具体如下:
oracle 11g release 2 的 jdb...
本文实例讲述了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程序设计有所帮助。
下一篇: Android中的设计模式