DM数据库JDBC配置连接

658 阅读3分钟

前言

JDBC定义了一个跨数据库、跨平台的通用 SQL 数据库 API。DM JDBC 驱动程序是 DM 数据库的 JDBC 驱动程序,它是一个能够支持基本 SQL 功能 的通用应用程序编程接口,支持一般的 SQL 数据库访问。

开发环境

系统环境:window 10 64位 专业版
开发工具:Intellij IDEA 2022.1.3 x64
JDK 版本:JDK 1.8
数据库连接驱动:DmJdbcDriver18

一. 手动配置

1.1 DM数据库驱动版本说明

  1. DmJdbcDriver16 对应 Jdk1.6 及以上环境
  2. DmJdbcDriver17 对应 Jdk1.7 及以上环境
  3. DmJdbcDriver18 对应 Jdk1.8 及以上环境

1.2 导入JDBC jar包

在开发过程中,我们需要手动导入DmJdbcDriver 驱动 jar 包,该 jar 包可以在DM数据库的安装目录下的../drivers/jdbc文件夹中找到,在开发工具中手动导入相依版本的jar包,本文主要介绍在 Intellij IDEA 开发环境下的导入配置方式。

导入jar包 在Intellij IDEA 中点击导航栏的 文件(File)选项,选中 项目结构(Project Structure)点击项目依赖,导入相应的驱动 jar 包。

image.png

image.png

image.png

image.png

image.png

image.png image.png

二. 基于Maven 包管理工具的配置

使用 Maven 包管理工具之前 首先需要配置本地 Maven 仓库,具体的配置过程在这里不作过多赘述,可以访问相关博文:Maven配置教程

2.1坐标导入

在Maven项目中打开pom.xml文件导入相应DMJDBC驱动坐标:

<dependency>
    <groupId>com.dameng</groupId>
    <artifactId>DmJdbcDriver18</artifactId>
    <version>8.1.2.79</version>
</dependency>

image.png

三. 用例测试

3.1 注册驱动初始化连接


private static final String driverName="dm.jdbc.driver.DmDriver";//连接驱动
private  static  final String url="jdbc:dm://localhost:5236?user=SYSDBA&password=123456789";//连接参数
public Connection initConnect(){
    try {
        Class.forName(driverName);//加载连接驱动
        Connection connection =DriverManager.getConnection(url);//获取连接对象
        return connection;
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

3.2 创建test数据表


@Test
//创建数据表测试
public void DM_JDBCCreateTest() throws SQLException {

   Connection connection= initConnect();
    Statement statement= connection.createStatement();
    statement.execute("create table test (c1 int,c2 varchar,c3 timestamp)");
    //释放连接资源
    statement.close();
    connection.close();
}

3.3 插入测试

@Test
//插入数据测试
public void DM_JDBCInsertTest() throws SQLException{
    Connection connection=initConnect();
    String insertSQL="insert into test (c1,c2,c3) values (?,?,?)";
    PreparedStatement prepareStatement= connection.prepareStatement(insertSQL);
    prepareStatement.setInt(1,1);
    prepareStatement.setString(2,"line_1");
    prepareStatement.setTimestamp(3,new Timestamp(System.currentTimeMillis()));
    //执行
    int resultCode=prepareStatement.executeUpdate();
    if(resultCode>0){
        System.out.println("insert new item!");
    }
    //释放连接资源
    prepareStatement.close();
    connection.close();
}
测试用例单行插入数据测试
SQLinsert into test (c1,c2,c3) values (?,?,?)
数据[1,"line_1",new Timestamp(System.currentTimeMillis())]
测试结果数据表test中新增一行数据

3.4 插入多条测试

@Test
//插入数据测试
public void DM_JDBCInsertAnyTest() throws SQLException{
    Connection connection=initConnect();
    String insertSQL="insert into test (c1,c2,c3) values (?,?,?)";
    PreparedStatement prepareStatement= connection.prepareStatement(insertSQL);
    long time =System.currentTimeMillis();
    for(int i=2;i<11;i++){
        prepareStatement.setInt(1,i);
        prepareStatement.setString(2,"line_"+String.valueOf(i));
        prepareStatement.setTimestamp(3,new Timestamp(System.currentTimeMillis()));
        int resultCode=prepareStatement.executeUpdate();
        if(resultCode>0){
            System.out.println("insert new item!");
        }
    }
    System.out.println("插入消耗时间"+(System.currentTimeMillis() - time) / 1000D + "s");
    //释放连接资源
    prepareStatement.close();
    connection.close();
}
测试用例多行插入数据测试
SQLinsert into test (c1,c2,c3) values (?,?,?)
数据C1列值递增2->10,循环插入
测试结果数据表test中新增9行数据,C1列值从2-10

3.5 更新测试

@Test
public void DM_JDBCUpdateTest() throws SQLException {
   Connection connection=initConnect();
   String updateSQL="update test set c2 =? where c1=1";
   PreparedStatement preparedStatement=connection.prepareStatement(updateSQL);
   preparedStatement.setString(1,"line_1_updated");
   int resultCode =preparedStatement.executeUpdate();
   if(resultCode>0){
       System.out.println("update success!");
   }
   preparedStatement.close();
   connection.close();

}
测试用例更新数据测试
SQLupdate test set c2 =? where c1=1
数据null
测试结果C1列为1的元组C2值变更为“line_1_updated”

3.6 查询测试(条件查询/无条件查询)

@Test
public void DM_JDBCSelectByConditionTest() throws SQLException {
    Connection connection=initConnect();
    String selectSQL="select * from test where c1= 7 ";
    Statement statement= connection.createStatement();
    ResultSet resultSet=statement.executeQuery(selectSQL);
    while (resultSet.next()){
        System.out.println(resultSet.getInt("c1")
                +" "+ resultSet.getString("c2")
                +" "+resultSet.getTimestamp("c3")+""+'\n');
    }
    connection.close();
    statement.close();
    resultSet.close();
}

测试用例查询数据测试
SQLselect * from test where c1= 7
数据null
测试结果控制台打印C1值为7的行数据

3.7删除测试

@Test
public void DM_JDBCDeleteTest() throws SQLException {
    Connection connection=initConnect();
    String deleteSQL="delete from test where c1=1";
    PreparedStatement preparedStatement= connection.prepareStatement(deleteSQL);
    int resultCode=preparedStatement.executeUpdate();
    if(resultCode>0){
        System.out.println("delete success!");
    }
    connection.close();
    preparedStatement.close();
}
测试用例删除数据测试
SQLdelete from test where c1=1
数据null
测试结果数据表中C1值为1的行数据被删除

结语

纸上得来终觉浅,绝知此事要躬行。获取更多技术支持可以访问达梦在线服务平台 eco.dameng.com/