Posted by : 小p
25 6月 2013
測試環境:
JAVA Version:java version "1.6.0_45"
Oracle Version:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
Step 1:Download JDBC
開啟 http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
下載:ojdbc6.jar (2,111,220 bytes) - Classes for use with JDK 1.6. It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types.
Step 2:
在Eclipse 專案資料夾中,建立一個CommonLib的資料夾
並將ojdbc6.jar存入該資料夾中
Step 3:將ojdbc6.jar匯入Eclipse專案中
1、在專案上按右鍵->Propertise會出現下圖
2、在左邊點選Java Build Path
3、點選Add JARs
4、展開CommonLib資料夾,選擇ojdbc6.jar
5、匯入後結果如下圖,會出現一個【ReferencedLibrary】的資料夾
Step 4:建立一個class,測試程式碼如下(JAVA_Exercise為範例名稱,可自行建立)
import java.sql.*; public class JAVA_Exercise { /** * @param args */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Class.forName("oracle.jdbc.OracleDriver"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@//localhost:1521/orcl", "userID", "userPWD"); // @//machineName:port/SID, userid, password try { Statement stmt = conn.createStatement(); try { ResultSet rset = stmt .executeQuery("select BANNER from SYS.V_$VERSION"); try { while (rset.next()) System.out.println(rset.getString(1)); // Print col 1 } finally { try { rset.close(); } catch (Exception ignore) { } } } finally { try { stmt.close(); } catch (Exception ignore) { } } } finally { try { conn.close(); } catch (Exception ignore) { } } } }
註:orcl為tnsnames.ora中的SERVICE_NAME
orcl =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl
)
)
Related Posts :
- Back to Home »
- [小PのIT筆記][JAVA]Program »
- [Oracle]使用JDBC與Oracle 11g 連線