D
D
Dmitry Volokitin2020-01-20 22:26:19
MySQL
Dmitry Volokitin, 2020-01-20 22:26:19

Not connecting connection = getConnection(URL, USERNAME, PASSWORD);. How to connect the database to the environment?

I connect the database. If you comment out the line connection = getConnection(URL, USERNAME, PASSWORD);, everything works. I can't figure out what's wrong.
PS I follow the model, because there were a lot of mistakes, I work with several reference books, but I can’t fix it

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>testSQL</artifactId>
    <version>1.0-SNAPSHOT</version>

 <dependencies>
     <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.31</version>
     </dependency>

 </dependencies>


</project>

import com.mysql.fabric.jdbc.FabricMySQLDriver;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

import static java.sql.DriverManager.getConnection;

public class Main {

    public static final String URL = "jdbc:mysql://localhost:3306/test";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "0000";

    public static void main(String[] args){

        Connection connection;

        try { //подключение драйвера MySQL
            Driver driver;
            driver = new FabricMySQLDriver();

              DriverManager.registerDriver(driver);


            connection = getConnection(URL, USERNAME, PASSWORD);
            System.out.println("FabricMySQLDriver и DriverManager работают! ");

        } catch (SQLException e) {
            System.out.println("Не удалось загрузить FabricMySQLDriver ");
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-01-20
@Terran37

If you remove the line, then you will get a message, but not the fact of the connection itself. And what are the mistakes?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question