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
  • 相关阅读:
    达到J2EE在后台action控制接待javascript弹出的对话框
    .Net下一个Winform方案可以让MessageBox.Show它显示在父窗口的中间
    Apache Commons-logging使用实例
    java aopalliance-1.0.jar这个包是做什么用的?
    antlr-2.7.6.jar的作用
    ORA-12516 TNS监听程序找不到符合协议堆栈要求的可用处理程序--解决方法
    Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)
    javaweb学习总结(二十六)——jsp简单标签标签库开发(二)
    javaweb学习总结(二十五)——jsp简单标签开发(一)
    javaweb学习总结(二十四)——jsp传统标签开发
  • 原文地址:https://www.cnblogs.com/fxust/p/7490903.html
Copyright © 2011-2022 走看看