I wrote a program where values are coming from sql database.Now i want to send those values to frontend using selenium. say it is an pincode i fetched from Database but now if locate the element where i want to store that fetched otp how will I do?.sendkeys in selenium is not accepting Piece of code I wrote :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class JavaConnect2SQL {

    public static void main(String[] args) throws ClassNotFoundException {
        try {
        // TODO Auto-generated method stub
//String url="jdbc:sqlserver://LAPTOP-F0E1C4GT\\SQLEXPRESS;databaseName=BikeStores;";
//String user="sa";
//String password="NewPassword@123";

String url = "jdbc:sqlserver://localhost;user=sa;password=NewPassword@123;databaseName=BikeStores";

    Connection connection=DriverManager.getConnection(url);
    Statement statement = connection.createStatement();

    String query = "select  TOP 1 pincode from pintable";
    ResultSet rs = statement.executeQuery(query);
        WebDriver driver=new ChromeDriver();
        driver.findElement(By.linkText("Personal Loan")).click();
        //Belowline->Is my confusion??
driver.findElement(By.name("pincode")).sendKeys("122018");//this 122018 i am writing by sendkeys but i want this to be filled from SQL itself

} }catch (SQLException e) {
    // TODO Auto-generated catch block
    System.out.println("Oops,there is error");

PLease help and suggest


2 Answers 2


Is this what you looking for

  • 1
    @ Ravneet kaur, we should upvote an answer if it helped. Jan 16, 2021 at 10:04
  • 1
    @RavneetKaur you don't have enough reputation you accound accept the answer by clicking the tick sign near to answer . Once you have enough reputation upvote and accept the answer :) if it helps Vishal Thank you .. :)
    – PDHide
    Jan 16, 2021 at 10:06
  • yes sure vishal Jan 17, 2021 at 7:05

I would also suggest to make it less messy:

  • Make few generic db connection/retrieval functions
  • if data retrieval is not dynamic , create separate script to populate a data source like an file: json, csv or excel to further use directly in simpler way in tests.
  • i will try for this Jan 17, 2021 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.