Java使用代码访问第三方数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
//这里面的变量都在nacos里定义
public List<PacsRespVo> payStoredProcedure(PacsDTO dto) {
log.info("入参:{}",dto);
List<PacsRespVo> list = new ArrayList<>();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.获取连接 "jdbc:oracle:thin:@192.168.80.100:1521:orcl"
String url = "jdbc:oracle:thin:@" + ip + ":" + port + "/" + serverName;
Connection conn = DriverManager.getConnection(url, username, password);
//创建Statement对象
Statement stmt = conn.createStatement();
String sql = "SELECT * from " + viewName + " where DJLSH='" + dto.getRegSerialNo()+"'" + "and ZHXMBH=" + dto.getZhxmCodeLis();
log.info("调用查询语句:{}",sql);
//执行查询
ResultSet rs = stmt.executeQuery(sql);
PacsRespVo vo;
while (rs.next()) {
vo = new PacsRespVo();
String djlsh = rs.getString("DJLSH");
String jcsj = rs.getString("JCSJ");
String jcqkfzsm = rs.getString("JCQKFZSM");
String jg = rs.getString("JG");
String shr = rs.getString("SHR");
String studytime = rs.getString("STUDYTIME");
String studystatusname = rs.getString("STUDYSTATUSNAME");
vo.setRegSerialNo(djlsh);
vo.setAuxiliaraResult(jcsj);
vo.setCheckPart(jcqkfzsm);
vo.setSummarize(jg);
vo.setCheckDoctor(shr);
vo.setCheckTime(studytime);
list.add(vo);
}
stmt.close();
conn.close();
return list;
} catch (Exception e) {
throw new RuntimeException("调用视图查询失败!!!!!!!");
}
}