Reputation:
I just instaled MySQL server and made one database using Terminal- on ubuntu, and the server works, my database is shown properly and my table is shown also properly. But when I try to run a program in eclipse, it trows an exception. First was a SSL exception which I fixed with ?autoReconnect=true&useSSL=false. So now, I get another exception. It says it can not connect to database and gives up after three tries. I also tried some solutions from this forum, but with no success. ... Im using drivers version 5.1.42. Any solutions?
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) );
}
}}
And this would be an exception:
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
Upvotes: 0
Views: 1035
Reputation: 2823
It might be problem of your given password for the database, you can use below solution:
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) );
}
tested with mysql-connector-java-6.0.6.jar
Upvotes: 1