import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;

public class DatabaseConnection { private static final String URL = "jdbc:mysql://localhost:3306/database_name"; private static final String USERNAME = "username"; private static final String PASSWORD = "password";

public static void main(String[] args) {
    // Establish a connection to the database
    try {
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        System.out.println("Connected to the database successfully.");
        // Perform database operations here

        // Close the connection
        connection.close();
        System.out.println("Disconnected from the database.");
    } catch (SQLException e) {
        System.out.println("Failed to connect to the database.");
        e.printStackTrace();
    }
}

}

使用 java写一份连接数据库的代码

原文地址: https://www.cveoy.top/t/topic/i4Mr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录