How to do database testing using selenium rc

How to do database testing using selenium rc?

There are no selenium specific commands to do the database testing. But, you have the work around with native language. Here is the example how you can make database testing in selenium. The below mentioned commands will help you to begin with.

Before you use the below code You should have created the DSN name for MS SQL Server DB or Oracle DB using ODBC drivers. Here the name that we have used for the database DSN name is QT_Flight32. And the table in that database we are using is Orders. Hope the remaining steps you can easily understand.

//KEEP THE NAME OF THE PACKAGE
package com.src.test;

//IMPORT THE SQL PACKAGE
import java.sql.* ;

//DECLARE THE CLASS
class JDBCODBCTestCase
{

//WRITE THE MAIN METHOD TO CONNECT TO THE DATABASE
public static void main( String args[] )
{

try{

// LOAD THE DATABASE DRIVER
Class.forName( “sun.jdbc.odbc.JdbcOdbcDriver” ) ;

// ESTABLISH THE CONNECTION TO THE DATABASE
Connection conn = DriverManager.getConnection( “jdbc:odbc:QT_Flight32” ) ;

// GET A STATEMENT FOR THE CONNECTION
Statement stmt = conn.createStatement() ;

// PREPARE THE SQL STATEMENT
String strSQL = “SELECT * FROM Orders”;

// EXECUTE THE SQL QUERY AND STORE IN RESULTS SET
ResultSet rs = stmt.executeQuery( strSQL ) ;

// LOOP THE RESULT TILL IT REACHED END
// rs.getString(1) – FIRST COLUMN
// rs.getString(2) – SECOND COLUMN ETC
while( rs.next() )
System.out.println( rs.getString(1) + rs.getString(2)+ rs.getString(3)+ rs.getString(4)) ;

// CLOSE THE RESULT, STATEMENT AND CONNECTION
rs.close() ;
stmt.close() ;
conn.close() ;

}
// HANDLE THE SQL EXCEPTION
catch( SQLException se ) {

System.out.println( “SQL Exception:” ) ;

// PRINT TILL ALL THE ECEPTIONS ARE RAISED
while( se != null ) {

System.out.println( “State : ” + se.getSQLState() ) ;
System.out.println( “Message: ” + se.getMessage() ) ;
System.out.println( “Error : ” + se.getErrorCode() ) ;

se = se.getNextException() ;

}

}
//CATCH THE CLASS EXCEPTION
catch( Exception e ) {

System.out.println( e ) ;

}
}

}

Advertisements
%d bloggers like this: