eclipse程序无法连接到mysql数据库

6tr1vspr  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(316)

我刚刚安装了mysql服务器,在ubuntu上用终端制作了一个数据库,服务器正常工作,数据库显示正常,表显示正常。但是当我尝试在eclipse中运行程序时,它抛出了一个异常。首先是一个ssl异常,我用?autoreconnect=true&usessl=false修复了这个异常。所以现在,我得到了另一个例外。它说它无法连接到数据库,三次尝试后就放弃了。我也尝试过这个论坛的一些解决方案,但是没有成功。。。我使用的驱动程序版本5.1.42。有什么解决办法吗?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MySql {

public static void main(String[] args) throws Exception {

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Imenik?autoReconnect=true&useSSL=false","root","******");
    PreparedStatement ps = conn.prepareStatement("select * from imenik");
    ResultSet result = ps.executeQuery();
    while(result.next()) {
        System.out.println(result.getString(1) + "   " +result.getString(2) + "   " +result.getString(3) );

    }
}}

这是个例外:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2104)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2029)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at MySql.main(MySql.java:11)
Caused by: java.sql.SQLException: Unknown system variable 
'query_cache_size'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2444)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1381)
at com.mysql.jdbc.ConnectionImpl.loadServerVariables(ConnectionImpl.java:3766)
at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3229)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2067)
... 13 more
a8jjtwal

a8jjtwal1#

这可能是您给定的数据库密码的问题,您可以使用以下解决方案:

Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Imenik?autoReconnect=true&useSSL=false","root","root");
PreparedStatement ps = (PreparedStatement) conn.prepareStatement("select * from imenik");
ResultSet result = ps.executeQuery();
while(result.next()) {
    System.out.println(result.getString(1) + "   " +result.getString(2) + "   " +result.getString(3) );

}

使用mysql-connector-java-6.0.6.jar测试

相关问题