Bạn cần sử dụng số cổng 3306 trong URL. Cú pháp như sau -
jdbc:mysql://localhost:3306
Ví dụ
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(); } } }
Đầu ra
MySQL connection is successful with port 3306.