DB - Yash-777/LearnJava GitHub Wiki

package com.github.jdbc

Standalone

public class DBConnection {
    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        try {
            conn = DBConnection.getConnection("DEV");
        } finally {
            conn.close();
        }
    }
    
    static Map<String, String> connectionUrls = new HashMap<String, String>();
    static {
        // validationQuery="select 1 from dual", Host, Port, Sid (default=xe), userName, Password
        
        connectionUrls.put("DEV", "HostDEV~1521~xeDEV~xeDEV1~xeDEV777");
        connectionUrls.put("UAT", "HostUAT~1522~xeUAT~xeUAT1~xeUAT777");
    }
    public static Connection getConnection(String envKey) throws SQLException {
        Connection connection = null;
        OracleDriver driverClassName = new oracle.jdbc.driver.OracleDriver();
        try {
            java.sql.DriverManager.registerDriver(driverClassName);
            
            if (connectionUrls.containsKey(envKey)) {
                String uralParams = connectionUrls.get(envKey);
                String[] split = uralParams.split("~");
                
                StringBuffer buffer = new StringBuffer();
                buffer.append("jdbc:oracle:thin:@(DESCRIPTION=(ENABLE=BROKEN)(ADDRESS=(PROTOCOL=tcp)(PORT="+split[1]);
                buffer.append(")(HOST="+split[0]);
                buffer.append("))(CONNECT_DATA=(SID="+split[2]+")))");
                
                String usernName = split[3], password = split[4];
                
                // jdbc:oracle:driver_type:[username/password]@[//]host_name[:port][:ORCL]
                String connectionUrl = buffer.toString();
                System.out.println("DB URL: "+connectionUrl);
                // connection = DriverManager.getConnection("jdbc:default:connection:");
                //connection = DriverManager.getConnection(connectionUrl, usernName, password);
                
                // https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html
                Properties connectionProps = new Properties();
                connectionProps.put("charset", "UTF8");
                connectionProps.put("lc_ctype", "en_US.UTF-8");
                connectionProps.put("user", usernName);
                connectionProps.put("password", password);
                
                connection = DriverManager.getConnection(connectionUrl, connectionProps);
                System.out.println("Connection : "+ connection);
            }
            return connection;
        } finally {
            DriverManager.deregisterDriver(driverClassName);
        }
    }
}

Server Context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Environment name="contexEnvironmentDataSource" value="jdbc/yashoracle"
        type="java.lang.String" override="false" />
    <Environment name="contexEnvironmentDataSource2" value="jdbc/yashoracle2"
        type="java.lang.String" override="false" />

    <Resource name="jdbc/yashoracle" type="javax.sql.DataSource"
        username="yash" password="yashDB"
url="jdbc:oracle:thin:@(DESCRIPTION=(ENABLE=BROKEN)(ADDRESS=(PROTOCOL=tcp)(PORT=1521)(HOST=localhost))(CONNECT_DATA=(SID=xe)))"
        driverClassName="oracle.jdbc.driver.OracleDriver" maxTotal="20"
        maxIdle="10" maxWaitMillis="5000" minEvictableIdleTimeMillis="300000"
        timeBetweenEvictionRunsMillis="600000" validationInterval="600000"
        validationQuery="select 1 from dual" testOnBorrow="true" auth="Container"
        /> <!-- accessToUnderlyingConnectionAllowed="true" -->
</Context>
private static final String JNDI_SUBCONTEXT = "java:comp/env/";
public Connection getDataSourceConnection() {
    Connection connection = null;
    try {
        InitialContext initialContext = new InitialContext();
        String datasourceName = (String) initialContext.lookup(JNDI_SUBCONTEXT + "/contexEnvironmentDataSource");
        javax.sql.DataSource dataSource = (DataSource) initialContext.lookup(JNDI_SUBCONTEXT + "/" + datasourceName);
        connection = dataSource.getConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return connection;
}
⚠️ **GitHub.com Fallback** ⚠️