`
aaron_ch
  • 浏览: 177365 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Read data from DB(UTF8) and convert other encoding

    博客分类:
  • Java
阅读更多

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);
  
   
  
  }
 }

 

 
分享到:
评论

相关推荐

    gb2312,utf-8,utf-8-bom等编码格式的互相转换

    byte[] utf8Bytes = Encoding.UTF8.GetBytes(utf8String); string gb2312String = Encoding.GetEncoding("gb2312").GetString(utf8Bytes); ``` 3. 如果需要读取有BOM的UTF-8文件并转换为其他编码,可以先去除BOM,...

    Python-convert2utf将目录下的全部源文件转成UTF8编码

    标题"Python-convert2utf将目录下的全部源文件转成UTF8编码"指的是使用Python编写的一个脚本或工具,该工具能够遍历指定目录,检测并转换其中的GB、GBK以及其他非UTF-8编码的文本文件和源代码文件,统一转换为UTF-8...

    Java解决UTF-8的BOM问题

    在Java编程中,UTF-8编码是一个非常常见且广泛使用的字符编码格式,它能支持全球大部分语言的字符表示。然而,UTF-8有一个特殊特性,那就是它可以带有Byte Order Mark(BOM),这是一个特殊的字节序列,用于标识数据...

    web scraping with python collecting more data from the modern web 2nd

    Learn web scraping and crawling techniques to access unlimited data from any web source in any format. With this practical guide, you’ll learn how to use Python scripts and web APIs to gather and ...

    gbk转Utf8_编码转换_firegbi_GBK转UTF-8_

    with codecs.open('新文件名', 'w', encoding='utf-8') as utf8_file: utf8_file.write(content) ``` 在上述代码中,`codecs.open()`函数用于打开文件,并指定编码方式。`'r'`表示读取模式,`'w'`表示写入模式。...

    WPF XAML 文件格式批量修改为Utf8格式

    with open(filepath, 'w', encoding='utf-8') as f: f.write(content) convert_to_utf8('path/to/your/wpf/project') ``` 这个脚本会遍历指定的目录,并将所有以`.xaml`结尾的文件从UTF-16转换为UTF-8。在实际...

    utf-8 互转 gb2312 转码

    with open('output.txt', 'w', encoding='utf-8') as f_out: f_out.write(content) ``` 在这个过程中,`'input.txt'`文件以GB2312编码读取,内容被解码为Unicode,然后以UTF-8编码写入到`'output.txt'`文件中。 ...

    文件编码转换(utf8与gbk相互转换)

    with codecs.open('新文件.utf8', 'w', encoding='utf-8') as f_out: f_out.write(content) ``` 同样,如果要将UTF-8文件转换为GBK,只需将上述代码中的`'gbk'`和`'utf-8'`位置互换即可。 在实际操作中,需要注意...

    GBK和UTF8相互转换

    with open('UTF8_file.txt', 'w', encoding='UTF-8') as outfile: outfile.write(content) # 从UTF-8编码文件转换为GBK编码文件 with open('UTF8_file.txt', 'r', encoding='UTF-8') as infile: content = infile...

    XML and Web Technologies for Data Sciences with R

    displaying spatial-temporal displays with Google Earth, and generating code from descriptions of data structures to read and write data. These topics demonstrate the rich possibilities and ...

    C#连接数据库UFT-8转为GB2312格式

    byte[] gb2312Bytes = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("GB2312"), utf8Bytes); return Encoding.GetEncoding("GB2312").GetString(gb2312Bytes); } } ``` 在上述代码中,`ConvertUtf8...

    判断文件编码格式和批量将gbk转为utf-8

    with codecs.open(dest_path, 'w', encoding='utf-8') as dest_file: dest_file.write(content) base_dir = 'TestProject' # 假设TestProject目录下包含需要转换的GBK文件 for filename in os.listdir(base_dir):...

    Python Data Structures and Algorithms [2017]

    Python Data Structures and Algorithms by Benjamin Baka English | 30 May 2017 | ASIN: B01IF7NLM8 | 310 Pages | AZW3 | 6.63 MB Key Features A step by step guide, which will provide you with a thorough...

    VBA Fans读取和写入UTF-8格式文本文件

    本主题将深入探讨如何使用VBA来读取和写入UTF-8编码的文本文件,这对于处理多语言数据或需要跨系统兼容性的项目尤为重要。 1. **UTF-8编码介绍** UTF-8是一种广泛使用的Unicode字符编码,能够表示Unicode字符集中...

    Utf-8编码与解码(vb6代码)

    UTF-8编码是一种广泛使用的Unicode字符编码方案,它在互联网上尤其常见,因为其兼容性和效率。UTF-8的特点是每个Unicode字符可以被编码为1到4个字节,其中ASCII字符(基本的英文字符)仅需1个字节。这使得UTF-8在...

    demo_gbk_2_utf8.zip

    标题中的"demo_gbk_2_utf8.zip"表明这是一个压缩包文件,它的主要目标是将GBK编码的文件转换为UTF-8编码。在信息技术领域,字符编码是非常关键的一部分,GBK和UTF-8是两种常见的字符编码标准。GBK是基于GB2312扩展的...

    read_data_from_PhysioNet.m

    read_data_from_PhysioNet.m

    STM32学习一:那些MDK5的坑

    STM32学习一:那些MDK5的坑,讲解mdk5在开发stm32的时候一些问题和解决方法

Global site tag (gtag.js) - Google Analytics