【DM】教你用JDBC连接达梦数据库并进行增删改查

JDBC连接达梦

平台及应用软件:Win10+JDK1.8+DM7

连接测试

class Connec{
    public Connection getConn(){
        Connection conn=null;
        String name="dm.jdbc.driver.DmDriver";
        String url="jdbc:dm://127.0.0.1:5237";
        String user="SYSDBA";
        String password="wf1105tm0306";
        try{
            Class.forName(name);
            conn=DriverManager.getConnection(url,user,password);
            System.out.println("数据库连接成功");
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }
}

在这里插入图片描述

建表测试

class Table extends Connec{
    public void create() throws SQLException{
        String sql_1="DROP TABLE TEST;";
        String sql_2="CREATE TABLE TEST(C1 INT,C2 VARCHAR(50));";
        Connection testcon=getConn();
        Statement state=testcon.createStatement();
        try{
            state.executeUpdate(sql_2);
        }catch(SQLException e){
            state.executeUpdate(sql_1);
            state.executeUpdate(sql_2);
        }
        System.out.print("建表成功");
        state.close();
        testcon.close();
    }
}

在这里插入图片描述

在这里插入图片描述

插入测试

public void insert() throws SQLException{
        String sql="INSERT INTO TEST VALUES(1,'LALALA')";
        Connection testcon=getConn();
        PreparedStatement pre=testcon.prepareStatement(sql);
        try{
            pre.executeUpdate();
        }catch(SQLException e){
            System.out.print(e);
        }
        System.out.print("插入成功");
    	pre.close();
        testcon.close();
    }

在这里插入图片描述

在这里插入图片描述

修改测试

public void update() throws SQLException{
        String sql="UPDATE TEST SET C2='HAHAHA' WHERE C1 =1";
        Connection testcon=getConn();
        PreparedStatement pre=testcon.prepareStatement(sql);
        try{
            pre.executeUpdate();
        }catch(SQLException e){
            System.out.print(e);
        }
        System.out.print("修改成功");
        pre.close();
        testcon.close();
    }

在这里插入图片描述

在这里插入图片描述

查询测试

public void select() throws SQLException{
        String sql="SELECT * FROM TEST;";
        Connection testcon=getConn();
        Statement state=testcon.createStatement();
        ResultSet res=state.executeQuery(sql);
        while(res.next()){
            System.out.println(
                res.getInt(1)+","+
                res.getString(2)
            );
        }
    }

在这里插入图片描述

删除测试

public void delete() throws SQLException{
        String sql="DELETE FROM TEST WHERE C1=1";
        Connection testcon=getConn();
        PreparedStatement pre=testcon.prepareStatement(sql);
        try{
            pre.executeUpdate();
        }catch(SQLException e){
            System.out.print(e);
        }
        System.out.print("删除成功");
        pre.close();
        testcon.close();
    }

在这里插入图片描述
在这里插入图片描述

完整代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.*;
import java.sql.*;
import java.util.Date;

import javafx.scene.control.Tab;

class Connec{
    public Connection getConn(){
        Connection conn=null;
        String name="dm.jdbc.driver.DmDriver";
        String url="jdbc:dm://127.0.0.1:5237";
        String user="SYSDBA";
        String password="wf1105tm0306";
        try{
            Class.forName(name);
            conn=DriverManager.getConnection(url,user,password);
            System.out.println("数据库连接成功");
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }
}

class Table extends Connec{
    public void create() throws SQLException{
        String sql_1="DROP TABLE TEST;";
        String sql_2="CREATE TABLE TEST(C1 INT,C2 VARCHAR(50));";
        Connection testcon=getConn();
        Statement state=testcon.createStatement();
        try{
            state.executeUpdate(sql_2);
        }catch(SQLException e){
            state.executeUpdate(sql_1);
            state.executeUpdate(sql_2);
        }
        System.out.print("建表成功");
        state.close();
        testcon.close();
    }

    public void insert() throws SQLException{
        String sql="INSERT INTO TEST VALUES(1,'LALALA')";
        Connection testcon=getConn();
        PreparedStatement pre=testcon.prepareStatement(sql);
        try{
            pre.executeUpdate();
        }catch(SQLException e){
            System.out.print(e);
        }
        System.out.print("插入成功");
        pre.close();
        testcon.close();
    }

    public void update() throws SQLException{
        String sql="UPDATE TEST SET C2='HAHAHA' WHERE C1 =1";
        Connection testcon=getConn();
        PreparedStatement pre=testcon.prepareStatement(sql);
        try{
            pre.executeUpdate();
        }catch(SQLException e){
            System.out.print(e);
        }
        System.out.print("修改成功");
        pre.close();
        testcon.close();
    }

    public void delete() throws SQLException{
        String sql="DELETE FROM TEST WHERE C1=1";
        Connection testcon=getConn();
        PreparedStatement pre=testcon.prepareStatement(sql);
        try{
            pre.executeUpdate();
        }catch(SQLException e){
            System.out.print(e);
        }
        System.out.print("删除成功");
        pre.close();
        testcon.close();
    }

    public void select() throws SQLException{
        String sql="SELECT * FROM TEST;";
        Connection testcon=getConn();
        Statement state=testcon.createStatement();
        ResultSet res=state.executeQuery(sql);
        while(res.next()){
            System.out.println(
                res.getInt(1)+","+
                res.getString(2)
            );
        }
    }
}

public class main {
    public static void main(String[] args) {
        Table table=new Table();
        try{
            // table.create();   
            // table.insert(); 
            // table.update(); 
            // table.select();
            table.delete();
        }catch(SQLException e){
            System.out.println(e);
        }
        
    }
}

技术总结

  • 首先JDK环境要配置好;
  • 其次,要找着DM自带的JDBC驱动,这个DM安装目录下的Drivers文件夹下;
  • 然后,编译java时要把这个驱动文件带上;
  • 最后,要注意编码问题,譬如我在Windows下编译要注明encoding=UTF-8,Linux下理论上不用。

猜你喜欢

转载自blog.csdn.net/qq_42229092/article/details/107613478