跨系统数据库取数实现方案

时间:2022-11-06 11:00:13

情景:假如要做一个系统A的报表,但其中部分字段的取值在系统B,那系统A里就不能一个SQL查询系统A和系统B的数据了,这种情况可以系统B提供一个接口,去查询取得系统B的数据。除此之外,还可以用以下方案实现取数:

1.利用url,用户名,密码直接连接系统B的数据库,这些参数可以在系统A的数据库中建一个表来保存,方便后续的修改维护。

跨系统数据库取数实现方案

2.java连接系统B数据库。

    /**
* 取得影像系统的数据库连接
* @return
*/
public static Connection getImageConnection(Context ctx){
String driver = null;
String url = null;
String username = null;
String password = null;
try
{
String sql = "select id,name,driver,imgurl,imgname,imgpassword from imageconparam where id='001'";
IRowSet rs = DbUtil.executeQuery(ctx, sql);
if(rs.next())
{
driver = rs.getString("driver");
url = rs.getString("imgurl");;
username = rs.getString("imgname");;
password = rs.getString("imgpassword");;
}
}
catch (BOSException e1)
{
e1.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}

Connection imageConnection = null;
try
{
Class.forName(driver);
imageConnection= DriverManager.getConnection(url, username, password);
}
catch (ClassNotFoundException e2)
{
e2.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
return imageConnection;
}

3.查询系统B数据并返回结果。

/**
* 从影像系统取得后补发票影像号对应的接收时间
* @param imagenoList
* @return
*/
private Map<String, String> getArchiveDate(List<String> imagenoList)
{
Map<String,String> archivedateMap = new HashMap<String, String>();
try
{
String inparam = ToolUtils.aryToStr(imagenoList);
Context ctx = WafContext.getInstance().getContext();
Connection imageConnection = BDUtil.getImageConnection(ctx);
Statement statementimg = imageConnection.createStatement();
String sql = "select 1,2 from t_hist_task where rownum <=10";
ResultSet rs = statementimg.executeQuery(sql);
while(rs.next())
{
String imgeno = rs.getString(1);
String date = rs.getString(2);
if(!StringUtils.isEmpty(imgeno) && !StringUtils.isEmpty(date))
{
archivedateMap.put(imgeno, date);
}
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}

return archivedateMap;
}