26

I'm new in Java, and I need to establish a connection to a MySQL server (local), I have add the libraries in Intellij idea but it seems not work, the IDE can't find the class i think... I become crazy I'm searching since two hours... I come from visual studio/c# dev environment and i think that i should miss something...

Here you can have a pic from my IDE and the simple code that I wanted use. You can also deduce that I have import the jar in my project (mysql-jdbc).

IDE pic

Edit : here is the code, the comment show where the error appear :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import org.*;
import com.mysql.jdbc.Driver;


public class JdbcLogin {
    public String Login;
    public String MotDePasse;
    private boolean Logged = false;

    public void StartBdd(){
        String driverName = "com.mysql.jdbc.Driver";
        Class.forName(driverName); // here is the ClassNotFoundException

        String serverName = "localhost";
        String mydatabase = "suptodo";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase;

        String username = "root";
        String password = "azerty";
        Connection connection = DriverManager.getConnection(url, username, password);
    }
}
Nimp
  • 441
  • 1
  • 5
  • 15
  • Put your code in the question, and format it, don't put pictures of the IDE. – RealSkeptic Jun 04 '15 at 18:56
  • thank you for your help, i have edit with the post i have think that a pic would be better, so shoud i delete the import line ? its just written on mouseover "unused import statement"... thank again ! – Nimp Jun 04 '15 at 19:13
  • In modern JDBC you are not supposed to have that line (`Class.forName...`) at all. You need to have the jar in the classpath, though. – RealSkeptic Jun 04 '15 at 19:14
  • 1
    You can leave link to picture as additional resource, but it can't be only resource. Also add error message. Anyway why do you need `import org.*;` or `import com.mysql.jdbc.Driver;` at all? – Pshemo Jun 04 '15 at 19:16
  • i wanted to be sure that it was not the source of my problem, i have delete this line now but i still have the same problem :( – Nimp Jun 04 '15 at 20:45
  • 1
    The classpath of intellij is not the same as the classpath of your project/application. Make sure you added the library to the build path of your project! – Mark Rotteveel Jun 05 '15 at 08:59

5 Answers5

52

It's easy to configure. First just open the IntelliJ IDE and follow this simple step:

File->Project Structure->Libraries

Then click on the plus(+) sign and select From Maven.... enter image description here

After you'll get a search box. There you should put:

mysql:mysql-connector-java:5.1.40

enter image description here

This will solve the issue.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Pronab Roy
  • 1,058
  • 1
  • 14
  • 18
  • This IS all you need to do, for those coming from a C# background don't look for a namespace called mysql - this ADDS classes/drivers etc. into the existing java.sql namespace to add connection functionality for mysql. You might be better off starting writing some mysql test code and when it fails to connect add the above library. – LordWabbit Aug 29 '21 at 15:11
35

You have to add 'mysql:mysql-connector-java:5.1.40' from maven or add it as java library as shown:

enter image description here

Mohammed Mukhtar
  • 1,731
  • 20
  • 18
6

Solution#1:Drop the mysql-connector-java-version-bin.jar file to your project, right click on it, select "Add as library".

Solution#2:Build and run with command line, for example(Windows)

  • javac yourclassname
  • java -cp".;mysql-connector-java-version-bin.jar" yourclassname
renzherl
  • 161
  • 1
  • 3
3

If it says time zone unrecognized you can add this piece of code to the URL:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost?useTimezone=true&serverTimezone=UTC","USER-NAME","PASSWORD");

Don't forget to wrap it with a try-catch.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mubarak Tahir
  • 207
  • 3
  • 4
0

As an Update this will work with mysql:mysql-connector-java:8.0.18 as the other version will generate a new problem and won't connect to the database server.