如何在 JAVA 中创建 MySQL 连接?本地主机需要设置的端口号是多少?


需要在 URL 中使用端口号 3306。语法如下 −

jdbc:mysql://:3306

示例

import java.sql.Connection;
import java.sql.DriverManager;
public class MySQLConnectionToJava {
   public static void main(String[] args) {
      String JDBCURL="jdbc:mysql://: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.

更新于:2019 年 12 月 16 日

399 次浏览

开启你的 职业 生涯

完成课程,获得认证

开始
广告
© . All rights reserved.