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

 

posted @ 2017-09-07 17:34  疯狂的蜗牛仔  阅读(202)  评论(0编辑  收藏  举报