App下載
首頁(yè)javaconnectionJava JDBC - 如何連接到MySQL數(shù)據(jù)庫(kù)

Java JDBC - 如何連接到MySQL數(shù)據(jù)庫(kù)

我們想知道如何連接到MySQL數(shù)據(jù)庫(kù)。
import java.sql.Connection;
import java.sql.DriverManager;

public class Main {

  public static void main(String[] argv) throws Exception {

    Class.forName("com.mysql.jdbc.Driver");

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    connection = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/dbName", "root", "password");

    if (connection != null) {
      System.out.println("database now!");
    } else {
      System.out.println("Failed to make connection!");
    }
  }
}