一,连接驱动

数据库的链接需要下载对应数据库的驱动jar,在连接oracle上面可以使用ojdbc6.jar 或者 ojdbc7.jar,下载地址可以访问http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html来下载对应的驱动包。

二,连接示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.devnp.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class OracleJDBCUtil {

public static void main(String[] args) {
// TODO Auto-generated method stub

Connection connection = getConnection();

if(connection == null)
System.out.println("Get Oracle JDBC Connection failed.");
else
System.out.println("Get Oracle JDBC Connection Success.");
}

public static Connection getConnection(){
System.out.println("-------- Oracle JDBC Connection Testing ------");

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Load Oracle JDBC Driver has Error.");

e.printStackTrace();
}

Connection connection = null;

try {

connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "password");

} catch (SQLException e) {

System.out.println("Connection Failed!");

e.printStackTrace();

}

return connection ;
}

}
测试用例:
 D:\Person-Test>javac OracleJDBCUtil.java

D:\Person-Test>java -cp D:\Person-Test\ojdbc7.jar;D:\Person-Test OracleJDBCUtil
 -------- Oracle JDBC Connection Testing ------------
 Get Oracle JDBC Connection Success.