从 BLOB 数据类型中识别特殊字符



我正在从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

我所有的字段标记字符(þ)都在转义。

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

谢谢

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

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

原因是您的默认字符集不是 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();
    }

最新更新