App下載
首頁(yè)javaconnectionJava JDBC - 如何連接到遠(yuǎn)程mysql數(shù)據(jù)庫(kù)

Java JDBC - 如何連接到遠(yuǎn)程mysql數(shù)據(jù)庫(kù)

我們想知道如何連接到遠(yuǎn)程mysql數(shù)據(jù)庫(kù)。
import java.sql.Connection;
import java.sql.DriverManager;

public class Main {
  public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost:3306/mydb";
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url, "root", " ");
    System.out.println("Database connection established");
    conn.close();
    System.out.println("Database connection terminated");
  }
}