關於Mysql資料庫longblob格式資料的插入com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V問題分析

來源:互聯網
上載者:User

標籤:

  當資料庫欄位為blob類型時 ,我們如果使用PreparedStatement中的setBinaryStream(int,InputStream,int)方法需要注意

  在向blob欄位類型中插入資料時,要使用javaio的inputstream,讀入檔案。
  而相反從blob欄位中讀出資料時,同樣使用javaio的inputstream,再用javaio的outputstream寫入檔案。

  同clob的樣本中的問題
  如果在設定位元組流的地方不加類型轉換的話,如下:
  stat.setBinaryStream(1, in, file.length());
  則會出現如下錯誤
  Exception in thread "main" java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
    at test.jdbc.BlobRW.create(BlobRW.java:38)
    at test.jdbc.BlobRW.main(BlobRW.java:24)

  後來看了看java和mysql的jdbc驅動兩方面的代碼,原因明白,原來是用的jdk1.6的版本中,有長度為long類型的方法。
  而對應的mysql的jdbc驅動jar中,還沒有實現。
  其進行類型轉換後,即可正常運行。運行代碼如下:

public void insertBlob() throws Exception {String strimg="E:/IBM/friend/src/abc.jpg";String strtext="E:/IBM/friend/src/abc.txt";String strarea="中國中國中國中國";conn = DBUtil.getConnection();String sql = "insert into userclob(id,name,photo,note) values(?,?,?,?)";this.pstm = conn.prepareStatement(sql);this.pstm.setInt(1, 2);this.pstm.setString(2, "中國2");File fileimg = new File(strimg);File filetext=new File(strtext);this.pstm.setString(3, strarea);this.pstm.setCharacterStream(4, new FileReader(fileimg),(int)fileimg.length());this.pstm.executeUpdate();this.pstm.close();conn.close();}

  

關於Mysql資料庫longblob格式資料的插入com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V問題分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.