zoukankan      html  css  js  c++  java
  • 连接数据库工具类DBUtil

    代码如下:

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.Properties;
    
    import org.apache.commons.dbcp.BasicDataSource;
    
    public class DBUtil {
        private static BasicDataSource ds;
        static {
            Properties prop = new Properties();
            try {
                prop.load(DBUtil.class.getClassLoader().getResourceAsStream("config.properties"));//类目录下要有对应的文件
                String className = prop.getProperty("classname");
                String url = prop.getProperty("url");
                String password = prop.getProperty("password");
                String username = prop.getProperty("username");
    
                ds = new BasicDataSource();
                ds.setDriverClassName(className);
                ds.setUrl(url);
                ds.setUsername(username);
                ds.setPassword(password);
                System.out.println("连接成功!");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static Connection getConnection() throws SQLException {
            return ds.getConnection();
        }
    
        public static void closeConnection(Connection conn) {
            try {
                conn.commit();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    timeit模块
    python中的del
    python的默认参数
    python3中的nonlocal 与 global
    python通俗讲解闭包
    vlc 视频播放器的快捷键
    Python的重要知识点汇总3
    Python的重要知识点汇总2
    Python的重要知识点汇总1
    01玩转数据结构_08_堆和优先队列
  • 原文地址:https://www.cnblogs.com/suhfj-825/p/8257617.html
Copyright © 2011-2022 走看看