0

Code snippet:

package dbIntegrationwithJDBC;

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 Example1 {
    public static void main(String[] args) throws SQLException, InterruptedException {
        String host = "localhost";
        String port = "3306";

        Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/mastro1729", "root",
                "mysql123");
        Statement s = con.createStatement();
        ResultSet rs = s.executeQuery("select*from credentials where Scenario='mastro1729'");
        System.out.println(rs.getString("Username"));
        System.out.println(rs.getString("Password"));

        while (rs.next()) {

            WebDriver driver = new ChromeDriver();
            driver.get("https://login.salesforce.com");

            driver.findElement(By.name("usernameOrEmail")).sendKeys(rs.getString("Username"));
            driver.findElement(By.xpath("//button[text()='Continue']")).click();
            Thread.sleep(3000);

            driver.findElement(By.name("password")).sendKeys(rs.getString("Password"));
            driver.findElement(By.xpath("//button[text()='Log In']")).click();
            Thread.sleep(3000);

        }
    }
}

Error message:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mastro1729
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at dbIntegrationwithJDBC.Example1.main(Example1.java:23)

I'm trying to connect to a MySQL database with JDBC API. I created the MySQL database with mastro1729 on localhost. My code seems correct and I have added mysql-connector-java.jar file to eclipse. While I am trying to integrate my database with JDBC API I am getting java.sql.SQLException at DriverManager.getConnection(); statement. I have tried my best and but I could not be able to resolve the problem.

What am I doing wrong?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Elegant Student
  • 305
  • 2
  • 8
  • 16
  • When you look at the project in Eclipse's "Package Explorer" do you see the jar file listed under "Referenced Libraries"? – Gord Thompson Sep 04 '18 at 13:16
  • 1
    Either you don't have the driver on the runtime classpath, or you are using an extremely old version that doesn't support automatic driver loading. – Mark Rotteveel Sep 04 '18 at 17:04

2 Answers2

1

It seems there is no suitable JDBC driver found in project classpath...

  • Download platform independent MySQL JDBC Driver from here.
  • Extract downloaded .zip
  • Add executable .jar to your project's classpath.
Sodium
  • 1,016
  • 1
  • 9
  • 22
0

You have to provide the mysql connector jar file in the classpath. If you are using maven, import mysql-connector-java dependency.

If not, you can download the binary and provide it in classpath (either via shell or using IDE) See: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-installing-classpath.html

Bajal
  • 5,487
  • 3
  • 20
  • 25