오라클 연구 노트 (스물셋) - JDBC 호출 저장 프로 시저 및 일괄 작업

저장 프로 시저를 호출 JDBC

그리고 매개 변수의 반환 값을 사용하여 모드를 나가

//存储过程为sum_sal(deptno department.deptno%type,sum in out number)
CallableStatement cs =conn.prepareCall("{call sum_sal(?,?)}"); 
cs.setInteger(1,7879);
cs.setDouble(2,0.0);//第二个传什么都无所谓,因为第二个参数是in out模式,是作为输出的
cs.registerOutParameter(2,java.sql.Types.Double,2);//最后那个参数是保留小数点2位
cs.excute();//执行会返回一个boolean结果

//获得结果,获取第二个参数
double result = cs.getDouble(2);

반환 결과는 오라클을 설정

//存储过程为list(result_set out sys_refcursor, which in number)
CallableStatement cs =conn.prepareCall("{call list(?,?)}"); 
cs.setInteger(2,1);
cs.registerOutParameter(1,racleTypes.CURSOR);
cs.execute();
//获得结果集
ResultSet rs = (ResultSet)cs.getObject(1);

일괄 작업

대량 삽입

사람들은 두 테이블, ID와 이름뿐만 아니라 해당 엔티티 클래스 사람들
은 명령문 실행이 실패한 경우 존재하므로 일괄 작업은 트랜잭션에 배치해야합니다.

public int[] insetBatch(List<People> list) {
    try (Connection connection = JdbcUtil.getConnection();
         PreparedStatement ps = connection.prepareStatement("insert into PEOPLE values (?,?)");
    ) {
        // 关闭事务自动提交,手动提交
        connection.setAutoCommit(false);
        //从list中取出数据
        for (People people : list) {
            ps.setInt(1, people.getId());
            ps.setString(2, people.getName());
            //加入到指语句组中
            ps.addBatch();
        }
        int[] recordsEffect = ps.executeBatch();
        // 提交事务
        connection.commit();
        return recordsEffect;

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

대량 삽입 테스트

public static void main(String[] args) {
    List<People> list = new ArrayList<>();
    int id = 1;
    list.add(new People(id++, "james"));
    list.add(new People(id++, "andy"));
    list.add(new People(id++, "jack"));
    list.add(new People(id++, "john"));
    list.add(new People(id++, "scott"));
    list.add(new People(id++, "jassica"));
    list.add(new People(id++, "jerry"));
    list.add(new People(id++, "marry"));
    list.add(new People(id++, "alex"));

    int[] ints = new BatchTest().insetBatch(list);

    System.out.println(Arrays.toString(ints));
}

일괄 업데이트

public int[] updateBatch(List<People> list) {
    try (Connection connection = JdbcUtil.getConnection();
         PreparedStatement ps = connection.prepareStatement("undate people set name=? where id=?");
    ) {
        // 关闭事务自动提交,手动提交
        connection.setAutoCommit(false);
        //从list中取出数据
        for (People people : list) {
            ps.setInt(1, people.getId());
            ps.setString(2, people.getName());
            //加入到指语句组中
            ps.addBatch();
        }
        int[] recordsEffect = ps.executeBatch();
        // 提交事务
        connection.commit();
        return recordsEffect;

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

일괄 삭제

public int[] updateBatch(List<People> list) {
    try (Connection connection = JdbcUtil.getConnection();
         PreparedStatement ps = connection.prepareStatement("delete people where id=?");
    ) {
        // 关闭事务自动提交,手动提交
        connection.setAutoCommit(false);
        //从list中取出数据
        for (People people : list) {
            ps.setInt(1, people.getId());
            //加入到指语句组中
            ps.addBatch();
        }
        int[] recordsEffect = ps.executeBatch();
        // 提交事务
        connection.commit();
        return recordsEffect;

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

추천

출처www.cnblogs.com/kexing/p/10990879.html