java读取blob全身乱码

一、BLOB操作
1、入库
(1)JDBC方式
    //通过JDBC获得数据库连接
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
    con.setAutoCommit(false);
    Statement st = con.createStatement();
    //插入一个空对象empty_blob()
    st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
    //锁定数据行进行更新,注意“for update”语句
    ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
    if (rs.next())
    {
        //得到java.sql.Blob对象后强制转换为oracle.sql.BLOB
        oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");
        OutputStream outStream = blob.getBinaryOutputStream();
        //data是传入的byte数组,定义:byte[] data
        outStream.write(data, 0, data.length);
    }
    outStream.flush();
    outStream.close();
    con.commit();
    con.close();
(2)JNDI方式
    //通过JNDI获得数据库连接
    Context context = new InitialContext();
    ds = (DataSource) context.lookup("ORA_JNDI");
    Connection con = ds.getConnection();
    con.setAutoCommit(false);
    Statement st = con.createStatement();
    //插入一个空对象empty_blob()
    st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
    //锁定数据行进行更新,注意“for update”语句
    ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
    if (rs.next())
    {
        //得到java.sql.Blob对象后强制转换为weblogic.jdbc.vendor.oracle.OracleThinBlob(不同的App Server对应的可能会不同)
        weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob("BLOBATTR");
        OutputStream outStream = blob.getBinaryOutputStream();
        //data是传入的byte数组,定义:byte[] data
        outStream.write(data, 0, data.length);
    }
    outStream.flush();
    outStream.close();
    con.commit();
    con.close();
2、出库
    //获得数据库连接
    Connection con = ConnectionFactory.getConnection();
    con.setAutoCommit(false);
    Statement st = con.createStatement();
    //不需要“for update”
    ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");
    if (rs.next())
    {
        java.sql.Blob blob = rs.getBlob("BLOBATTR");
        InputStream inStream = blob.getBinaryStream();
        //data是读出并需要返回的数据,类型是byte[]
        data = new byte[input.available()];
        inStream.read(data);
        inStream.close();
<pre class="java" name="code">conn = this.getConnection();
            conn.setAutoCommit(false);
            java.sql.Statement st = conn.createStatement();
            rs= st.executeQuery(sql);
     BLOB inblob = null;
     if (rs.next()) {
    inblob = (BLOB) rs.getBlob("BLOBATTR");
    }
     data=inblob.getBytes(1,(int)inblob.length());//这个就是数据
    }
    inStream.close();
    con.commit();
    con.close();  

二、CLOB操作
1、入库
(1)JDBC方式
    //通过JDBC获得数据库连接
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
    con.setAutoCommit(false);
    Statement st = con.createStatement();
    //插入一个空对象empty_clob()
    st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
    //锁定数据行进行更新,注意“for update”语句
    ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
    if (rs.next())
    {
        //得到java.sql.Clob对象后强制转换为oracle.sql.CLOB
        oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("CLOBATTR");
        Writer outStream = clob.getCharacterOutputStream();
        //data是传入的字符串,定义:String data
        char[] c = data.toCharArray();
        outStream.write(c, 0, c.length);
    }
    outStream.flush();
    outStream.close();
    con.commit();
    con.close();
(2)JNDI方式
    //通过JNDI获得数据库连接
    Context context = new InitialContext();
    ds = (DataSource) context.lookup("ORA_JNDI");
    Connection con = ds.getConnection();
    con.setAutoCommit(false);
    Statement st = con.createStatement();
    //插入一个空对象empty_clob()
    st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
    //锁定数据行进行更新,注意“for update”语句
    ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
    if (rs.next())
    {
        //得到java.sql.Clob对象后强制转换为weblogic.jdbc.vendor.oracle.OracleThinClob(不同的App Server对应的可能会不同)
        weblogic.jdbc.vendor.oracle.OracleThinClob clob = (weblogic.jdbc.vendor.oracle.OracleThinClob) rs.getClob("CLOBATTR");
        Writer outStream = clob.getCharacterOutputStream();
        //data是传入的字符串,定义:String data
        char[] c = data.toCharArray();
        outStream.write(c, 0, c.length);
    }
    outStream.flush();
    outStream.close();
    con.commit();
    con.close();
2、出库
    //获得数据库连接
    Connection con = ConnectionFactory.getConnection();
    con.setAutoCommit(false);
    Statement st = con.createStatement();
    //不需要“for update”
    ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1");
    if (rs.next())
    {
        java.sql.Clob clob = rs.getClob("CLOBATTR");
        Reader inStream = clob.getCharacterStream();
        char[] c = new char[(int) clob.length()];
        inStream.read(c);
        //data是读出并需要返回的数据,类型是String
        data = new String(c);
        inStream.close();
    }
    inStream.close();
    con.commit();
    con.close();  

需要注意的地方:
1、java.sql.Blob、oracle.sql.BLOB、weblogic.jdbc.vendor.oracle.OracleThinBlob几种类型的区别
2、java.sql.Clob、oracle.sql.CLOB、weblogic.jdbc.vendor.oracle.OracleThinClob几种类型的区别
公司项目中的用法(博客):
入库:先插一个oracle.sql.CLOB.empty_lob()进去,然后
String updateBaseSourceSql = "select content from mb_baseSource where id = ? for update";
    conn.setAutoCommit(false);
    ps = conn.prepareStatement(updateBaseSourceSql);
    ps.setLong(1, result);
    ResultSet rs = ps.executeQuery();
    oracle.sql.CLOB clob = null;
    if (rs.next()) {
     clob = (oracle.sql.CLOB) rs.getClob(1);
    }
    Writer wr = clob.getCharacterOutputStream();
    wr.write(baseSource[4]);
    wr.flush();
    wr.close();
    rs.close();
    ps.close();
    conn.commit();
出库:
findBaseSourceSql = "select content from mb_baseSource where id = ?";
   ps = conn.prepareStatement(findBaseSourceSql);
   ps.setLong(1, sourceID);
   rs = ps.executeQuery();
   if (rs.next()) {
    CLOB clob = (oracle.sql.CLOB) rs.getClob(1);
    if (clob != null) {
     Reader is = clob.getCharacterStream();
     BufferedReader br = new BufferedReader(is);
     String s = br.readLine();
     while (s != null) {
      result[6] += s;
      s = br.readLine();
     }
    }
   }
   rs.close();
   ps.close();
   conn.close();  
时间: 2024-10-02 05:10:37

java读取blob全身乱码的相关文章

java读取.properties文件乱码

1.config.properties文件写不进中文,写进去都变成了unicode,解决办法是右键该文件--Properties--Resource--Text file encoding ,选other,我将other改为了UTF-8,这样可以写进去中文,但是读取时又变成乱码了. 2,解决读取乱码: String content = new String(PropertiesConfig.getProperty("mail.content").getBytes("ISO88

java读取blob,clob转换为字符串

直接上代码: package com.it.test; import java.io.BufferedReader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLExceptio

java 读取文件中文乱码问题

很少写java io的代码,今天整了一个发现 本地调试好好的,放到jmeter里就打印乱码.一番折腾,终于搞定~直接上代码: List<Order> orderList = new ArrayList<Order>(); InputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; try { String jsonStr = ""; //fis = new Fil

Java读取/更新Oracle数据库blob字段

在写java程序过程中,如何读取Oracle数据库表某类型为blob的字段? 以下是我在写程序的时候一种解决方法.核心语句.(传上来做了修改,格式不要学习,养成良好习惯) 详细请参考: 读取序列ID:http://blog.csdn.net/yzsind/article/details/6918506 BLOB相关:http://jslfl.iteye.com/blog/1771949 http://www.linuxidc.com/Linux/2011-08/40218.htm http://

Java读取UTF-8格式文件第一行出现乱码——问号“?”及解决 And Java读带有BOM的UTF-8文件乱码原因及解决方法

测试例子: Java读取UTF-8的txt文件第一行出现乱码"?"及解决 test.txt文件内容: 1 00:00:06,000 --> 00:00:06,010 <b>Allerleirauh</b> (2012) <i>dTV - Das Erste - 20. Januar 2013</i> 2 00:00:10,280 --> 00:00:12,680 Was geh?rt zu einer guten Suppe?

JAVA读取Oracle数据库BLOB字段数据文件并保存到本地文件

******JAVA读取Oracle数据库BLOB字段数据文件并保存到本地文件****** package com.bo.test; import java.io.FileOutputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import

java读取文件乱码

List<String> lines=new ArrayList<String>(); BufferedReader br = new BufferedReader(new FileReader(fileName)); String line = null; while ((line = br.readLine()) != null) { lines.add(line); } br.close(); Java的I/O类处理如图: Reader 类是 Java 的 I/O 中读字符的

Java读取数据库数据生成柱状图

此案例是用swing显示数据的.需要引入jfreechart相关包,不同版本可能包不相同,本人用的是 此案例在ssi框架下会报错,不用框架就没问题. Java后台逻辑代码: public class BarChart { ChartPanel frame1; public BarChart() { CategoryDataset dataset = getDataSet(); JFreeChart chart = ChartFactory.createBarChart3D("水果",

java中遇到字符串乱码的问题(转)

转发博客地址:http://blog.csdn.net/greenqingqingws/article/details/7395213 最近遇到一个问题,Java读取文本文件(例如csv文件.txt文件等),遇到中文就变成乱码.读取代码如下: [java] view plaincopyprint? List<String> lines=new ArrayList<String>(); BufferedReader br = new BufferedReader(new FileRe