码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-12-20 10:05:52      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:nec   acl   封装   man   cti   ring   source   .sql   throw   

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之三(封装工具类)

标签:nec   acl   封装   man   cti   ring   source   .sql   throw   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!