连接数据库代码 新

private static DruidDataSource druidDataSource;
    static {
        Properties properties = new Properties();
        InputStream inputStream = DButil.class.getResourceAsStream("/datebase.properties");
        try {
            properties.load(inputStream);
            try {
                druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Connection getconnection()
    {
        try {
            return druidDataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet)
    {
        try {
            if(resultSet != null)
                resultSet.close();
            if(statement != null)
                statement.close();
            if(connection != null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

 

posted @ 2021-11-04 23:26  软工小蜗牛  阅读(66)  评论(0编辑  收藏  举报