开发者

Cannot find Oracle jdbc driver

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.开发者_StackOverflow中文版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");
    }

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜