没有为jdbc找到合适的驱动程序:oracle:thin:

时间:2016-12-28 13:55:46

标签: java eclipse oracle selenium jdbc

当我从selenium web驱动程序运行此测试用例时,我收到以下错误:

java.sql.SQLException:没有为jdbc找到合适的驱动程序:oracle:thin:@ 10.96.0.65:1521:orcl     在java.sql.DriverManager.getConnection(DriverManager.java:602)     在java.sql.DriverManager.getConnection(DriverManager.java:185)     在Database.DatabaseValidation.test(DatabaseValidation.java:50)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)     在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)     在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)     在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)     在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)     在org.junit.runners.ParentRunner.run(ParentRunner.java:309)     在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)     在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

package Database;

import oracle.jdbc.*;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.driver.*;
import java.sql.PreparedStatement;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class DatabaseValidation {

private WebDriver driver = null;
private Connection con = null;
private Statement stmt = null;
String baseUrl;

@Before
public void setUp() throws Exception {
// use firefox browser
  ProfilesIni profile = new ProfilesIni(); 
    FirefoxProfile myprofile = profile.getProfile("SOFAdmin");
    driver = new FirefoxDriver(myprofile);
    baseUrl = "https://10.96.0.65:9443";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

@Test
public void test() throws SQLException, ClassNotFoundException {
// Load Microsoft SQL Server JDBC driver.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Prepare connection url.
String url = "jdbc:oracle:thin:@10.96.0.65:1521:orcl";
// Get connection to DB.
con = DriverManager.getConnection(url, "POS_SOF", "POS_SOF");
// Create statement object which would be used in writing DDL and DML
// SQL statement.
stmt = con.createStatement();
// Send SQL SELECT statements to the database via the
// Statement.executeQuery
// method which returns the requested information as rows of data in a
// ResultSet object.
// define query to read data
try {
  String query = "select * from ACCOUNTS";
  ResultSet result = stmt.executeQuery(query);
  if (result.next()) {
    while (result.next()) {
      // Fetch value of "username" and "password" from "result"
      // object; this will return 2 existing users in the DB.


      String username = result.getString("ID");
      String password = result.getString("CODE");
      // print them on the console
      System.out.println("ID :" + username);
      System.out.println("CODE: " + password);
    }
    result.close();
  }
}

catch (SQLException ex) {
  System.out.println(ex);
}
// Add a new user on the UI
String newtestusername = "test1234";
String newtestuserpassword = "1234";
// navigate to the site
driver.get(baseUrl + "/POSAdminTool/AdminToo"
        + "l/Login.faces");
// set new user name "NewTestUser"
driver.findElement(By.id("userID")).sendKeys(newtestusername);
// set new user password for the new user "NewTestUser"
driver.findElement(By.id("password")).sendKeys(newtestuserpassword);
// click on Add User button
driver.findElement(By.id("form1:btn_login")).click();
// verify the welcome message displayed
System.out
    .println("Is welcome message displayed: "
        + isElementPresent(By
            .xpath("//*[contains(.,'Welcome back ')]")));

// verify the new user in the database
// create a query
String newuserquery = "SELECT * From userlogin where username=?";
// create a statement
PreparedStatement stat = con.prepareStatement(newuserquery);
stat.setString(1, newtestusername);
try {
  boolean hasResultSet = stat.execute();
  if (hasResultSet) {
    ResultSet result = stat.getResultSet();
    // get new user name from the table

    String newusername = result.getString("username");
    // assert that new user name should be
    assertEquals(newtestusername, newusername);
  }
} catch (SQLException ex)

{
  System.out.println(ex);
} finally {
  con.close();
}

}

@After
public void tearDown() throws Exception {
// close the driver
driver.close();
}

private boolean isElementPresent(By by) {
try {
  driver.findElement(by);
  return true;
} catch (NoSuchElementException e) {
  return false;
}
}

}

1 个答案:

答案 0 :(得分:2)

您正在使用带有Oracle URL的Microsoft SQL Server JDBC驱动程序类。

// Load Microsoft SQL Server JDBC driver.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

这是完全错误的。获取Oracle JDBC驱动程序JAR。您必须将驱动程序与要连接的数据库进行匹配。

您迫切需要JDBC tutorial

您也可以使用如何正确编写JUnit测试的课程。我建议您将这些连接外部化并加强这些测试。