zoukankan      html  css  js  c++  java
  • 转:JDBC连接MYSQL数据库 单例模式

    public class SqlHelper
    {
        private final Lock lock = new ReentrantLock();
        private static final SqlHelper sqlHelper = new SqlHelper();
        /**
         * 私有的默认构造方法
         */
        private SqlHelper()
        {
        }
        /**
         * 静态方法获得单例
         */
        public static SqlHelper getInstance()
        {
            return sqlHelper;
        }
        private static final String DRIVER = "com.mysql.jdbc.Driver";
        private static final String URL = "jdbc:mysql://localhost:3306/test";
        private static final String USER = "root";
        private static final String PWD = "root";
        private Connection conn = null;
        public Connection getConnection()
        {
            try
            {
                Class.forName(DRIVER);
                return DriverManager.getConnection(URL, USER, PWD);
            } catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
            return null;
        }
        public ResultSet executeQuery(String sql, Object[] params)
        {
            try
            {
                this.conn = this.getConnection();
                PreparedStatement pst = conn.prepareStatement(sql);
                for (int i = 0; i < params.length; i++)
                {
                    pst.setObject(i + 1, params[i]);
                }
                return pst.executeQuery();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
            return null;
        }
        public void closeConnection()
        {
            try
            {
                if (this.conn != null && !conn.isClosed())
                {
                    conn.close();
                }
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        public ResultSet executeQuery(String sql)
        {
            try
            {
                this.conn = this.getConnection();
                PreparedStatement pst = conn.prepareStatement(sql);
                return pst.executeQuery();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
            return null;
        }
        public int executeUpdate(String sql, Object[] params)
        {
            this.lock.lock();
            PreparedStatement pst = null;
            int num = 0;
            try
            {
                this.conn = this.getConnection();
                pst = conn.prepareStatement(sql);
                for (int i = 0; i < params.length; i++)
                {
                    pst.setObject(i + 1, params[i]);
                }
                num = pst.executeUpdate();
            } catch (SQLException e)
            {
                e.printStackTrace();
            } finally
            {
                this.closeConnection();
                this.lock.unlock();
            }
            return num;
        }
    }
  • 相关阅读:
    聊聊Spark的分区、并行度 —— 前奏篇
    深入探讨HBASE
    分布式流平台Kafka
    GeoServer中使用SLD样式
    OpenLayer修改WFS中的要素
    leaflet加载GeoServer的WFS服务
    OL实现属性查询的功能
    OL3实现空间查询的代码示例
    WFS—GetFeature方法
    OpenLayer+Geoserver+postgis实现路径分析
  • 原文地址:https://www.cnblogs.com/youxin/p/2787170.html
Copyright © 2011-2022 走看看