In order to do Database testing using Selenium you have to make a connection to the different Databases as per requirement.Please follow Below Steps
Step:1
Make a connection to the different Databases as per requirement
MySQL connection:
public static void getMySQLConnection(String hostName,
String portConn,
String dbName,
String userName,
String passWord)
throws ClassNotFoundException, SQLException {
// With port
String connectionURL = "jdbc:mysql://" + hostName + ":" + portConn + "/" + dbName;
// Without port
String connectionURL = "jdbc:mysql://"
+ hostName + "/"
+ dbName
+ "?characterEncoding=UTF-8&useSSL=false";
// Connection
Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
System.out.println("--- MySQL database connected ---");
}
SQL-Server connection by JDTS:
public static void getSQLServerConnection_JDTS(String hostName,
String sqlInstanceName,
String database,
String userName,
String passWord,
String portConn)
throws ClassNotFoundException, SQLException {
/*
* Syntax: "jdbc:jtds:sqlserver://localhost:1433/testsimple;instance=SQLEXPRESS"
*/
String connectionURL = "jdbc:jtds:sqlserver://"
+ hostName + ":"
+ portConn + "/"
+ database
+ ";instance="
+ sqlInstanceName;
Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
System.out.println("--- SQLSERVER JTDS connected ---");
}
SQL-Server connection by JDBC:
public static void getSQLServerConnection_JDBC(String hostName,
String portConn,
String sqlInstanceName,
String database,
String userName,
String passWord)
throws ClassNotFoundException, SQLException {
/*
* Syntax: "jdbc:sqlserver://ServerIp:1433;instance=SQLEXPRESS;databaseName=testmydb"
*/
String connectionURL = "jdbc:sqlserver://"
+ hostName + ":"
+ portConn + ";"
+ sqlInstanceName + ";"
+ "databaseName="
+ database;
Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
System.out.println("--- SQLSERVER JDBC connected ---");
}
Oracle connection:
public static void getOracleConnection(String hostName,
String sid,
String userName,
String password,
String port)
throws ClassNotFoundException, SQLException {
//Syntax: "jdbc:oracle:thin:@localhost:1521:db11g"
String connectionURL = "jdbc:oracle:thin:@"
+ hostName + ":"
+ port + ":"
+ sid;
Connection conn = DriverManager.getConnection(connectionURL, userName, password);
System.out.println("--- ORACLE database connected ---");
}
Step:2
Send Queries to the Database and retrieve the data.
//Statement Object to send queries
Statement stmt = con.createStatement();
//send the query to database use execute query and store the results in the Result Set object
Step:3
Process the results.
Step:1
Make a connection to the different Databases as per requirement
MySQL connection:
public static void getMySQLConnection(String hostName,
String portConn,
String dbName,
String userName,
String passWord)
throws ClassNotFoundException, SQLException {
// With port
String connectionURL = "jdbc:mysql://" + hostName + ":" + portConn + "/" + dbName;
// Without port
String connectionURL = "jdbc:mysql://"
+ hostName + "/"
+ dbName
+ "?characterEncoding=UTF-8&useSSL=false";
// Connection
Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
System.out.println("--- MySQL database connected ---");
}
SQL-Server connection by JDTS:
public static void getSQLServerConnection_JDTS(String hostName,
String sqlInstanceName,
String database,
String userName,
String passWord,
String portConn)
throws ClassNotFoundException, SQLException {
/*
* Syntax: "jdbc:jtds:sqlserver://localhost:1433/testsimple;instance=SQLEXPRESS"
*/
String connectionURL = "jdbc:jtds:sqlserver://"
+ hostName + ":"
+ portConn + "/"
+ database
+ ";instance="
+ sqlInstanceName;
Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
System.out.println("--- SQLSERVER JTDS connected ---");
}
SQL-Server connection by JDBC:
public static void getSQLServerConnection_JDBC(String hostName,
String portConn,
String sqlInstanceName,
String database,
String userName,
String passWord)
throws ClassNotFoundException, SQLException {
/*
* Syntax: "jdbc:sqlserver://ServerIp:1433;instance=SQLEXPRESS;databaseName=testmydb"
*/
String connectionURL = "jdbc:sqlserver://"
+ hostName + ":"
+ portConn + ";"
+ sqlInstanceName + ";"
+ "databaseName="
+ database;
Connection conn = DriverManager.getConnection(connectionURL, userName, passWord);
System.out.println("--- SQLSERVER JDBC connected ---");
}
Oracle connection:
public static void getOracleConnection(String hostName,
String sid,
String userName,
String password,
String port)
throws ClassNotFoundException, SQLException {
//Syntax: "jdbc:oracle:thin:@localhost:1521:db11g"
String connectionURL = "jdbc:oracle:thin:@"
+ hostName + ":"
+ port + ":"
+ sid;
Connection conn = DriverManager.getConnection(connectionURL, userName, password);
System.out.println("--- ORACLE database connected ---");
}
Step:2
Send Queries to the Database and retrieve the data.
//Statement Object to send queries
Statement stmt = con.createStatement();
//send the query to database use execute query and store the results in the Result Set object
ResultSet rs = stmt.executeQuery(select * from employee;);
Process the results.
No comments:
Post a Comment