简体   繁体   中英

.MySQLNonTransientConnectionException: Could not create connection to database server

Getting Error For the Following Code

  package jdbcdrive;

import java.sql.*;
public class Lpt {

    /**
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lpt?autoReconnect=true&useSSL=false","root","2664");

        Statement st = con.createStatement();

        ResultSet rs = st.executeQuery("SELECT * FROM sys.lpt");

        while(rs.next()) {

            System.out.println(rs.getInt("BatchName")+" "+rs.getString("StudentName")+" "+rs.getString("StudentName")+" "+rs.getString("studentMobileNo")+" "+rs.getString("Address"));
        }

    }

}

The Output Error I'm getting is

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Try adding this to your connection String:

"useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC" 

it worked for me

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