IntelliJ IDEA 如何创建一个 Java 项目
链接: IntelliJ IDEA 如何创建一个 Java 项目
IDEA配置JDBC驱动DM数据库
链接: https://eco.dameng.com/document/dm/zh-cn/start/JAVA_NEW.html
AI改进后的代码
package com.lpb.java;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class DmJdbcTest {// 定义 DM JDBC 驱动串private static final String CNAME = "dm.jdbc.driver.DmDriver";// 定义 DM URL 连接串private static final String URL = "jdbc:dm://localhost:5236";// 定义连接用户名private static final String USERID = "SYSDBA";// 定义连接用户口令private static final String PWD = "SYSDBA";public static void main(String[] args) {Connection con = null;try {// 加载 JDBC 驱动Class.forName(CNAME);// 获取数据库连接con = DriverManager.getConnection(URL, USERID, PWD);// 设置自动提交为 truecon.setAutoCommit(true);System.out.println("[SUCCESS] Connected to database");} catch (ClassNotFoundException e) {System.out.println("[FAIL] Failed to load JDBC driver: " + e.getMessage());} catch (SQLException e) {System.out.println("[FAIL] Failed to connect to database: " + e.getMessage());} finally {// 使用 try-with-resources 或手动关闭连接if (con != null) {try {con.close();} catch (SQLException e) {System.out.println("[FAIL] Failed to close connection: " + e.getMessage());}}}}
}