String driverName = "";
String driverConfig = "";
String testSql = "";
if (MYSQL.equals(type)) {
// Download mysql-connector-java-5.1.29-bin.jar from below
// http://dev.mysql.com/downloads/connector/j/
driverName = "com.mysql.jdbc.Driver";
driverConfig = "jdbc:mysql://" + host + "/" + name + "?user=" + user + "&password=" + pass;
// host = 192.168.1.1:3306
// name = TESTDB
// user = ABCD
// pass = 1234
testSql = "select 'MySQL connection test' as test";
} else if (ORACLE.equals(type)) {
// Download ojdbc6.jar from below
// http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
driverName = "oracle.jdbc.driver.OracleDriver";
driverConfig = "jdbc:oracle:thin:" + user + "/" + pass + "@" + host;
// host = 192.168.1.1:1521:XE
// name = TESTDB
// user = ABCD
// pass = 1234
testSql = "select 'Oracle connection test' as test from dual";
} else if (SQLSERVER.equals(type)) {
// Download sqljdbc4.jar from below
// http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774
driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
driverConfig = "jdbc:sqlserver://" + host + ";databaseName=" + name + ";user=" + user + ";password=" + pass + ";";
// host = 192.168.1.1\TEST
// name = TESTDB
// user = ABCD
// pass = 1234
testSql = "select 'SQLServer connection test' as test";
}
Class.forName(driverName).newInstance();
conn = DriverManager.getConnection(driverConfig);
if (ORACLE.equals(type)) {
conn.createStatement().execute("alter session set current_schema=" + name);
}
ResultSet rs = conn.createStatement().executeQuery(testSql);
while (rs.next()) {
System.out.println(rs.getString("test"));
}