Created
October 11, 2016 15:26
-
-
Save trplll/ec618d79ede555de63e3eede3be62f48 to your computer and use it in GitHub Desktop.
Basic JDBC connection Utility for multiple databases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.trplcd.jdbc; | |
/** | |
* Created by ace01359 on 10/10/2016. | |
*/ | |
public enum DBType{ | |
AS400, MYSQLDB | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.trplcd.jdbc; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
/** | |
* Created by ace01359 on 10/10/2016. | |
*/ | |
public class DBUtil { | |
static final String mySqlUserName = "Username"; | |
static final String mySqlPassword = "Password"; | |
static final String mySqlConString = "jdbc://connection/String"; | |
static final String aS400UserName = "Username"; | |
static final String aS400Password = "Password"; | |
static final String aS400ConString = "jdbc://connection/String"; | |
public static Connection getConnection(DBType dbType) throws SQLException { | |
switch (dbType) { | |
case AS400: | |
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver()); | |
return DriverManager.getConnection(aS400ConString, aS400UserName, aS400Password); | |
case MYSQLDB: | |
return DriverManager.getConnection(mySqlConString, mySqlUserName, mySqlPassword); | |
default: | |
return null; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.trplcd.jdbc; | |
import java.sql.Connection; | |
import java.sql.SQLException; | |
/** | |
* Created by ace01359 on 10/10/2016. | |
*/ | |
public class TestSqlCon { | |
public static void main(String[] args) throws SQLException { | |
Connection conn1 = null; | |
Connection conn2 = null; | |
try { | |
conn1 = DBUtil.getConnection(DBType.AS400); | |
System.out.println("Connection Established to AS400"); | |
} catch (SQLException e) { | |
System.err.println(e.getMessage()); | |
} finally { | |
conn1.close(); | |
System.out.println("AS400 CON CLOSED"); | |
} | |
try { | |
conn2 = DBUtil.getConnection(DBType.MYSQLDB); | |
System.out.println("Connection Established to MySQL"); | |
} catch (SQLException e) { | |
System.err.println(e.getMessage()); | |
} finally { | |
conn2.close(); | |
System.out.println("MYSQL CON CLOSED"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment