'Connect Remote Database using Selenium Webdriver and run Test Cases from local machine through eclipse

I am using selenium web driver java built, the editor is eclipse. For testing one of our websites I am using Data-driven testing by fetching data from MySQL database.

I dumped the development server database to my local machine and installed that dumped data in my machine xampp and able to connect to the database and proceed through the testing process.

To connect to my local machine database I am using this connection string

String url1 ="jdbc:mysql://localhost:3306/databasename";           
String dbClass = "com.mysql.jdbc.Driver";
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(url1, "root", "");
Statement stmt = (Statement) con.createStatement();

Now I need to connect to our original development server database which is in remote server.

I have tried this as connection string to connect remote machine

 String url1 ="jdbc:mysql://10.0.2.129:3306/test";
 String dbClass ="com.mysql.jdbc.Driver";
 Class.forName(dbClass).newInstance();
 Connection con = DriverManager.getConnection(url1, "root","root");

but not able to connect to the remote machine database.Following error is showing

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)

Can anyone suggest me what changes are needed to be done in the connection string to connect the remote server database which is ht access protected? and how I can run my test cases from my local machine while connecting to the remote server database.

Please provide some suggestion.



Solution 1:[1]

You just need to specify machine's IP address, port and database name in the connection string. So try something like

String dbUrl = "jdbc:mysql://192.168.1.53306/myDB";
DriverManager.getConnection(dbUrl, "username", "password");

And your tests should work the same way as before, you are just connectng to the different machine which can be more time consuming (e.g. queries can take more time) but that's the only difference.

Solution 2:[2]

You can utilize data driven capability provided by one of the selenium testing-frameworks - ISFW where you can specify data base query to provide test data. For example:

@IsDataProvider(sqlQuery="select query")
@Test
public void testSomething(Map<String, String> testdata){
  //use query col as key testdata.get("colName")
  }

Solution 3:[3]

You need to add a jar file to connect to the database

jar: mysql-connector-java

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3