如何在 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.