0

I am trying to learn how to use mysql databases with java. In my searches for knowledge I came across this piece of code from : https://www.javatpoint.com/java-drivermanager-registerdriver-method

import java.sql.Driver;  
import java.sql.DriverManager;  
import java.sql.SQLException;  

public class JavaDriverManagerExample1 {  
    public static void main(String args[]) throws SQLException {   
            Driver d=new com.mysql.jdbc.Driver();  
            DriverManager.registerDriver(d);  
            System.out.println("Driver successfully registered !");  
}  
} 

the problem is that I get a compilation error: com.mysql cannot be resolved to a type.

I tried to install jdbc for ubuntu but for no avail.

I would appreciate any sort of help in this issue. Thank you!

guyr79
  • 169
  • 1
  • 11
  • 1
    Can you try to compile this by providing the classpath java -cp JavaDriverManagerExample1 – ptk Oct 16 '19 at 15:33
  • look here if you can be useful: https://javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html – Nobady Oct 16 '19 at 15:35
  • 1
    You should not load drivers using `new com.mysql.jdbc.Driver` nor should you call `DriverManager.registerDriver()` yourself. It is called by the driver itself when its class is loaded (which since Java 6 happens automatically with a JDBC 4 compliant driver). That tutorial is a bad example. In any case, the error indicates that you don't have the MySQL Connector/J driver on the classpath when compiling. – Mark Rotteveel Oct 16 '19 at 16:13
  • first, thanks to all the helpers. @MarkRotteveel, what do you mean by "the error indicates that you don't have the MySQL Connector/J driver on the classpath when compiling" ? I am afraid I don't really understand how to fix that. I installed the Connector/J driver from the microsoft site. was there anything else I should do? – guyr79 Oct 16 '19 at 20:10
  • @ptk, I will try your suggestion I hope it will work.. – guyr79 Oct 16 '19 at 20:13
  • @nobady, I downloaded the file that is matching my ubuntu version - however, it only shows installation options. I am afraid I don't understand what the meaning of put the jar file in the class path... Thanks again – guyr79 Oct 16 '19 at 20:16
  • When you compile classes with Java, you need to configure the classpath to reference classes not part of Java itself (see the duplicate). Downloading by itself is not sufficient. And I - as far as I know - you can't download the MySQL Connector/J driver from Microsoft, possibly you downloaded the Microsoft SQL Server JDBC driver instead. You may also want to find a good introductory text on Java that explains all this, because this is rather fundamental knowledge you need to have to work with Java. – Mark Rotteveel Oct 17 '19 at 06:01

0 Answers0