zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    数据结构-树与二叉树-思维导图
    The last packet successfully received from the server was 2,272 milliseconds ago. The last packet sent successfully to the server was 2,258 milliseconds ago.
    idea连接mysql报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property
    redis学习笔记
    AJAX校验注册用户名是否存在
    AJAX学习笔记
    JSON学习笔记
    JQuery基础知识学习笔记
    Filter、Listener学习笔记
    三层架构学习笔记
  • 原文地址:https://www.cnblogs.com/fxust/p/7490903.html
Copyright © 2011-2022 走看看