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
  • 相关阅读:
    php笔记之数组
    php笔记之函数
    vue笔记之路由
    <QT>:使用QUdpSocket进行UDP通信
    Linux下用ssh在远程Xserver上显示图形界面
    <学习QT>在QListWidget中实现每个单元项显示文字在上图片在下
    const限定符以及顶层const和底层const的理解
    了解X window以及相关知识
    Linux下使用Eclipse C/C++生成创建并调用动态库(.so)
    Linux下搭载Xwindow(Xlib)开发环境
  • 原文地址:https://www.cnblogs.com/fxust/p/7490903.html
Copyright © 2011-2022 走看看