2

enter image description hereI am connected mysql databse in eclipse.i am trying to test enter ui value is match in databse or not so i have write code for that but i am not getting satisfied output My code is listed below:

import java.io.IOException;
import java.sql.*;

import jxl.write.WriteException;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.TestNGException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Database {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
     static final String DB_URL = "jdbc:mysql://localhost:3306/test";

     static final String USER = "root";
     static final String PASS = "";

    // Connection con;
    static Statement smt = null;

    static WebDriver driver;
    ResultSet set;

    @BeforeClass
    public static void setup() throws TestNGException, IOException, WriteException
    {
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium Jar\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-extensions");
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
    }

    @Test
    public void start() throws SQLException
    {

        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Connecting to a selected database...");
              conn = DriverManager.getConnection(DB_URL, USER, PASS);
              System.out.println("Connected database successfully...");

              String sql;
              smt= conn.createStatement();
              sql="select * from users";
              ResultSet set=smt.executeQuery(sql);

              while(set.next())
              {
                  int id=set.getInt("ID");
                  String username=set.getString("user_login");
                  String password=set.getString("user_pass");


                  System.out.println("ID->"+id);
                  System.out.println("username->"+username);
                  System.out.println("password->"+password);

              }
              set.close();

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        driver.get("http://example.com");

        String uname="ghgfhgj";
        String upass="12Abcd";

        WebElement username = driver.findElement(By.name("log"));
        username.clear();
        username.sendKeys(uname);

        WebElement pass = driver.findElement(By.name("pwd"));
        pass.clear();
        pass.sendKeys(upass);

        WebElement submit = driver.findElement(By.name("Submit"));
        submit.click();

          String newuserquery = "SELECT * From users where users.user_login=ghgfhgj";
         PreparedStatement stat = conn.prepareStatement(newuserquery);
          stat.setString(1, uname);

          ResultSet set1=stat.executeQuery();
          while(set1.next())
          {
              String newusername = set1.getString("user_login");

                System.out.println("newusername->"+newusername);

                    Assert.assertEquals(uname, newusername);

                   }

          }


    }
  • Tell us what is the output that you are willing to have. – ChathuD Jan 11 '17 at 14:52
  • What result do you get? – Kate Paulk Jan 11 '17 at 20:30
  • i have add screenshot please check – Rutvi Soni Jan 12 '17 at 10:29
  • I voted to close as off-topic because you wil find answers (there were already such questions) on stackoverflow.com – dzieciou Jan 12 '17 at 10:55