Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JDBC/Mysql Connector in intellij idea

Tags:

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);     } } 
like image 269
Nimp Avatar asked Jun 04 '15 18:06

Nimp


People also ask

Can I use JDBC in IntelliJ?

Locate the JDBC driver jar file and press Open. To do this in Intellij, right click on Project folder -> Open module settings, this should open a new window. Go to libraries under Project settings and click on add to locate and add the JDBC jar files to the project.

How add JDBC jar to IntelliJ?

In the file browser, navigate to the JAR file of the JDBC driver, select it, and click OK. In the Class field, specify the value that you want to use for the driver . Click Apply.


2 Answers

It's easy to configure first just open the InteliJ IDE and follow this simple step

File->Project Structure->Libraries 

Then Click on the plus(+) sign and select From Meven.... 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

like image 125
Pronab Roy Avatar answered Nov 02 '22 23:11

Pronab Roy


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

like image 25
Mohammed Mukhtar Avatar answered Nov 02 '22 22:11

Mohammed Mukhtar