Java(120):Java通过jdbc给DB2二进制字段类型插入和查询数据

Java(120):Java通过jdbc给DB2二进制字段类型插入和查询数据

1、表

CREATE TABLE dee_blob ( 
  id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1 ),
  CIPHER_DE BLOB,
  SIMULATION_RE_DE BLOB,
  SM3_DE BLOB,
  PRIMARY KEY (id)
  )

2、Java代码插入和查询

import com.sun.org.apache.xpath.internal.objects.XObject;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Description :
 *
 * @author : HMF
 * Date : Created in 16:47 2024/1/29
 * @version :
 */
public class dbBlob {
    public static Connection con;

    public dbBlob() {


        //直接过数据库db2
        String driver = "com.ibm.db2.jcc.DB2Driver";
        String url = "jdbc:db2://10.1.1.38:50000/sample:currentSchema=db2inst1";
        String user = "db2inst1";
        String password = "lianshi";

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("Open DB Connection success");
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static void main(String[] args)  {

        dbBlob db = new dbBlob();
        try{
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String sqlStr= "INSERT INTO db2inst1.DEE_BLOB  (CIPHER_DE,SIMULATION_RE_DE,SM3_DE) values(?,?,?)";
        db.DBExecute(sqlStr);
        db.DBQuery("select * from db2inst1.DEE_BLOB ");

        System.exit(0);
    }

    void DBExecute(String sqlStr) {
        PreparedStatement pstat; //预编译
        try {
            System.out.println("+++++ execute sql: "+sqlStr);
            pstat = con.prepareStatement(sqlStr);
            pstat.setBytes(1, "密文测试".getBytes());
            pstat.setBytes(2, "123456789".getBytes());
            pstat.setBytes(3, "张三".getBytes());
            int result=pstat.executeUpdate();
            pstat.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    void DBQuery(String sqlStr)  {
        try {
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(sqlStr);

            System.out.println("+++++ query sql: "+sqlStr);
            List datalist = new ArrayList();
            while(rs.next()){
                LinkedHashMap mapResult = new LinkedHashMap<>();
                String CIPHER_DE=new String(rs.getBytes("CIPHER_DE")).trim();
                mapResult.put("id",rs.getInt("id"));
                String SIMULATION_RE_DE=new String(rs.getBytes("SIMULATION_RE_DE")).trim();
                mapResult.put("SIMULATION_RE_DE",SIMULATION_RE_DE);
                String SM3_DE=new String(rs.getBytes("SM3_DE")).trim();
                System.out.println(CIPHER_DE+ " "+ SIMULATION_RE_DE +" "+ SM3_DE);
                mapResult.put("SIMULATION_RE_DE",SM3_DE);
                datalist.add(mapResult);
            }
            System.out.println(datalist);
            rs.close();
            statement.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

}

3、结果展示

你可能感兴趣的:(java,数据库,sql)