JDBCUtil工具类

JDBCUtil工具类

package com.lingyi.sync.util;


import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class JdbcUtil {

    //日志
    private static Logger logger = Logger.getLogger(JdbcUtil.class.getClass());



    public static enum DatabaseType {
        Oracle("oracle"),
        SQL_Server("sqlserver"),
        MySQL("mysql");

        private String name;
        private DatabaseType(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    };


    //配置文件
    private static Properties props;
    //项目运行路径
    private static String filePath =null;


    //获取项目运行路径
    static  {
        URL url = JdbcUtil.class.getProtectionDomain().getCodeSource().getLocation();
        try {
            filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码,支持中文
        } catch (Exception e) {
            e.printStackTrace();
        }

        filePath = filePath.replace("target/classes/", "");

        if (filePath.endsWith(".jar")) {// 可执行jar包运行的结果里包含".jar"
            // 获取jar包所在目录
            filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
        }
        if (System.getProperty("os.name").contains("dows")){
            File file = new File(filePath);
            filePath = file.getAbsolutePath();//得到windows下的正确路径
        }
    }


    //加载配置文件
    static {
        try {
            props = new Properties();
            InputStream in = new FileInputStream(new File(filePath + "/config/chemical_db.properties"));
            props.load(in);
        } catch (IOException e) {
            LogUtil.insert(new Date(),"安全考试系统","错误信息","加载外部配置文件失败");
            throw  new RuntimeException("没有找到外部配置文件");
        }
    }




    /**
     * 获取数据库连接
     * @param type 要操作的数据库
     * @return
     */
    public static Connection getConnection(DatabaseType type){
        //加载驱动
        try {
            Class.forName(props.getProperty(type.getName() + ".driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String jdbcUrl = props.getProperty(type.getName() + ".jdbcUrl");
        String user = props.getProperty(type.getName() + ".user");
        String password = props.getProperty(type.getName() + ".password");
        //链接数据库
        try {
            return DriverManager.getConnection(jdbcUrl, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new RuntimeException("连接数据库失败");
        }
    }


    /**
     * 关闭数据库连接
     * @param ps
     */
    public static void releaseDB(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 关闭数据库连接
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void releaseDB(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

#MySQL
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.jdbcUrl=jdbc:mysql://localhost:3306/labv2
mysql.user=root
mysql.password=root



#Oracle
oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
oracle.user=
oracle.password=


#sql_server
sqlserver.driver=
sqlserver.jdbcUrl=
sqlserver.user=
sqlserver.password=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值