zoukankan      html  css  js  c++  java
  • java web数据库连接封装-simple

    package cn.cslg.bm.web.util;
    
    import java.sql.Connection;
    
    import java.sql.DriverManager;
    
    import org.apache.log4j.Logger;
    
    
    /**
    
     * 返回数据库连接
    
     * @author husky
    
     * 未使用
    
     */
    
    public class DBSimpleUtil {
    
    
    
        private static Logger logger = Logger.getLogger(DBSimpleUtil.class);
    
        
    
        public static String URL="jdbc:mysql://******:3306/bm?characterEncoding=UTF-8";
    
        public static String USER="*****";
    
        public static String PASS="******";
    
        public static Connection conn=null;
    
        
    
        static {
    
            try {
    
                Class.forName("com.mysql.jdbc.Driver");
    
            } catch (ClassNotFoundException e) {
    
                e.printStackTrace();
    
            }
    
        }
    
        
    
        public static Connection openConnection(){
    
            System.out.println("test");
    
            try{
    
                if(conn==null||conn.isClosed())
    
                    logger.info("打开数据库!");
    
                    conn=DriverManager.getConnection(URL, USER, PASS);
    
                
    
            }catch(Exception e){
    
                logger.error("打开数据库出错!");
    
                e.printStackTrace();
    
            }
    
            return conn;
    
        }
    
        
    
        public static void closeConnection(){
    
            try{
    
                if(conn!=null&&!conn.isClosed()){
    
                    logger.info("关闭数据库!");
    
                    conn.close();
    
                }
    
            }catch(Exception e){
    
                logger.error("关闭数据库出错!");
    
                e.printStackTrace();
    
            }
    
        }
    
    //    public static void main(String[] args) {
    
    //        DBSimpleUtil.openConnection();
    
    //        DBSimpleUtil.closeConnection();
    
    //    }
    
    }
  • 相关阅读:
    hdu5081
    hdu5079
    hdu5076
    hdu5072
    codeforces 739E
    codeforces 739D
    2017.2其他简要题解
    spring事务传播回滚策略
    mybatis一级缓存和二级缓存
    spring-boot-mybatis-starter工作原理
  • 原文地址:https://www.cnblogs.com/husky/p/6529507.html
Copyright © 2011-2022 走看看