Java连接Oracle,PostgreSQL数据库

不多说直接上代码

测试例子


    @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连接
其他的代码就很明显了,在此就不过多赘述。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值