简体   繁体   中英

Connecting JAVA with MySql using JDBC

This is the code that i have tried. But everytime it shows the Error:Could not create connection to database server.What is the error in the code?Is the url syntax correct?

import java.sql.*;
public class Second {
    public static void main(String[] args) {
        Connection con = null;
        try {
            String url = "jdbc:mysql://localhost:3306/sakila";
            String user = "root";
            String password = "1234";
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, password); 
            System.out.println("Connection is established.");
            con.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

I've posted the StackTrace here.What is the Null pointer Exception I am getting?

Wed May 13 09:19:35 IST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.Util.getInstance(Util.java:387)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2332)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at JDBC.Second.main(Second.java:12)
Caused by: java.lang.NullPointerException
    at mysql.connector.java@5.1.38/com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3005)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1916)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1845)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255)
    at mysql.connector.java@5.1.38/com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
    ... 14 more

Your connection url should be jdbc:mysql://localhost:3306/sakila?autoReconnect=true&useSSL=false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM