首页 > 数据库技术 > 详细

Java jdbc 连接oracle之三(封装工具类)

时间:2016-12-20 10:02:37      阅读:280      评论:0      收藏:0      [点我收藏+]
driver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@192.168.10.105:1521:orcl
user = LF
password = LF
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class JdbcTools {
    //properties文件名
    private static String defaultName = "jdbc.properties";
    /**
     * 获取Connection
     * @return connection
     * @throws Exception
     */
    public static Connection getConnection() throws Exception{
        Connection connection = getConnection(defaultName);
        return connection;
    }
    /**
     * 通过properties文件名获取Connection
     * @param fileName  properties文件名
     * @return          Connection
     * @throws Exception
     */
    public static Connection getConnection(String fileName) throws Exception{
        //IO流读取jdbc.properties文件
        InputStream in = JdbcTools.class.getClassLoader().getResourceAsStream(fileName);
        // 读取参数
        Properties p = new Properties();
        p.load(in);
        String url = p.getProperty("url");
        String user = p.getProperty("user");
        String password = p.getProperty("password");
        String driver=p.getProperty("driver");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        if (in!=null) {
            in.close();
        }
        return connection;
    }
    
    
}

 

Java jdbc 连接oracle之三(封装工具类)

原文:http://www.cnblogs.com/lantu1989/p/6201374.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!