import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import com.ibm.db2.jcc.DB2Driver;


public class SelectSql {
//	private BufferedWriter bw = null;
//	public SelectSql(){
//
//	}

	public static String getFileContent(String filename) throws Exception{
		BufferedReader bs = new BufferedReader(new FileReader(new File(filename)));
		StringBuffer sb = new StringBuffer();
		String line = null;
		while((line = bs.readLine()) != null){
			sb.append(line).append("\n");
		}
		return sb.toString();
	}



	public static void main(String[] args) throws Exception{
		args = new String[]{
			"oracle.jdbc.driver.OracleDriver",
			"jdbc:oracle:thin:@192.168.1.171:32777:xe",
			"system",
			"oracle",
			""


		};

		args[4] = " SELECT 'adfadfadfdfafd' from dual  ";
		//args[4] = " SELECT *   FROM CM_ATTACH_FILE where o_name like '%adobecc2014%' ";

		String driverName = args[0];
		String url = args[1];
		String user = args[2];
		String pass = args[3];
		String filename = args[4];

		Class.forName(driverName);
		Connection conn = DriverManager.getConnection(url, user, pass);

		Statement stmt = conn.createStatement();

		String sql = filename;//getFileContent(filename);
		ResultSet rs = stmt.executeQuery(sql);
		ResultSetMetaData rsm = rs.getMetaData();
		int columnCount = rsm.getColumnCount();

		while(rs.next()){
			for(int i=1; i<=columnCount; i++){
				String value = rs.getString(i);
				value = value == null ? "" : value;
				value.replaceAll("\\t", "#tab#");
			}
		}
		stmt.close();
		conn.close();
	}
}
