2009-08-21 49 views
1

我已經使用JDK1.6安裝了NetBeans 6.5。我想在NetBeans 6.5中使用java與oracle進行連接。問題是:JDBC精簡連接

如何將JDBC配置爲java 1.6?

感謝, Sopolin

回答

1

您需要下載Oracle瘦JDBC驅動程序,並彈出它在你的類路徑中。

請參閱這裏的代碼示例。

http://w2.syronex.com/jmr/edu/db/oracle-and-java

+0

您好,我看到了你的參考,但我效仿的榜樣在這個網站之後,我有錯誤發生。在線程「main」中的異常java.lang.NoClassDefFoundError:C:\ TestJava \ DbTest 原因:java.lang.ClassNotFoundException:c:\ TestJava \ DbTest at java.net.URLClassLoader $ 1.run(Unknown Source).. ... 你能指導我在oracle中使用java配置JDBC Thin驅動程序嗎? – Sopolin 2009-08-22 04:21:47

+0

你如何執行你的程序看起來像你做錯了什麼。它應該看起來像java -cp ... DbTest你在使用什麼IDE,因爲通過IDE運行和配置應用程序更容易。 – pjp 2009-08-22 16:18:53

+0

是的,我複製了DbTest以運行測試。然後當我跑它時,它出現這個錯誤。 – Sopolin 2009-08-24 01:40:18

1

這裏的提示:1.下載 從以下站點正確的Oracle數據庫驅動程序版本: http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html 並導入到NetBeans的libary如果你使用NetBeans IDE作爲。

2.In你的Java代碼,定義一樣,適當的JDBC連接聲明:

public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ; 
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:Your_DB_NAME"; 
public static final String DBUSER = "YOUR ORACLE DB ID" ; 
public static final String DBPASS = "YOUR ORACLE DB PASSWORD" ; 


Connection conn = null ; // DB CONNECTIONS 
PreparedStatement pstmt = null ;// DB OPERATIONS 
ResultSet rs = null ;  // save the query result 


Class.forName(DBDRIVER) ; // Load the ORACLE DRIVER 
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ; 
String sql = "SELECT name FROM client" ; //sample query 
pstmt = conn.prepareStatement(sql) ; // execute the query and save the result 

    // the above cope snippet is the main things of JDBC. 
    //Hope it helps!