我是java和數據庫連接的新手,我試圖與oracle數據庫進行非常簡單的連接。當我運行這段代碼時:找不到Oracle jdbc驅動程序
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
public class Hello
{
public String sayHtmlHello()
{
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
// Create a connection to the database
String serverName = "sever2";
String portNumber = "1521";
String sid = "serv1";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber +":" + sid;
String username = "user";
String password = "pass";
OracleDataSource ods = new OracleDataSource();
ods.setUser(username);
ods.setPassword(password);
ods.setURL(url);
Connection conn = ods.getConnection();
System.out.println("Connection made?");
} catch (ClassNotFoundException e) {
// Could not find the database driver
System.out.println("Can't find database driver");
} catch (SQLException e) {
// Could not connect to the database
System.out.println("Can't connect to database");
}
我得到輸出Can't find database driver
。我正在使用Eclipse Helios,並且已將ojdbc6.jar
添加到我的構建路徑中(我仔細檢查了第一件事),並且使用了JDK 1.6。
您可以在第一個catch塊中使用e.printstacktrace()並粘貼完整的堆棧跟蹤嗎? – nsfyn55 2011-05-25 14:49:55
還是更好,不要捕捉異常(重新拋出它''拋出新的RuntimeException(e)') – artbristol 2011-05-25 14:55:15
@atbristol在技術上你必須趕上它來重新拋出:) – nsfyn55 2011-05-25 16:31:27