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;
        }
    }
  • 相关阅读:
    Nginx 413 Request Entity Too Large
    tp U函数 logs
    div + css 边框 虚线
    html+css判断各个IE浏览器版本
    win7 cmd 常用命令
    mysql 常用命令
    ThinkPHP学习(二)理清ThinkPHP的目录结构及访问规则,创建第一个控制器
    将字符串 由一个字符集 转成 另一个字符集 及 随机生成中文
    Sublime Text 3 中实现编译C语言程序
    C语言入门
  • 原文地址:https://www.cnblogs.com/youxin/p/2787170.html
Copyright © 2011-2022 走看看