Oracle encoding format: UTF8
package com.aaron.dao;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class ConnectCots {
//private final String UID="RCOTRPR1";
//private final String PWD="RCOTRPR1";
private final String UID="RCOTRPR1";
private final String PWD="RCOTRPR1";
private final String DRIVER="oracle.jdbc.driver.OracleDriver";
private final String DBURL = "jdbc:oracle:thin:@192.168.70.54:5201:a3cotr";
private final static String qry="SELECT PAYER_ACC_NO AS EILINE1,PAYER_ACC_NAME AS EILINE2," +
"PAYER_BANK_NAME AS EILINE3,REMARK AS EILINE4 FROM COTS_TAB_DIPS_DETAILS";
public ConnectCots(){
try{
Class.forName(DRIVER);
}catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}
}
public Connection getCon(){
Connection conn=null;
try{
conn=DriverManager.getConnection(DBURL,UID,PWD);
}catch(SQLException e){
System.out.println(e.getMessage());
}
return conn;
}
public void close(Connection con){
if(con!=null){
try{
con.close();
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
public ArrayList exeQuery(String sql) throws UnsupportedEncodingException{
ArrayList<String> result=new ArrayList<String>();
Connection connect;
PreparedStatement stmt;
ResultSet rs;
connect=this.getCon();
StringBuffer st=new StringBuffer("");
String filename="d:/aaron.txt";
PrintWriter pw=null;
try {
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF8")),true);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
stmt=connect.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
while(rs.next()){
/*
//aaron
//if(rs.getInt(4)>1 && !rs.isFirst()){
if(rs.getInt(4)>1 && rs.previous()){
if( rs.getString(3).startsWith("BIN") ){
rs.next();
break;
}else{
rs.next();
}
}*/
pw.write(rs.getString("EILINE1")); //get the data from db and write to file by UTF8 format
// st.append(new String(rs.getString("EILINE1").getBytes("UTF8"),"GB2312"));
//st.append("|");
// st.append(new String(rs.getString("EILINE2").getBytes("UTF8"),"GB2312"));
// st.append("|");
st.append(rs.getString("EILINE3"));
st.append("|");
st.append(rs.getString("EILINE4"));
//aaron
//String family=rs.getString(1);
//result.add(family);
}
pw.close();
rs.close();
stmt.close();
}catch(SQLException e){
System.out.println(e.getMessage());
}
this.close(connect);
System.out.println(st.toString());
return result;
}
public void getData(String sql){
Connection connect;
PreparedStatement stmt=null;
ResultSet rs=null;
connect=this.getCon();
try{
stmt=connect.prepareStatement(sql);
rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("COTS_UID")+"::"+rs.getString("CUSTNUM"));
}
return;
}catch(SQLException e){
System.out.println(e.getMessage());
}
finally{
System.out.println("Enter the finally");
try{
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
this.close(connect);
} catch (Exception ePstmt) {
System.out.println(" Error in Closing Prepared Statement");
}
System.out.println("End the finally");
}
}
public void sbBuffer(){
StringBuffer sb1=new StringBuffer();
sb1.append("hello");
StringBuffer sb2=sb1;
sb2.append(" World");
System.out.println("SB1:"+sb1.toString());
System.out.print("SB2:"+sb2.toString());
}
public static void main(String[] args) {
ConnectCots aaron =new ConnectCots();
//String test="select ACCT_NUM, length(ACCT_NUM) from COTS_TAB_SWP_KR where to_char(COTS_UPD_DT,'YYYYMMDD')='20070626'";
String test="SELECT BENE.COTS_UID COTS_UID,BENE.CUSTNUM CUSTNUM FROM COTS_TAB_BENE_ADVICE BENE WHERE BRANCH=712 AND DELIVERYSTATUS='FAIL'";
String kk="SELECT BENE.COTS_UID COTS_UID,BENE.BRANCH BRANCH,BENE.CUSTNUM CUSTNUM," +
"BENE.TRANSACTIONREF TRANSACTIONREF,BENE.DELIVERYMEDIA DELIVERYMEDIA," +
"BENE.FILENAME FILENAME,BENE.PAYMENTCCY PAYMENTCCY,BENE.PAYMENTAMT PAYMENTAMT," +
"BENE.ACCTNUM ACCTNUM,SITE.EMAILADR EMAILADR " +
"FROM COTS_TAB_BENE_ADVICE BENE,COTS_TAB_BUSINESS_SITE SITE " +
"WHERE BENE.BRANCH=712 AND BENE.DELIVERYSTATUS='FAIL' " +
"AND BENE.BRANCH=SITE.BRANCH " +
"AND BENE.CUSTNUM=SITE.CUSTNUM " +
"AND SITE.SEQUENCENO=1 " +
"ORDER BY BENE.CUSTNUM";
aaron.getData(kk);
}
}