0
error: package com.mysql.cj.jdbc does not exist
import com.mysql.cj.jdbc.MysqlDataSource;

java version: jdk 16 mysql version: 8.0 mysql driver version: 8.0.26

I already put the java connector along with the program files but I still have this error. Here's my code:

import com.mysql.cj.jdbc.MysqlDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class conn {

    private static String servername = "localhost";
    private static String username = "root";
    private static String dbname  = "station_db";
    private static Integer portnumber  = 3306;
    private static String password = "";
    
    public static Connection getConnection()
    {
        Connection con = null;
        
        MysqlDataSource datasource = new MysqlDataSource();
        
        datasource.setServerName(servername);
        datasource.setUser(username);
        datasource.setPassword(password);
        datasource.setDatabaseName(dbname);
        datasource.setPortNumber(portnumber);
        
        try {
            con = datasource.getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(" Get Connection -> " + con.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return con;
    }
    
}
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
LokuDesu
  • 3
  • 2

1 Answers1

0

You need to list the version of JAVA, mySql, and the mySql driver.

This could be a classpath issue or incompatible versions between the 3 elements listed above or a typo in variable names. Any one of these three should throw this error.

Here is a basic tutorial for JDBC so you can review if you set things up properly.

James Drinkard
  • 15,342
  • 16
  • 114
  • 137