Home >Java >javaTutorial >How to establish MySQL connection in JAVA? What is the port number to set on localhost?

How to establish MySQL connection in JAVA? What is the port number to set on localhost?

WBOY
WBOYforward
2023-08-26 11:17:06828browse

How to establish MySQL connection in JAVA? What is the port number to set on localhost?

You need to use port number 3306 in the URL. The syntax is as follows -

jdbc:mysql://localhost:3306

Example

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();
      } 
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete