• JDBC方式操作数据库


    1.读取配置文件,我将配置信息写入配置文件(.properties)中,方便后期数据库变更后或者其他项目使用,扩展性好些,尽量避免硬编码.

    driver=oracle.jdbc.driver.OracleDriver
    url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
    username=test
    password=test

    根据key获取配置文件方法之前的blog中有写.

    2.获取数据库连接

        /**
         * 实例化数据库连接
         * @return
         */
        private Connection getConnection() {
            String fileName = "thirdconfig.properties";
            Connection conn = null;
            try {
                String driver = PropertiesUtil.readPropertiesInfo(fileName, "driver");
                Class.forName(driver);
                String url = PropertiesUtil.readPropertiesInfo(fileName, "url");
                String username = PropertiesUtil.readPropertiesInfo(fileName, "username");
                String password = PropertiesUtil.readPropertiesInfo(fileName, "password");
                conn = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                logger.error("数据库连接失败", e);
            }
            return conn;
        }

    3查询数据总量

        private int getTotalCount(Connection conn, PreparedStatement prep) {
            ResultSet rs = null;
            int rowNum = 0;
            String querySql = "select count(1) from BACKUP_ALARM_INFO";
            try {
                prep = conn.prepareStatement(querySql);
                rs = prep.executeQuery();
                if (rs.next()) {
                    rowNum = rs.getInt(1);
                }
            } catch (SQLException e) {
                logger.error("执行查询出错", e);
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                    }
                    rs = null;
                }
            }
            return rowNum;
        }

     4JDBC读取PG数据库

    driver=org.postgresql.Driver
    url=jdbc:postgresql://localhost:5432/postgres
    username=postgres
    password=postgres
    querysql=select count(*) from test where id='#'
        private boolean validatePgExist(String hashValue) {
    
            String driver = IncrementSysConfigHandler.readSystemConfig("driver");
            String url = IncrementSysConfigHandler.readSystemConfig("url");
            String username = IncrementSysConfigHandler.readSystemConfig("username");
            String password = IncrementSysConfigHandler.readSystemConfig("password");
            String sqlStr = IncrementSysConfigHandler.readSystemConfig("querysql");
            sqlStr = sqlStr.replace("#", hashValue);
    
            Connection c = null;
            Statement stmt = null;
            ResultSet rs = null;
            try {
                Class.forName(driver);
                c = DriverManager.getConnection(url, username, password);
                c.setAutoCommit(false);
                logger.info("Opened database successfully");
                stmt = c.createStatement();
                rs = stmt.executeQuery(sqlStr);
                while (rs.next()) {
                    String result = rs.getString(1);
                    int count = Integer.valueOf(result);
                    if (count > 0) {
                        return true;
                    }
                }
    
            } catch (Exception e) {
                logger.error(e.getClass().getName() + ": " + e.getMessage());
            } finally {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    c.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return false;
        }
    View Code
  • 相关阅读:
    2 数据库开发--MySQL下载(windows)
    Spring课程 Spring入门篇 3-1 Spring bean装配(上)之bean的配置项及作用域
    Spring课程 Spring入门篇 2-2 Spring注入方式
    Spring课程 Spring入门篇 2-1 IOC和bean容器
    maven课程 项目管理利器-maven 5-1 课程总结 1星(2018-11-08 07:19)
    maven课程 项目管理利器-maven 4-1 使用maven创建web项目 5星
    maven课程 项目管理利器-maven 3-10 maven聚合和继承 4星
    maven课程 项目管理利器-maven 3-9 maven依赖冲突 4星
    maven课程 项目管理利器-maven 3-8 maven依赖传递 4星
    maven课程 项目管理利器-maven 3-7 maven依赖范围 2星
  • 原文地址:https://www.cnblogs.com/fxust/p/7490903.html
Copyright © 2020-2023  润新知