我是java和数据库连接的新手,我正在尝试与oracle数据库建立一个非常简单的连接。当我运行此代码时:
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。
答案 0 :(得分:7)
检查.jar是否也在您的运行路径上。在eclipse中去Run - >运行配置 - >选择您的配置 - > classpath选项卡。如果你的jar不在右侧选择“Add Jar”,你的jar需要在“用户条目”中。