不多说直接上代码
测试例子
@Value("${custom.url}")
private String gzUrl;
@Value("${custom.username}")
private String glUsername;
@Value("${custom.password}")
private String glPassword;
@Scheduled(cron = "0 5 8 * * ?")
public List<Day> getFxzbBfjhwclHb() {
List<Day> list = new ArrayList<>();
String ywzbSsbf = "select * from day ";
Connection connection = DBConnection.getConnection(glUsername, glPassword, gzUrl);
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(ySsbf);
while (rs.next()) {
String dateid = rs.getString(1);
String managecom = rs.getString(2);
String branchtype = rs.getString(3);
String indexCode = rs.getString(4);
String indexNumber = rs.getString(5);
String etlDate = rs.getString(6);
String etlDesc = rs.getString(7);
Day cwzb = new Day();
cwzb.setDateid(parseDateString(dateid));
cwzb.setManagecom(managecom);
cwzb.setBranchtype((branchtype));
cwzb.setIndexCode((indexCode));
cwzb.setIndexNumber(zh(indexNumber));
cwzb.setEtlDate(parseDateString(etlDate));
cwzb.setEtlDesc((etlDesc));
list.add(cwzb);
}
jdbc工具类
@Slf4j
public class DBConnection {
private final static String DRVIER = "oracle.jdbc.driver.OracleDriver";
private final static String POSTGRESQL = "org.postgresql.Driver";
/**
* 连接oracle
* @param userName 用户名
* @param password 密码
* @param url url
*/
public synchronized static Connection getConnection(String userName, String password, String url) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Connection connection;
log.info("访问oracle数据库信息,用户名={},url={}", userName, url);
try {
Class.forName(DRVIER);
connection = DriverManager.getConnection(url, userName, password);
} catch (ClassNotFoundException e) {
throw new RuntimeException("class not find !", e);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("get connection error!"+sdf.format(new Date()), e);
}
return connection;
}
/**
* 连接POSTGRESQL
* @param userName 用户名
* @param password 密码
* @param url url
*/
public synchronized static Connection getPostConnection(String userName, String password, String url) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Connection connection;
log.info("访问POSTGRESQL数据库信息,用户名={},url={}", userName, url);
try {
Class.forName(POSTGRESQL);
connection = DriverManager.getConnection(url, userName, password);
} catch (ClassNotFoundException e) {
throw new RuntimeException("class not find !", e);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("get connection error!"+sdf.format(new Date()), e);
}
return connection;
}
}
yml配置
custom:
url: jdbc:oracle:thin:@**177:1521:ey
username: 1234
password: 1234
代码释意 ⚠️
该测试类需要将DmAomDay改为自己创建的实体类
@Scheduled(cron = “0 5 8 * * ?”) 定时任务
Connection connection = DBConnection.getConnection(glUsername, glPassword, gzUrl) jdbc连接
其他的代码就很明显了,在此就不过多赘述。