2

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.
5
  • This issue mostly occured when mysql service is not started. start mysql service and start connection Commented Feb 1, 2018 at 5:55
  • Started in Services and its running too but getting the same error Commented Feb 1, 2018 at 5:59
  • ok try one more thing remove port number make it as jdbc:mysql://localhost/ Commented Feb 1, 2018 at 6:04
  • thanks Engr,But the result is same error Commented Feb 1, 2018 at 6:10
  • follow this i hope this helps, stackoverflow.com/questions/6865538/… Commented Feb 1, 2018 at 6:11

1 Answer 1

4

Try adding this to your connection String:

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

it worked for me

Sign up to request clarification or add additional context in comments.

1 Comment

I already had my connection string (spring.datasource.url) quite close to that, except for the timezone which shouldn't matter. What solved it for me was to append &createDatabaseIfNotExist=true to the connection string! hope this helps someone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.