无法将SQL Server连接到Selenium Webdriver

时间:2018-05-24 09:11:48

标签: selenium-webdriver

我已执行以下步骤: 1-控制面板>管理工具>单击ODBC

2-转到用户DSN并单击添加按钮

3-选择相应的数据库(SQL Server Native Client)

4-将名称命名为DSN并保存

通过pom.xml将以下JDBC驱动程序添加到我的项目中:



	<dependency>
  		 <groupId>com.microsoft.sqlserver</groupId>
   		 <artifactId>mssql-jdbc</artifactId>
   		 <version>6.5.2.jre9-preview</version>
   		 <scope>test</scope>
	</dependency>
&#13;
&#13;
&#13;

然后我从eclipse执行了以下简单代码:

  

的Class.forName(&#34; com.microsoft.sqlserver.jdbc.SQLServerDriver&#34);

     

连接conn = DriverManager.getConnection(&#34; wsyddbd10678,1433&#34;,&#34;用户名&#34;,&#34;密码&#34;);

     

Statement st = conn.createStatement();

     

String Sql =&#34;从miris.dimset中选择*其中setsk介于5499和5510之间&#34 ;;

     

ResultSet rs = st.executeQuery(Sql);

     

while(rs.next())
  {         的System.out.println(rs.getString(&#34; setsk&#34));
  }

但它给我一个错误说:

Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver. 

请建议..

1 个答案:

答案 0 :(得分:0)

获取与DB的连接您需要首先设置一些依赖项,具体取决于您连接的内容(哪个DB)。

  

连接字符串设置:https://www.connectionstrings.com/sql-server/

对于MSSQL:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>

以下是几种可以连接到DB的方法:

最简单(直接连接)

private WebDriver driver;
private Connection con = null;
private Statement stmt = null;
String dataBaseName = "testDB";
String dbDriver = "jdbc:sqlserver://localhost:3306/";
String user = "user";
String pass = "pass";
String Query;
ResultSet res;

public void DBConnection() throws Exception {
    try {
        Class.forName(
                "com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection con = DriverManager.getConnection(
                dbDriver + dataBaseName, user, pass);
        stmt = con.createStatement();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}


@Test
public void testDB() throws Exception {
    try {
        Query = "Select * from table";
        res = stmt.executeQuery(Query);
        driver = new FirefoxDriver();  //or whatever driver
        driver.get("//some URL //");
        while (res.next()) //move to the next row in result set, 
        //... do your stuff //
    }
} catch(Exception e)

{
    System.out.println(e.getMessage());
}

或您的示例(已更新);

连接语法(MS-SQL Server):

  

[URL] = jdbc:sqlserver:// [ipAddress]:portNumber / dbName

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");      
Connection con = DriverManager.getConnection([URL], userName, password);

或试试这个:

Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection([URL],userName, password);

然后是代码的其余部分:

Statement st = conn.createStatement();
String Sql = "query";
ResultSet rs = st.executeQuery(Sql);

while (rs.next()) 
{
    System.out.println(rs.getString("setsk"));
}

(或者我认为最好的)Hibernate

设置它几乎没有什么工作,但是当它启动并运行时,它确实令人满意。以下是关于它的教程:https://www.javatpoint.com/steps-to-create-first-hibernate-application

希望这有用。