Connecting to DataBase using Selenium WebDriver


Selenium Webdriver Script to connect postgresql database.

package selenium_examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
 public static void main(String[] args) throws ClassNotFoundException, SQLException {
  Class.forName("org.postgresql.Driver");
  Connection con = DriverManager.getConnection(dbUrl,username,password);
                //dbUrl="jdbc:postgresql://hostname:port/dbname"
  System.out.println("Connected to database");
 
                Statement stmt =con.createStatement();
  ResultSet rs = stmt.executeQuery("select id from emp");
  while (rs.next()) {
  
                int x = rs.getInt("id");
                System.out.println("Employee ids="+ x);
 }
 
  con.close();
  System.out.println("Connection closed");
 }

}

Output:-Displays all employee ids.

 

No comments:

Related Posts Plugin for WordPress, Blogger...