Monday, December 3, 2012

Selenium Database Connectivity using Java

We can easily connect Database with Selenium. The below example illustrates how to use/connect MySQL with Selenium using Java. 

Prerequisites:

mysql-connector-java-5.1.0-bin.jar  or above versions
Create a table with some data

Snippet:

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

public class dbconnection
{
public static void main(String args[])
{
String email;
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 email 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(email);
} //end while

con.close();
} //end try

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

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

}  //end main

}  //end class

9 comments:

  1. Hi Anitha,

    This is Anand. I am working as Manual Test Engineer. I am Learning Web driver my self thru goggling and reading some of blogs.I read your blog i am looking for the blog which you posted about the Data Base connection. My question is can i use the same script for webdriver, Eclipse, TestNG??
    Please help me to post more blogs. if you have any notes on web driver please forward me.

    I want to automate one end to end scenario for log In functionality with valid and Invalid credentials. I want read the error messages also hope you understand. Please forward me the script.
    Thanking you with early anticipation
    Thanks/Regards,
    Anand
    Mail Id: Patil.anand677@gmail.com

    ReplyDelete
  2. Hi,

    Yes, you can use the same for DB connection using Selenium. Please go through this blog entirely to get some insights about selenium. Use Selenium IDE (A firefox plug-in) to learn/record selenium scripts.

    Thanks

    ReplyDelete
    Replies
    1. Hi Anitha,
      I have read all your blogs it's really helpful for the beginners.
      Can you please send me one log In functionality end to end with valid and Invalid credentials?. It will help me lot to automate other modules in my application.

      Which automation framework is good to use?
      How to make frameworks?

      Please send me details to my id.

      Thanks/Regards,
      Anand
      My Id: Patil.anand677@gmail.com













      Delete
  3. Hi Anand,

    Very soon we will post an article about this. So that it will be useful for others too.

    Choosing a tool and framework is depends on your requirement. You can find lot of framework for selenium in internet.

    Thanks

    ReplyDelete
  4. hi all my name is nitin,

    And i m learning selenium by my own.

    Need some help......

    i m having an web based applicatin which i want to test it with the help of selenium

    Do any one have any simple jave program which i can write it into eclipse and testNg for the database connection.

    My email id is : nitin_words4u@yahoo.co.in

    ReplyDelete
  5. hai all i am alagar. i am a automate test engineer.

    i have some clarification on using webdriver. In the application combo contain 10 values. i want to select each value by doing testing
    for example the combo contains three value male,female,other
    i have to select first time male and next female and next other
    how to do it using selenium webdriver.?
    if i want to select only once means i will do

    new Select(dri.findElement(By.name("ctl00$ContentPlaceHolder1$gender"))).selectByVisibleText("male");
    but it is not only one value thousands of value will be available

    my id is algates.bca@gmail.com
    your valueble feedback is most welcome

    ReplyDelete
  6. The above script is incorrect :

    Instead of dbtime = rs.getString(1);... it should be email = rs.getString(1);

    ReplyDelete
  7. Can this be used to connect to a remote SQL DB?

    ReplyDelete