简体   繁体   中英

Error occur while connecting to database mysql workbench:java.sql.SQLNonTransientConnectionException: Could not create connection to database server

JDBC connection error using MySQL-connector-java-5.1.44 and mysql workbench 8

import java.sql.Connection;
import java.sql.DriverManager;

public class DB {
    public static Connection getConnection(){
        Connection con=null;
        try{
        Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false");
        }catch(Exception e){System.out.println(e);}
        return con;
    }

}

Username and password needs to be passed to the getConnection() eg.

Connection conn = null; 
 String url = "jdbc:mysql://localhost/"; 
 String dbName = "someDb"; 
 String driver = "com.mysql.jdbc.Driver"; 
 String userName = "root"; String password = "password"; 
 try { Class.forName(driver).newInstance();
       conn = DriverManager.getConnection(url+dbName,userName,password);   System.out.println("Connected to the database"); conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { System.out.println("NO CONNECTION =("); } }

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