文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

spring中JdbcTemplate操作oracle的存储过程实例代码

2023-05-16 11:41

关注

场景:

使用java代码调用oracle的存储过程,本例使用JdbcTemplate模板类操作.

功能:

方便后续查阅.

1.JdbcTemplate调用存储过程(Procedure)不带返回值

1.1存储过程

CREATE OR REPLACE PROCEDURE PRO_QUERY_INFO_ARGS4(TASK_ID IN NUMBER) IS
BEGIN
  INSERT INTO F_LOG_INFO
    (TASK_ID,
     BEGIN_TIME,
     END_TIME,
     FLAG,
     FAIL_INFO,
     DATA_COUNT,
     TABLE_NAME)
  VALUES
    (TASK_ID, SYSDATE - 1, SYSDATE, '999', '999', 999, 'TABLE_NAME2019');
  COMMIT;
EXCEPTION
  WHEN OTHERS THEN
    ROLLBACK;
END PRO_QUERY_INFO_ARGS4;

1.2代码

public static void doProcedures() {
	String procedures = "{call PRO_QUERY_INFO_ARGS4 ('888')}";
	jdbcTemplate.execute(procedures);
}

2.JdbcTemplate调用存储过程(Procedure)带返回值但值不是集合类型

2.1存储过程

CREATE OR REPLACE PROCEDURE PRO_QUERY_INFO_ARGS3(ARGS     IN VARCHAR2,
                                                 RTNINFO  OUT VARCHAR2,
                                                 ERRORMSG OUT VARCHAR2,
                                                 FAILINFO OUT VARCHAR2) IS
BEGIN
  ERRORMSG := '';
  RTNINFO  := '你输入的ARGS=' || ARGS;
  SELECT FAIL_INFO INTO FAILINFO FROM F_LOG_INFO where TASK_ID = 1;
  COMMIT;
EXCEPTION
  WHEN OTHERS THEN
    ERRORMSG := 'PRO_QUERY_INFO_ARG抛出异常: ' || SQLERRM;
END PRO_QUERY_INFO_ARGS3;

2.2代码

public static void getProceduresResult() {
  String tt2 = (String) jdbcTemplate.execute(
  new CallableStatementCreator() {
  	public CallableStatement createCallableStatement(
  	  	Connection con) throws SQLException {
  	  String procedures = "{call PRO_QUERY_INFO_ARGS3 (?,?,?,?)}";
  	  CallableStatement cs = con.prepareCall(procedures);
  	  
  	  cs.setString(1, "代码调用");
  	  
  	  cs.registerOutParameter(2, OracleTypes.VARCHAR);
  	  cs.registerOutParameter(3, OracleTypes.VARCHAR);
  	  cs.registerOutParameter(4, OracleTypes.VARCHAR);
  	  return cs;
  	}
  }, new CallableStatementCallback() {
  	public Object doInCallableStatement(CallableStatement st)
  	  	throws SQLException, DataAccessException {
  	  st.execute();
  	  
  	  Object tt2 = st.getObject(2);
  	  Object tt3 = st.getObject(3);
  	  Object tt4 = st.getObject(4);
  	  return tt2;
  	}
	});
}

3.JdbcTemplate调用存储过程(Procedure)带返回值且值集合类型

3.1存储过程

CREATE OR REPLACE PROCEDURE PRO_QUERY_INFO_ARGS2(ERRORMSG OUT VARCHAR2,
                                                 CURINFO  OUT SYS_REFCURSOR) IS
BEGIN
  ERRORMSG := '';
  OPEN CURINFO FOR
    SELECT FAIL_INFO, TABLE_NAME FROM F_LOG_INFO;
  COMMIT;
EXCEPTION
  WHEN OTHERS THEN
    ERRORMSG := 'PRO_QUERY_INFO_ARG2抛出异常: ' || SQLERRM;
END PRO_QUERY_INFO_ARGS2;

3.2代码

public static List getProceduresResultList() {
 
 List resultList = (List) jdbcTemplate.execute(
  new CallableStatementCreator() {
  	public CallableStatement createCallableStatement(
  	  	Connection conn) throws SQLException {
  	  
  	  String procedures = "{ CALL PRO_QUERY_INFO_ARGS2(?,?) }";
  	  CallableStatement statement = conn
  	  		.prepareCall(procedures);
  	  
  	  statement.registerOutParameter(1, OracleTypes.VARCHAR);
  	  
  	  statement.registerOutParameter(2, OracleTypes.CURSOR);
  	  return statement;
  	}
  }, new CallableStatementCallback() {
  	public Object doInCallableStatement(
  	  	CallableStatement statement) throws SQLException,
  	  	DataAccessException {
  	  List resultsMap = new ArrayList();
  	  statement.execute();
  	  
  	  ResultSet resultSet = (ResultSet) statement
  	  		.getObject(2);
  	  
  	  while (resultSet.next()) {
  	  	Map rowMap = new HashMap();
  	  	rowMap.put("FAIL_INFO",
  	  			resultSet.getObject("FAIL_INFO"));
  	  	rowMap.put("TABLE_NAME",
  	  			resultSet.getObject("TABLE_NAME"));
  	  	resultsMap.add(rowMap);
  	  }
  	  resultSet.close();
  	  return resultsMap;
  	}
  });
 return resultList;
}

4.附本例使用建表语句

create table F_LOG_INFO
(
  task_id    NUMBER(16) not null,
  begin_time DATE,
  end_time   DATE,
  flag       VARCHAR2(8),
  fail_info  VARCHAR2(512),
  data_count NUMBER(16),
  table_name VARCHAR2(256)
);
alter table F_LOG_INFO
  add constraint PK_F_LOG_INFO primary key (TASK_ID);

5.附本例使用完整测试代码

public class TestProcedures {
public static JdbcTemplate jdbcTemplate = getJdbcTemplate();
public static void main(String[] args) {
 System.out.println("测试开始......");
 // getProceduresResult();
 doProcedures();
 List result = getProceduresResultList();
 for (int i = 0; i < result.size(); i++) {
  Map rowMap = (Map) result.get(i);
  String id = rowMap.get("FAIL_INFO").toString();
  String name = rowMap.get("TABLE_NAME").toString();
  System.out.println("FAIL_INFO=" + id + ";TABLE_NAME=" + name);
 }
 System.out.println("测试结束......");
}

public static void doProcedures() {
	String procedures = "{call PRO_QUERY_INFO_ARGS4 ('888')}";
	jdbcTemplate.execute(procedures);
}

public static void getProceduresResult() {
 String tt2 = (String) jdbcTemplate.execute(
  new CallableStatementCreator() {
  	public CallableStatement createCallableStatement(
  	 	Connection con) throws SQLException {
  	 String procedures = "{call PRO_QUERY_INFO_ARGS3 (?,?,?,?)}";
  	 CallableStatement cs = con.prepareCall(procedures);
  	 
  	 cs.setString(1, "代码调用");
  	 
  	 cs.registerOutParameter(2, OracleTypes.VARCHAR);
  	 cs.registerOutParameter(3, OracleTypes.VARCHAR);
  	 cs.registerOutParameter(4, OracleTypes.VARCHAR);
  	 return cs;
  	}
  }, new CallableStatementCallback() {
  	public Object doInCallableStatement(CallableStatement st)
  	 	throws SQLException, DataAccessException {
  	 st.execute();
  	 
  	 Object tt2 = st.getObject(2);
  	 Object tt3 = st.getObject(3);
  	 Object tt4 = st.getObject(4);
  	 return tt2;
  	}
  });
}

public static List getProceduresResultList() {
  List resultList = (List) jdbcTemplate.execute(
   new CallableStatementCreator() {
   	public CallableStatement createCallableStatement(
   	 	Connection conn) throws SQLException {
   	 
   	 String procedures = "{ CALL PRO_QUERY_INFO_ARGS2(?,?) }";
   	 CallableStatement statement = conn
   	 		.prepareCall(procedures);
   	 
   	 statement.registerOutParameter(1, OracleTypes.VARCHAR);
   	 
   	 statement.registerOutParameter(2, OracleTypes.CURSOR);
   	 return statement;
   	}
   }, new CallableStatementCallback() {
   	public Object doInCallableStatement(
   	 	CallableStatement statement) throws SQLException,
   	 	DataAccessException {
   	 List resultsMap = new ArrayList();
   	 statement.execute();
   	 
   	 ResultSet resultSet = (ResultSet) statement
   	 		.getObject(2);
   	 
   	 while (resultSet.next()) {
   	 	Map rowMap = new HashMap();
   	 	rowMap.put("FAIL_INFO",
   	 			resultSet.getObject("FAIL_INFO"));
   	 	rowMap.put("TABLE_NAME",
   	 			resultSet.getObject("TABLE_NAME"));
   	 	resultsMap.add(rowMap);
   	 }
   	 resultSet.close();
   	 return resultsMap;
   	}
   });
  return resultList;
}

public static JdbcTemplate getJdbcTemplate() {
  DruidDataSource dataSource = new DruidDataSource();
  
  String username = "demodb";
  String password = "123456";
  String jdbcUrl = "jdbc:oracle:thin:@127.0.0.1:1521/orcl";
  String driverName = "oracle.jdbc.OracleDriver";
  
  dataSource.setPassword(password);
  dataSource.setUrl(jdbcUrl);
  dataSource.setUsername(username);
  dataSource.setDriverClassName(driverName);
  
  JdbcTemplate jdbcTemplate = new JdbcTemplate();
  
  jdbcTemplate.setDataSource(dataSource);
  return jdbcTemplate;
}
}

以上,感谢.

总结

到此这篇关于spring中JdbcTemplate操作oracle的存储过程的文章就介绍到这了,更多相关JdbcTemplate操作oracle存储过程内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯