L
L
Loligan2015-08-19 21:43:20
Java
Loligan, 2015-08-19 21:43:20

How to connect MySQL driver to a project in IDEA?

In general, I'm trying to connect to the MySQL database through the program code:
program code

packagecom.company;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws SQLException {
sql_class.sql_query();
}
}
class sql_class{
public static void sql_query() throws SQLException {
Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testphones","admin", "admin");
Statement st = cn.createStatement();
}
}

but it gives an error in the console:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testphones
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager. getConnection(DriverManager.java:247)
at com.company.sql_class.sql_query(Main.java:16)
at com.company.Main.main(Main.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com. intellij.rt.execution.application.AppMain.main(AppMain.java:140)

The book says that you need to drop the driver into the project folder in the lib folder. I downloaded the driver from the Internet but nothing helped.
PS MySQL installed via Denwer.
PPS connected via DataSourse fine but connection via code doesn't work
ef449e4d50924067bb71ee46e599ecf0.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Emin, 2015-08-19
@Loligan

You need to open the Project Structure, find the downloaded mysql driver from the libraries section and add it as a library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question