Automation Using Selenium Webdriver

Tuesday, 1 November 2016

MySQL database connectivity Selenium Script


Selenium Script for MySQL database connectivity

Prerequisite:

Download - mysql-connector-java-latest-bin.jar and add it to your project

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

public class dbconnection{

public static void main(String args[]){
   String username;
   String dbUrl = "jdbc:mysql://localhost:3306/test";  //This URL is based on your IP address
   String username="username"; //Default username is root
   String password="password"; //Default password is root
   String dbClass = "com.mysql.jdbc.Driver";
   String query = "Select username from users where user_id = 1;";
  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(username);
       } //end while

  con.close();
  } //end try

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

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

}  //end main

}  //end class

No comments:

Post a Comment