public Connection getConnection(String hostname, int portnumber, String username, String password, String dBorSIDName) throws SQLException, ClassNotFoundException { String connectString = getConnectStringTemplate(); connectString = connectString.replaceAll("host", hostname); connectString = connectString.replaceAll("port", Integer.toString(portnumber)); connectString = connectString.replaceAll("dbname", dBorSIDName); logger.info("Connect to:"+ connectString); Connection conn = getConnection(getDriver(), connectString, username, password); return conn; }
@Override public ArrayList<ArrayList<String>> getDataForLimitedRows(DBType dbType, String hostname, int portnumber, String username, String password, String tableName, String dBorSIDName, int rowCount) throws SQLException, ClassNotFoundException { Connection conn = getConnection(hostname, portnumber, username, password, dBorSIDName); String query = "Select top " + rowCount + " * from " + tableName; logger.info("Execute:" + query); Statement s = conn.createStatement(); ResultSet r = s.executeQuery(query); if (r == null) { s.close(); return null; } ArrayList<ArrayList<String>> vals = parseResultSetIntoArrayListOfRows(r); r.close(); s.close(); return vals; }
@Override public ArrayList<ArrayList<String>> getSQLQueryDataForLimitedRows(DBType dbType, String hostname, int portnumber, String username, String password, String query, String dBorSIDName, int rowCount) throws SQLException, ClassNotFoundException { Connection conn = getConnection(hostname, portnumber, username, password, dBorSIDName); if(query.toUpperCase().indexOf("SELECT TOP") == -1) { //Add limit only if it doesnt exist, else sql will be invalid int idx = query.toUpperCase().indexOf("SELECT"); query = query.substring(idx+6); query = "SELECT TOP " + rowCount + query; } logger.info("Execute:" + query); Statement s = conn.createStatement(); ResultSet r = s.executeQuery(query); if (r == null) { s.close(); return null; } ArrayList<ArrayList<String>> vals = parseResultSetIntoArrayListOfRows(r); r.close(); s.close(); return vals; }