这是我试过的代码。但每次都显示错误:Could not create connection to database server.代码中的错误是什么?url语法是否正确?
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();
}
}
}
我已经把StackTrace贴在这里了,我得到的Null pointer Exception是什么?
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 [email protected]/com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at [email protected]/com.mysql.jdbc.Util.getInstance(Util.java:387)
at [email protected]/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at [email protected]/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at [email protected]/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at [email protected]/com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at [email protected]/com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2332)
at [email protected]/com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
at [email protected]/com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at [email protected]/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 [email protected]/com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at [email protected]/com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at [email protected]/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 [email protected]/com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3005)
at [email protected]/com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1916)
at [email protected]/com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1845)
at [email protected]/com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
at [email protected]/com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255)
at [email protected]/com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
... 14 more
你的连接url应该是jdbc:mysql:/localhost:3306sakila?autoReconnect=true&useSSL=false。