zoukankan      html  css  js  c++  java
  • DBUtil

    package utils;
    import org.apache.log4j.Logger;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class DBUtil4Mysql {
         String JDBC_DRIVER="com.mysql.jdbc.Driver";
         String DB_URL;
         String user;
         String passwd;
        static Logger log=LogUtils.getLogger();
    
        /**
         * 初始化参数
         * @param dbUrl
         * @param port
         * @param dbName
         * @param user
         * @param passwd
         */
        public void init(String dbUrl, String port, String dbName, String user, String passwd){
            this.DB_URL=String.format("jdbc:mysql://%s:%s/%s",dbUrl,port,dbName);
            this.user=user;
            this.passwd=passwd;
        }
    
        /**
         * 获取Map<String,Object>的结果
         * @param querySql
         * @return
         */
        public Map<String,Object> queryMapResult(String querySql){
            try {
                Class.forName(JDBC_DRIVER);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                log.error("未找到数据库驱动,数据库驱动注册失败");
            }
            Connection connection = null;
            Statement statement=null;
            ResultSet resultSet=null;
            Map<String,Object> map=null;
            try {
                connection = DriverManager.getConnection(DB_URL,user,passwd);
                statement=connection.createStatement();
                resultSet = statement.executeQuery(querySql);
                ResultSetMetaData metaData= resultSet.getMetaData();
                int count=metaData.getColumnCount();
                while (resultSet.next()){
                    map=new HashMap<String, Object>();
                    for(int i=1;i<=count;i++){
                        map.put(metaData.getColumnName(i),resultSet.getObject(i));
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
                log.error("获取数据失败");
            }
            close(resultSet,statement,connection);
            return map;
    
    
    
    
        }
    
        /**
         * 获取List<Map<String,Object>>的结果
         * @param querySql
         * @return
         */
        public List<Map<String,Object>> queryListMapResult(String querySql){
            try {
                Class.forName(JDBC_DRIVER);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                log.error("未找到数据库驱动,数据库驱动注册失败");
            }
            Connection connection = null;
            Statement statement=null;
            ResultSet resultSet=null;
            List<Map<String,Object>> list=null;
            try {
                connection = DriverManager.getConnection(DB_URL,user,passwd);
                statement=connection.createStatement();
                resultSet = statement.executeQuery(querySql);
                ResultSetMetaData metaData= resultSet.getMetaData();
                int count=metaData.getColumnCount();
                list=new ArrayList<Map<String, Object>>();
                while (resultSet.next()){
                    Map<String,Object> map=new HashMap<String, Object>();;
                    for(int i=1;i<=count;i++){
                        map.put(metaData.getColumnName(i),resultSet.getObject(i));
                    }
                    list.add(map);
                }
            } catch (SQLException e) {
                e.printStackTrace();
                log.error("获取数据失败");
            }
            
            close(resultSet,statement,connection);
            return list;
        }
    
        /**
         * 关闭各连接
         * @param resultSet
         * @param statement
         * @param connection
         */
        private void close(ResultSet resultSet,Statement statement,Connection connection){
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                log.error("resultSet 关闭失败");
            } finally {
                try {
                    if (statement != null) {
                        statement.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    log.error("statement 关闭失败");
                } finally {
                    try {
                        if (connection != null) {
                            connection.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                        log.error("connection 关闭失败");
                    }
                }
            }
        }
    }
  • 相关阅读:
    自动重连脚本
    自动输入用户名密码脚本
    idea 安装及配置
    manjaro安装anaconda
    hive 常用命令
    python built-in zip()
    isinstance(),issubclass()
    python built-in delattr()
    字符串匹配算法
    贪心,分治,回溯,动态规划 4大核心算法思想
  • 原文地址:https://www.cnblogs.com/yjh1995/p/11558534.html
Copyright © 2011-2022 走看看