zoukankan      html  css  js  c++  java
  • JDBCUtils.java

    package com.yuan25.util;
    
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    /**
     *
     * @Description 操作数据库的工具类
     *
     */
    
    public class JDBCUtils {
    
        /**
         *
         * @Description 获取数据库的连接
    
         */
        public static Connection getConnection() throws Exception {
            // 1.读取配置文件中的4个基本信息
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
    
            Properties pros = new Properties();
            pros.load(is);
    
            String user = pros.getProperty("user");
            String password = pros.getProperty("password");
            String url = pros.getProperty("url");
            String driverClass = pros.getProperty("driverClass");
    
            // 2.加载驱动
            Class.forName(driverClass);
    
            // 3.获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            return conn;
        }
        /**
         *
         * @Description 关闭连接和Statement的操作
         */
        public static void closeResource(Connection conn,Statement ps){
            try {
                if(ps != null)
                    ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         *
         * @Description 关闭资源操作
    
         */
        public static void closeResource(Connection conn,Statement ps,ResultSet rs){
            try {
                if(ps != null)
                    ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    DevOps中30 个 Docker 相关的 面试题
    Docker面试题
    微服务-服务的注册与发现
    Zookeeper 节点特性
    ElementUI 分页
    ElementUI input只允许输入数字和两位小数
    Kubernetes等待部署完成 kubectl wait rollout
    使用docker搭建selenium分布式环境
    使用Django,Prometheus,和Kubernetes定制应用指标
    使用Python和Flask编写Prometheus监控
  • 原文地址:https://www.cnblogs.com/fenxiangyuan/p/14695209.html
Copyright © 2011-2022 走看看