I want establish a connection with a MySQL database. I downloaded JDBC from the official website of MySQL, and I added the jar file to my Eclipse IDE. However, when I tried to import java.sql package, it's still not accessible.
I don't know where is the problem. I watched some videos talk about database development perspective, shall I add it to my IDE? And do I need to download MySQL server?
CodePudding user response:
The JDBC API itself is included in Java. What you downloaded from the MySQL website is the MySQL Connector/J JDBC driver. You're using a modular project because module-info.java
exists. If you want to use JDBC in a modular project, you will need to add requires java.sql;
to your module-info.java
to give it access to the java.sql
module (which, among others, includes the java.sql
and javax.sql
packages).
If you want to use the MySQL classes directly, you will also need to add an appropriate requires
directive for the MySQL library. However, in general you should not use JDBC driver classes directly, but only use the API from the java.sql
and javax.sql
packages.
Alternatively, delete the module-info.java
to convert to a non-modular Java project.
CodePudding user response:
I cannot reproduce your problem. I did exactly like you and my Eclipse did not show any errors.
One guess would be that your Java modules get in the way and somehow prevent you from importing the classes. Can you try removing the module-info.java
and disabling modules?
Also - very important, you have selected "jre" as your java runtime. Make sure you are using a JDK and not a simple JRE. The latter is used to run Java programs but the former is necessary when you develop them.