package com.Modules;

import java.sql.*;

public class DMTDMTest {
    public static void main(String[] args) {
        // 连接信息
        String url = "jdbc:postgresql://139.9.39.176:16502/dameng";
        String user = "SYSDBA";
        String password = "Sysdba12345";   // 如果已修改密码，请替换为实际密码

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            // 1. 加载驱动（JDBC 4.0 后自动加载，但显式加载更稳妥）
            Class.forName("dm.jdbc.driver.DmDriver");
            System.out.println("达梦 JDBC 驱动加载成功");

            // 2. 建立连接
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("数据库连接成功！");

            // 3. 执行查询（达梦支持 DUAL 表）
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT 1 FROM DUAL");

            if (rs.next()) {
                int result = rs.getInt(1);
                System.out.println("查询结果: " + result);
            }

            // 4. 可选：查询当前时间
            rs.close();
            rs = stmt.executeQuery("SELECT SYSDATE FROM DUAL");
            if (rs.next()) {
                Timestamp now = rs.getTimestamp(1);
                System.out.println("服务器当前时间: " + now);
            }

        } catch (ClassNotFoundException e) {
            System.err.println("未找到达梦 JDBC 驱动，请检查 DmJdbcDriver18.jar 是否在 classpath 中");
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("数据库连接或操作失败");
            e.printStackTrace();
        } finally {
            // 5. 关闭资源
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }
}