找不到Oracle jdbc驱动程序 [英] Cannot find Oracle jdbc driver
问题描述
我是Java和数据库连接的新手,我正在尝试与oracle数据库建立非常简单的连接.当我运行此代码时:
I am new to java and database connections and I am trying to make a very simple connection to an oracle database. When I run this code:
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.
I get the output Can't find database driver
. I am using Eclipse Helios and I have added ojdbc6.jar
to my build path (first thing I double checked), and I am using JDK 1.6.
推荐答案
检查.jar是否也在您的运行路径中.在eclipse中,转到运行->运行配置->选择配置->类路径选项卡.如果您的jar不在右侧选择添加jar",则需要在用户条目"中.
Check that the .jar is also on your run path. In eclipse go to Run --> Run Configurations --> Select your configuration --> classpath tab. Your jar needs to be in the "user entries" if its not choose "Add Jar" on the right hand side.
这篇关于找不到Oracle jdbc驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!