1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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")); } |