D
D
Dmitry Belousov2016-08-20 21:52:45
MySQL
Dmitry Belousov, 2016-08-20 21:52:45

Why is MySql database not working in Maven?

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

There is a dependency, but it throws an error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
here is the connection code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionDateBase {

    public static final String DB_URL = "jdbc:mysql://localhost:3306/chat";
    public static final String H2_DRIVER = "com.mysql.jdbc.Driver";
    public static final String LOGIN = "root";
    public static final String PASSWORD = "app112";

    public ConnectionDateBase() {
    }

    public Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName(H2_DRIVER);
        return DriverManager.getConnection(DB_URL, LOGIN, PASSWORD);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evhen, 2016-08-21
@dreven

https://yadi.sk/d/sqycbG9SuN7NC
Launched, the only error is that it cannot reach the sql server, since it does not exist

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

but I made changes to the pom.xml file:
since you have a web application, the package must be war, not jar, since the standard jar assembly will not collect dependencies into a package
, I also changed the java version to 1.7, so you are using features from this version
<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

also close connections and database statements + read about connection pool

N
Neonoviiwolf, 2016-08-21
@Neonoviiwolf

private static final String URL = "jdbc:mysql://localhost:3306/testsql?useUnicode=true&useSSL=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";

after these changes that I found in the open spaces of Google, I stopped cursing
when building the project, I also wrote this in pom (in the devkolibri courses that I watched, it was different)
<plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>8.5.0</version>
                <configuration>
                    <mainClass>mainFx.Main</mainClass>
                </configuration>
            </plugin>

<dependency>

            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.3</version>
        </dependency>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question