从不同的类调用jdbc连接,获取“ java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDriver”

时间:2018-07-16 05:06:39

标签: java oracle selenium-webdriver jdbc

我有一个主要方法,该方法用于从数据库中获取值,后来我将这些值用于进一步的输入数据操作。

主要方法

public static void main(String[] args) throws ClassNotFoundException, SQLException {                

// Object of Connection from the Database
    Connection conn = null;                                                             
    Statement stmt = null;                                              
    ResultSet resultSet = null;

    Class.forName("oracle.jdbc.driver.OracleDriver");

    String getDB_IP = Allocator.GetPropoertyfromfile("Database_IP");
    String getDB_SID = Allocator.GetPropoertyfromfile("Database_SID");
    String getDB_username = Allocator.GetPropoertyfromfile("Database_username");
    String getDB_password = Allocator.GetPropoertyfromfile("Database_password");
    String getDB_port = Allocator.GetPropoertyfromfile("Database_port");

   // Open a connection
    conn = DriverManager.getConnection("jdbc:oracle:****:@"+getDB_IP+":"+getDB_port+":"+getDB_SID,getDB_username , getDB_password);

    // Execute a query
    stmt = conn.createStatement();     

    resultSet = stmt.executeQuery("select t.*,t.rowid from s_adinfo t where adcode like '123465'");


    while (resultSet .next()) { 

    String firstLoginTime = resultSet.getString("FirstLOGINTIME");

    System.out.println("firstLoginTime = "+firstLoginTime);
   }
}

现在,在一个单独的类中创建此数据库方法的假设类似于类A,并在另一个类中调用该方法的假设类似于类B。这时出现了类似无法加载驱动程序的错误。

另一个类中的数据库方法

  public static String sqlQueryUsdpDBLastLoginTime ( String KeyNameOfDatabaseResult) throws ClassNotFoundException, SQLException {


    // Object of Connection from the Database
    Connection conn = null;

    // Object of Statement. It is used to create a Statement to execute the query
    Statement stmt = null;   

    //Object of ResultSet => 'It maintains a cursor that points to the current row in the result set'
    ResultSet resultSet = null;

    Class.forName("oracle.jdbc.driver.OracleDriver");

        String getDB_IP = Allocator.GetPropoertyfromfile("Database_IP");
        String getDB_SID = Allocator.GetPropoertyfromfile("Database_SID");
        String getDB_username = Allocator.GetPropoertyfromfile("Database_username");
        String getDB_password = Allocator.GetPropoertyfromfile("Database_password");
        String getDB_port = Allocator.GetPropoertyfromfile("Database_port");

    // Open a connection
    conn = DriverManager.getConnection("jdbc:oracle:***:@"+getDB_IP+":"+getDB_port+":"+getDB_SID,getDB_username , getDB_password);

    // Execute a query
    stmt = conn.createStatement();     


    resultSet = stmt.executeQuery("select t.*,t.rowid from t_adinfo t where adcode like '123465'");


    while (resultSet .next()) {                                                                                          
         keyNameValue = resultSet.getString(KeyNameOfDatabaseResult);                                                        

    }
    return keyNameValue;                                        
    }
}

控制台错误:

Class.forName("oracle.jdbc.driver.OracleDriver");

Jul 16, 2018 10:16:23 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at util.DBConnector.<init>(DBConnector.java:35)
at components.Corp.verifyLastLoginTime(Corp.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at 

at java.lang.reflect.Method.invoke(Method.java:498)
at components.LoadActionKeyword.<init>(LoadActionKeyword.java:104)
at components.Allocator.testcaserun(Allocator.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at 

1 个答案:

答案 0 :(得分:0)

您似乎没有在库文件夹中添加jdbc驱动程序的jar文件

从oracle网站下载ojdbc驱动程序,并将其添加到项目内的lib文件夹中,然后从“服务”选项卡中创建连接

您可以从此处下载:http://www.java2s.com/Code/Jar/o/Downloadojdbc14jar.htm

我正在添加一个youtube视频链接,请看他们如何添加jar的ojdbc驱动程序并逐步创建由我创建的java程序与Oracle数据库之间的连接(请看整个视频)

https://www.youtube.com/watch?v=vmP-wOskDRc&t=187s

期望这可以解决您的问题