You need to use port number 3306 in the URL. The syntax is as follows -
jdbc:mysql://localhost:3306
import java.sql.Connection; import java.sql.DriverManager; public class MySQLConnectionToJava { public static void main(String[] args) { String JDBCURL="jdbc:mysql://localhost:3306/sample?useSSL=false"; Connection con=null; try { con = DriverManager.getConnection(JDBCURL,"root","123456"); if(con!=null) { System.out.println("MySQL connection is successful with port 3306."); } } catch(Exception e) { e.printStackTrace(); } } }
MySQL connection is successful with port 3306.
The above is the detailed content of How to establish MySQL connection in JAVA? What is the port number to set on localhost?. For more information, please follow other related articles on the PHP Chinese website!