get Oracle Connection via Properties - Java java.sql

Java examples for java.sql:Oracle

Description

get Oracle Connection via Properties

Demo Code


import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Main{
    public static void main(String[] argv) throws Exception{
        System.out.println(getConnection());
    }//from w w  w  .ja  v a2 s  . co m
    public static Connection getConnection(String path) {
        Properties properties = new Properties();
        Connection connection = null;
        try {
            properties.load(DatabaseUtil.class
                    .getResourceAsStream("config.properties"));
            String url = properties.getProperty("url");
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            connection = getConnection(url, username, password);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return connection;
    }
    public static Connection getConnection() {
        return getConnection(url, username, password);
    }
    public static Connection getConnection(String url, String username,
            String password) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(url, username,
                    password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}

Related Tutorials