Selenium DB connectivity


Connecting database using java for selenium test 

 Jar file download : mysql-connector-java-5.1.8-bin.jar
Table Created : Test

 Code:

import java.sql.*;
import javax.sql.*;

public class jdbcdemo{

public static void main(String args[]){
String dbtime;
String dbUrl = "jdbc:mysql://localhost:3306/test";
String username="root";
String password="root";
String dbClass = "com.mysql.jdbc.Driver";
String query = "show tables;";

try {

Class.forName(dbClass);
Connection con = DriverManager.getConnection (dbUrl,username,password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while

con.close();
} //end try

catch(ClassNotFoundException e) {
e.printStackTrace();
}

catch(SQLException e) {
e.printStackTrace();
}

}  //end main

}  //end class

Note: Use --> String dbUrl = "jdbc:mysql://ip/yourDBname"; if you know the ip address of remote host!


keep testing!

Comments

  1. How to write MySQL database table values in to Excel sheet using selenium web-driver....can u please help me on this

    ReplyDelete
    Replies
    1. You can export your query to a CSV file and use an array or collection to run data driven tests.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi Bijoy,

    I see that you use Selenium WebDriver and MySQL. However, if you'd like to benefit from convenience of Selenium IDE, you may be interested in SeLite. It enhances Selenese and it helps the tests to use SQLite. See https://code.google.com/p/selite/wiki/ProjectHome.

    ReplyDelete
  4. Am getting the following Error message,please help...

    java.lang.ClassNotFoundException: JDBC_DRIVER
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:188)
    at AutomatingBackOffice.DatabaseConnect.main(DatabaseConnect.java:29)

    ReplyDelete

Post a Comment

Popular Posts