Skip to main content

Database connection and selenium with examples

This illustrates how to connect MySQL database with Selenium using Java bindings.  Make sure mysql-connector-java-5.x.x-bin.jar in your buildpath.

Configure MySQL

1| Download an install MySQL.
2| Create a database and table. e.g.,
create database one;
create table pets (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
3| Follow up whatever queries intended
4| Now, try to connect the schema with java via selenium webdriver for retrieving the table values.


private String dbvalue;
private String dbUrl = "jdbc:mysql://localhost:3306/one"; //Here, one is the schema/db
private String username="root";
private String password="yourpassword";
private String dbClass = "com.mysql.jdbc.Driver";

@Test
public void connectdb() throws Exception {  
driver.get("www.xyz.com");
      
try {
Class.forName(dbClass);
Connection con = DriverManager.getConnection (dbUrl,username,password);
Statement stmt = con.createStatement();

//insert values into the table | Get values from the already created schema/db
String insertvalues1 = "Insert into pets values('dashund', 'Sams', 'DOG', 'M' ,'2012-05-12', '2012-05-12');";
String insertvalues2 = "Insert into pets values('brownie', 'Sams', 'CAT', 'F' ,'2013-07-22', '0000-00-00');";
String insertvalues3 = "Insert into pets values('parrot', 'Sams', 'BIRD', 'F' ,'2011-12-02', '0000-00-00');";
stmt.executeUpdate(insertvalues1);
stmt.executeUpdate(insertvalues2);
stmt.executeUpdate(insertvalues3);

String query = "select * from pets";
System.out.println(query);
ResultSet rst = stmt.executeQuery(query);

//In a loop
while (rst.next()) {
dbvalue =rst.getString(1);
System.out.println(dbvalue);
driver.findElement(By.id(Value)).sendKeys(dbvalue);
driver.findElement(By.id(Value)).submit();
        driver.get("www.xyz.com");
}

con.close();

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

Comments

Popular posts from this blog

Jenkins CLI examples

Automate Jenkins with the CLI or the REST API Jenkins is used a lot to automate your build process and make it a real continuous integration process. But you can even take it a step further and automate the configuration of Jenkins itself.  In this short instruction I will show two ways to do this: the CLI and the REST API. With these capabilities you can for example write programs to create, backup, restore, start and view Jenkins jobs.  Download the Try-it-out-yourself code to provision an Ubuntu VM with Jenkins installed. This way you can immediately try the examples below.  The CLI   The Jenkins CLI is distributed inside the jenkins.war, but you have to download it before you can use it. Suppose your Jenkins url is: http://localhost:8080/jenkins Then the CLI can be downloaded like this:  wget http://localhost:8080/jenkins/jnlpJars/jenkins-cli.jar Note In the remainder of this document I assume the Jenkins url is http://localhost:8080/jenkins. You shoul

Jenkins Matrix Jobs

For many similar different combination build setup. Example Diff OS v/s Diff platform Configuration Matrix CHROME IE Test Module1 Test Module2 Test Module3 Test Module4 Plugin required:  matrix-project Wiki:  https://wiki.jenkins-ci.org/display/JENKINS/Matrix+Project+Plugin Location to download plugin: http://updates.jenkins-ci.org/download/plugins/ Install Plugin: Jenkins>Manage jenkins> Manage Plugin> Advansed> Browse and install Result Look like: Configuration: