java同时调用oracle和db2数据库存储过程:添加/修改/删除/查询的处理方法

java同时调用oracle和db2数据库存储过程:添加,修改,删除,查询的处理方法

在java开发过程中,经常会遇到调用存储过程,有时用调用oracle数据库,有时调用db2数据库。。。。那么如何同时在一个项目中调用两种不同的大型数据库呢????

 以下代码是我以前做的项目中用到的同时调用oracle和db2数据库:添加,修改,删除,查询的处理方法,以供大家用时间做为参考,

如有不同的意见,请说明下,谢谢

package com.jbeduhai.util;

import java.sql.*;
import java.util.*;
import oracle.jdbc.OracleTypes;
import com.bstek.dorado.common.DoradoContext;
import com.bstek.dorado.common.MessageHelper;
import com.bstek.dorado.common.ds.ConnectionHelper;
import com.jbeduhai.base.jbeduhaiMsg;

public class jbeduhaiTemplate {
 private String flag = "0";

 private String retMsg = "";

 private Connection conn = null;

 private CallableStatement stored_p = null;

 private ResultSet rs = null;

 private String dialect = null;

 private String dbsql = null;

 private String dbflag = "0";

 private int intflag = 0;

 private String oracle = "Oracle";

 private String db2 = "DB2";

 private jbeduhaiMsg msg = null;

 public List query(String sql, Object objs[], jbeduhaiMapper mapper)
   throws Exception {

  this.getDialect(sql);
  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  stored_p = conn.prepareCall("{call " + dbsql + "}");
  for (int i = 0; i < objs.length; i++) {
   if (objs[i] == null) {
    objs[i] = "";
   }
   if ("".equals(objs[i])) {
    stored_p.setString(i + 1, null);
   } else {
    stored_p.setObject(i + 1, objs[i]);
   }
   System.out.println("params" + i + "================>>>:" + objs[i]);
  }
  stored_p.registerOutParameter(objs.length + 1, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 2, Types.CHAR);
  if (dbflag == "1" || dbflag.equals("1")) {
   stored_p.registerOutParameter(objs.length + 3, OracleTypes.CURSOR);
  }
  stored_p.execute();

  flag = stored_p.getString(objs.length + 1);
  retMsg = stored_p.getString(objs.length + 2);

  if (!flag.equals("0")) {
   // throw new Exception(retMsg);
   MessageHelper.addMessage(DoradoContext.getContext(), "flag",flag);
   MessageHelper.addMessage(DoradoContext.getContext(), "retMsg",retMsg);
   return null;
  }
  if (dbflag == "1" || dbflag.equals("1")) {
   rs = (ResultSet) stored_p.getObject(objs.length + 3);
  }
  if (dbflag == "2" || dbflag.equals("2")) {
   rs = stored_p.getResultSet();
  }
  if (rs == null || rs.equals("")) {
   return null;
  }
  List list = new ArrayList();
  try {
   while (rs.next()) {
    Object obj = mapper.processResult(rs);
    list.add(obj);
   }
  } finally {
   this.closeConnection();
  }

  return list;
 }

 /**
  * 取过程最后一个输出参数为字符串的方法
  *
  * @param sql
  * @param objs
  * @return
  * @throws Exception
  */

 public jbeduhaiMsg query1(String sql, Object objs[]) throws Exception {
  msg = new jbeduhaiMsg();
  int fg;
  // this.getDialect(sql);
  String dbsql = sql;
  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  stored_p = conn.prepareCall("{call " + dbsql + "}");
  for (int i = 0; i < objs.length; i++) {
   if (objs[i] == null) {
    objs[i] = "";
   }
   if ("".equals(objs[i])) {
    stored_p.setString(i + 1, null);
   } else {
    stored_p.setObject(i + 1, objs[i]);
   }
   System.out.println("params" + i + "================>>>:" + objs[i]);
  }

  stored_p.registerOutParameter(objs.length + 1, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 2, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 3, Types.CHAR);
  stored_p.execute();

  fg = stored_p.getInt(objs.length + 1);
  // String retMsg = stored_p.getString(objs.length + 2);
  String resultSet = stored_p.getString(objs.length + 3);

  msg.setCode(fg);
  msg.setMessage(resultSet);

  if (!flag.equals("0")) {
   return null;
  }
  try {
   // stored_p.close();
  } finally {
   this.closeConnection();
  }
  return msg;
 }

 /**
  * 取过程最后一个输出参数为字符串的方法 输出参数为4个

  *
  * @param sql
  * @param objs
  * @return
  * @throws Exception
  */

 public jbeduhaiMsg queryOut4(String sql, Object objs[]) throws Exception {
  msg = new jbeduhaiMsg();
  int fg;
  // this.getDialect(sql);
  String dbsql = sql;
  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  stored_p = conn.prepareCall("{call " + dbsql + "}");
  for (int i = 0; i < objs.length; i++) {
   if (objs[i] == null) {
    objs[i] = "";
   }
   if ("".equals(objs[i])) {
    stored_p.setString(i + 1, null);
   } else {
    stored_p.setObject(i + 1, objs[i]);
   }
   System.out.println("params" + i + "================>>>:" + objs[i]);
  }

  stored_p.registerOutParameter(objs.length + 1, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 2, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 3, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 4, Types.CHAR);
  stored_p.execute();

  fg = stored_p.getInt(objs.length + 1);
  // String retMsg = stored_p.getString(objs.length + 2);
  String resultSet = stored_p.getString(objs.length + 3);
  // String resultSetName = stored_p.getString(objs.length + 4);

  msg.setCode(fg);
  msg.setMessage(resultSet);

  if (!flag.equals("0")) {
   return null;
  }
  try {
   // stored_p.close();
  } finally {
   this.closeConnection();
  }
  return msg;
 }

 /*
  *
  * 取过程最后一个输出参数为字符串的方法 方法名:query1 sql :调用的过程SQL objs[] :传入的输入参数' mapper :
  */

 public List query1(String sql, Object objs[], jbeduhaiMapper mapper)
   throws Exception {

  // this.getDialect(sql);
  String dbsql = sql;
  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  stored_p = conn.prepareCall("{call " + dbsql + "}");
  for (int i = 0; i < objs.length; i++) {
   if (objs[i] == null) {
    objs[i] = "";
   }
   if ("".equals(objs[i])) {
    stored_p.setString(i + 1, null);
   } else {
    stored_p.setObject(i + 1, objs[i]);
   }
   System.out.println("params" + i + "================>>>:" + objs[i]);
  }

  stored_p.registerOutParameter(objs.length + 1, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 2, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 3, Types.CHAR);
  stored_p.execute();

  flag = stored_p.getString(objs.length + 1);
  String msgg = stored_p.getString(objs.length + 3);
  System.out.println(msgg);
  List list = new ArrayList();
  list.add(msgg);
  if (!flag.equals("0")) {
   return null;
  }
  try {
   // stored_p.close();
  } finally {
   this.closeConnection();
  }
  return list;
 }
 
 /*
  *
  * 取过程第二个输出参数为Number的方法 方法名:query2 sql :调用的过程SQL objs[]
  *
 */
 public int query2Para(String sql, Object objs[])
   throws Exception {

  // this.getDialect(sql);
  int seqNo = 0;
  String dbsql = sql;
  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  stored_p = conn.prepareCall("{call " + dbsql + "}");
  for (int i = 0; i < objs.length; i++) {
   if (objs[i] == null) {
    objs[i] = "";
   }
   if ("".equals(objs[i])) {
    stored_p.setString(i + 1, null);
   } else {
    stored_p.setObject(i + 1, objs[i]);
   }
   System.out.println("params" + i + "================>>>:" + objs[i]);
  }

  stored_p.registerOutParameter(objs.length + 1, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 2, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 3, Types.CHAR);
  stored_p.execute();

  seqNo = stored_p.getInt(objs.length + 1);
  System.out.println(flag);
  flag = stored_p.getString(objs.length + 2);
  System.out.println(seqNo);
  String msgg = stored_p.getString(objs.length + 3);
  System.out.println(msgg);

  if (!flag.equals("0")) {
   return 0;
  }
  try {
   // stored_p.close();
  } finally {
   this.closeConnection();
  }
  return seqNo;
 }
 
 
 /**
  * 取过程倒数第三个输出参数为字符串的方法
  *
  * @param sql
  * @param objs
  * @return
  * @throws Exception
  */

 public jbeduhaiMsg query3(String sql, Object objs[]) throws Exception {
  msg = new jbeduhaiMsg();
  int fg;
  // this.getDialect(sql);
  String dbsql = sql;
  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  stored_p = conn.prepareCall("{call " + dbsql + "}");
  for (int i = 0; i < objs.length; i++) {
   if (objs[i] == null) {
    objs[i] = "";
   }
   if ("".equals(objs[i])) {
    stored_p.setString(i + 1, null);
   } else {
    stored_p.setObject(i + 1, objs[i]);
   }
   System.out.println("params" + i + "================>>>:" + objs[i]);
  }

  stored_p.registerOutParameter(objs.length + 1, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 2, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 3, Types.CHAR);
  stored_p.execute();

  String resultSet = stored_p.getString(objs.length + 1);
  fg = stored_p.getInt(objs.length + 2);
  String retMsg = stored_p.getString(objs.length + 3);
 

  msg.setCode(fg);
  msg.setMessage(retMsg);
  msg.setOutString(resultSet);

  if (!flag.equals("0")) {
   return null;
  }
  try {
   // stored_p.close();
  } finally {
   this.closeConnection();
  }
  return msg;
 }
 
 /**
  * 取过程第三个输出参数为字符串的方法

  *
  * @param sql
  * @param objs
  * @return
  * @throws Exception
  */

 public jbeduhaiMsg queryout3(String sql, Object objs[]) throws Exception {
  msg = new jbeduhaiMsg();
  int fg;
  // this.getDialect(sql);
  String dbsql = sql;
  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  stored_p = conn.prepareCall("{call " + dbsql + "}");
  for (int i = 0; i < objs.length; i++) {
   if (objs[i] == null) {
    objs[i] = "";
   }
   if ("".equals(objs[i])) {
    stored_p.setString(i + 1, null);
   } else {
    stored_p.setObject(i + 1, objs[i]);
   }
   System.out.println("params" + i + "================>>>:" + objs[i]);
  }

  stored_p.registerOutParameter(objs.length + 1, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 2, Types.CHAR);
  stored_p.registerOutParameter(objs.length + 3, Types.CHAR);
  stored_p.execute();

  fg = stored_p.getInt(objs.length + 1);
  String retMsg = stored_p.getString(objs.length + 2);
  String status = stored_p.getString(objs.length + 3);
 
  msg.setCode(fg);
  msg.setMessage(retMsg);
  msg.setOutString(status);

  if (!flag.equals("0")) {
   return null;
  }
  try {
   // stored_p.close();
  } finally {
   this.closeConnection();
  }
  return msg;
 }
 

 public jbeduhaiMsg update(String sql, Object objs[]) throws Exception {
  int flag = 0;
  String dbsql = sql;
  msg = new jbeduhaiMsg();

  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  conn.setAutoCommit(false);
  try {
   stored_p = conn.prepareCall("{call " + dbsql + "}");
   for (int i = 0; i < objs.length; i++) {
    if (objs[i] == null) {
     objs[i] = "";
    }
    if ("".equals(objs[i])) {
     stored_p.setString(i + 1, null);
    } else {
     stored_p.setObject(i + 1, objs[i]);
    }
    System.out.println("params" + i + "================>>>:"
      + objs[i]);
   }
   stored_p.registerOutParameter(objs.length + 1, Types.CHAR);
   stored_p.registerOutParameter(objs.length + 2, Types.CHAR);
   stored_p.execute();

   flag = stored_p.getInt(objs.length + 1);
   retMsg = stored_p.getString(objs.length + 2);

   if (flag != 0)
    conn.rollback();
   else
    conn.commit();

   msg.setCode(flag);
   msg.setMessage(retMsg);

  } finally {
   this.closeConnection();
  }
  return msg;
 }

 /*
  * 查询,第三个参数为游标
  */

 public List queryOther(String sql, Object objs[], jbeduhaiMapper mapper)
   throws Exception {

  this.getDialect(sql);
  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  stored_p = conn.prepareCall("{call " + dbsql + "}");

  stored_p.registerOutParameter(1, Types.CHAR);
  stored_p.registerOutParameter(2, Types.CHAR);
  if (dbflag == "1" || dbflag.equals("1")) {
   stored_p.registerOutParameter(3, OracleTypes.CURSOR);
  }
  for (int i = 0; i < objs.length; i++) {
   if (objs[i] == null) {
    objs[i] = "";
   }
   if ("".equals(objs[i])) {
    if (dbflag == "1" || dbflag.equals("1")) {
     stored_p.setString(i + 4, null);
    }
    if (dbflag == "2" || dbflag.equals("2")) {
     stored_p.setObject(i + 3, objs[i]);
    }
   } else {
    if (dbflag == "1" || dbflag.equals("1")) {
     stored_p.setObject(i + 4, objs[i]);
    }
    if (dbflag == "2" || dbflag.equals("2")) {
     stored_p.setObject(i + 3, objs[i]);
    }
   }
   System.out.println("params" + i + "================>>>:" + objs[i]);
  }
  stored_p.execute();

  flag = stored_p.getString(1);
  retMsg = stored_p.getString(2);

  if (!flag.equals("0")) {
   // throw new Exception(retMsg);
   MessageHelper.addMessage(DoradoContext.getContext(), retMsg);
  }
  if (dbflag == "1" || dbflag.equals("1")) {
   rs = (ResultSet) stored_p.getObject(3);
  }
  if (dbflag == "2" || dbflag.equals("2")) {
   rs = stored_p.getResultSet();
  }
  if (rs == null || rs.equals("")) {
   return null;
  }

  List list = new ArrayList();
  try {
   while (rs.next()) {
    Object obj = mapper.processResult(rs);
    list.add(obj);
   }
   // stored_p.close();
  } finally {
   this.closeConnection();
  }

  return list;
 }

 /**
  *
  * @param sql
  * @param objs
  * @return
  * @throws Exception
  */
 public jbeduhaiMsg updateOther(String sql, Object objs[]) throws Exception {
  int flag = 0;
  String dbsql = sql;
  msg = new jbeduhaiMsg();

  System.out.println("sql================>>>" + "{call " + dbsql + "}");
  conn = this.getConnection();
  conn.setAutoCommit(false);
  try {
   stored_p = conn.prepareCall("{call " + dbsql + "}");

   stored_p.registerOutParameter(1, Types.CHAR);
   stored_p.registerOutParameter(2, Types.CHAR);

   for (int i = 0; i < objs.length; i++) {
    if (objs[i] == null) {
     objs[i] = "";
    }
    if ("".equals(objs[i])) {
     stored_p.setString(i + 3, null);
    } else {
     stored_p.setObject(i + 3, objs[i]);
    }
    System.out.println("params" + i + "================>>>:"
      + objs[i]);
   }
   stored_p.execute();

   flag = stored_p.getInt(1);
   retMsg = stored_p.getString(2);

   if (flag != 0)
    conn.rollback();
   else
    conn.commit();

   msg.setCode(flag);
   msg.setMessage(retMsg);

   // stored_p.close();
  } finally {
   this.closeConnection();
  }
  return msg;
 }

 public Connection getConnection() throws Exception {
  conn = ConnectionHelper.getDefaultConnection();
  return conn;
 }

 public void closeConnection() throws Exception {
  stored_p.close();
  conn.close();
 }

 public void getDialect(String sql) {
  dialect = ConnectionHelper.getDefaultDialect().toString();
  System.out.println("dialect================>>>" + dialect);

  intflag = dialect.indexOf(oracle);
  if (intflag > 0) {
   dbflag = "1";
   dbsql = sql;
   System.out.println("DataSource================>>>" + oracle);
  }
  intflag = dialect.indexOf(db2);
  if (intflag > 0) {
   dbflag = "2";
   dbsql = sql.substring(0, sql.length()-3)+")";
   //dbsql = sql.replace("(?,", "(");
   System.out.println("DataSource================>>>" + db2);
  }
 }
}

 

如果有遇到这样的问题解决不了了,

QQ:15900487756

EMail:[email protected]  大家可以共论一下...

 

你可能感兴趣的:(java,oracle,sql,qq,db2)