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
Release: 2023-08-26 11:17:06
forward
871 people have browsed it

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
Copy after login

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

Output

MySQL connection is successful with port 3306.
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template