从BLOB数据类型中转义特殊字符

时间:2013-12-20 04:36:58

标签: java oracle text blob special-characters

我正在从Oracle数据库中读取BLOB数据类型表。但是特殊角色正在逃避。下面是我的代码。

public static void main(String args[]) throws FileNotFoundException

{

    System.out.println("Oracle Connect START.");
    Connection conn = null;

    String url = "jdbc:oracle:thin:@localhost:1521:";
    String dbName = "xe";
    String driver = "oracle.jdbc.OracleDriver";
    String userName = "T24DBA";
    String password = "ABCD123abcd";
    int locationofp=0;
    ResultSet orset = null;
    ResultSet rs1 = null;
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url+dbName,userName,password);
        Statement stmt = conn.createStatement();
        orset =stmt.executeQuery("select RECID, XMLRECORD from karthi");

        Blob lob = null;
        FileOutputStream fos = new FileOutputStream("C:/DataRead/fbnk.txt");
        DataOutputStream dos = new DataOutputStream(fos);

        while (orset.next()) {
            String RECID = orset.getString(1);


            lob=orset.getBlob("XMLRECORD");
            byte[] bdata = lob.getBytes(1, (int) lob.length());
            String text = new String(bdata);
            System.out.println("TEXT "+ text);
        }
    }

数据库中的记录是

EUR100011510þGBP100011200þGBP100011250þGBP100011500þGB

但我得到的输出是

EUR100011510�GBP100011200�GBP100011250�GBP100011500�GB

我的所有字段标记字符(þ)都在逃避。

任何帮助将不胜感激。过去两天我一直在努力

由于

2 个答案:

答案 0 :(得分:0)

原因是你的默认字符集不是UTF-8,你的特殊字符的字节是-61 -66(不是ascii)

尝试

System.out.println(new String (bdata, "UTF-8"));

实施例

    byte[] str = "EUR100011510þGBP100011200þGBP100011250þGBP100011500þGB".getBytes();

    try {
        System.out.println(new String (str, "UTF-8"));
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }

答案 1 :(得分:0)

确保存储在数据库中的数据采用UTF-8格式。 还要创建一个指定编码的新String:

String text = new String(bdata, "UTF-8");
System.out.println("TEXT "+ text);