我的github

提前请确保已经安装了postgresql相关的驱动包

import java.sql.*;
 
public class PostgresConnectionExample {
    public static void main(String[] args) throws SQLException {
        // 定义数据库连接信息
        String url = "jdbc:postgresql://localhost:5432/mydatabase";
        String username = "your_username";
        String password = "your_password";
        
        try (Connection conn = DriverManager.getConnection(url, username, password)) {
            System.out.println("成功连接到PostgreSQL数据库!");
            
            // 在此处添加对数据库的查询、更新等操作
            
        } catch (SQLException e) {
            System.err.println("无法连接到PostgreSQL数据库:" + e.getMessage());
        }
    }
}

参考:https://blog.csdn.net/weixin_60414376/article/details/123467598

posted on 2024-01-16 10:57  XiaoNiuFeiTian  阅读(173)  评论(0编辑  收藏  举报