Development environment

intellij idea community 

I need to work with mySQL database. Because I have a community version, I had to download a plugin

 DB Navigator 

Now I am writing code

 import java.sql.*; public class Main { public static void main(String[] args) { Connection connection; try { Driver driver = FabricMySQLDriver(); } catch (SQLException se) { System.out.println("Ошибка загрузки драйвера"); } } } 

and the FabricMySQLDriver line FabricMySQLDriver highlighted in red. Judging by Google, this code is outdated and needs another. Help to understand or give a link to the material.

  • At a minimum, you need to replace FabricMySQLDriver with new FabricMySQLDriver() . In general, as an option, you can try the method described in this article. - post_zeew
  • FabricMySQLDriver typo (corrected). IDEA does not understand this code - DriveMind Novel

1 answer 1

To connect to the database, you need to use DriverManager.getConnection .

Example of use:

 import java.sql.*; public class Main { public static void main(String[] args) { try { Connection connection = DriverManager.getConnection("jdbc:..."); } catch (SQLException se) { System.out.println("Ошибка загрузки драйвера"); } } } 

Also, in order to work with J D BC, you do not need IntelJ Idea, nor any plug-ins.

Example JDBC link: jdbc: mysql: // localhost: 3306 /

If you still want to work directly with the drivers, then you have two problems:

  1. You do not have the required library
  2. You have not connected the desired class file.
  • Could you explain to me about installing the driver. I need to download the jar. and put it in the root directory of the project? - Roman DriveMind
  • Yes, you need to download it. But it needs to be attached to the project as a library. To make life easier for programmers, there is a gradle. He can do it for you: 1. download 2. compile 3. make a finished project of an idea. Also, you probably need something that is not a fabric driver, but simple. MySQL Fabric for clusters. - Qertu Uerty
  • driver won. There is a connection, but a warning is issued Sun Nov 20 00:07:53 EAT 2016 WARN: Establishment of SSL connection. According to MySQL 5.5.45+, 5.6.26+, and 5.7.6+ requirements. For compliance with existing applications, it is not SSL to verifyServerCertificate property is set to 'false'. SetSSL = false, or setSSL = true and provide for server certificate verification. - The novel DriveMind
  • If you are not going to use an SSL connection with your database, you can add to the link? UseSSL = false. If you still want to set up SSL, there is a good and official tutorial dev.mysql.com/doc/connector-j/5.1/en/… - Qertu Uerty