如何在 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.
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP