導航:首頁 > 編程語言 > javabyteblob

javabyteblob

發布時間:2022-12-10 06:39:14

java 中 blob轉字元串

//字元串轉blob
String str="哈哈哈哈哈哈";
java.sql.Blob bl=new SerialBlob(str.getBytes());
//blob轉字元串
BufferedReader bf=new BufferedReader(new InputStreamReader(bl.getBinaryStream()));
String temp="";
StringBuffer sb=new StringBuffer();
while((temp=bf.readLine())!=null)
{
sb.append(temp);
}

System.out.println(sb.toString());

⑵ 如何將byte類型轉換blob類型

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Main {
static String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";
static String username = "username";
static String password = "welcome";

public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);

String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "java.gif");
stmt.setString(2, "Java Official Logo");

File image = new File("D:\\a.gif");
FileInputStream fis = new FileInputStream(image);
stmt.setBinaryStream(3, fis, (int) image.length());
stmt.execute();

conn.commit();
fis.close();
conn.close();
}
}

⑶ java中向oracle資料庫blob欄位中插入byte[]相關問題, 高人來.

和blob無關吧

可能是你計算錯誤

保存blob前 輸入看看是什麼值

⑷ java中向oracle資料庫blob欄位中插入byte[]相關問題, 高人來.

你把一下的代碼加入到一個類里就可以了,我已經測試過了,由於字數的限制不能把完整的類發上來。

String PSQL = "insert into TESTBLOB(NUMCONTENTID,BLOBCONTENT) " + "values(?,EMPTY_BLOB())";
String SSQL = "select BLOBCONTENT from TESTBLOB where NUMCONTENTID = ? for update";
String USQL = "update TESTBLOB set BLOBCONTENT = ? where NUMCONTENTID = ?";

public WriteBLOB() throws Exception {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
ByteBuffer bb = ByteBuffer.allocate(10836);
bb.position(0);
bb.putInt(1);
//調用 insertBLOB 方法 的contentid 應該使用序列這里為了簡單就直接寫死了
//下面的插入方式應該是你想要的方法 在資料庫中會看到 0000 0001 0000 0000 0000....
//但是你這里好像沒有真正的是用 ByteBuffer
byte [] b1 = bb.array();
insertBLOB(conn, 1, b1);
//下面是使用 ByteBuffer 後的插入 在資料庫中會看到 0000 0001
//不知道你想要什麼樣子的,你自己選擇不吧
bb.flip();
byte [] b2 = new byte [bb.remaining()];
bb.get(b2);
insertBLOB(conn, 2, b2);
} catch (Exception ex) {
ex.printStackTrace();
conn.rollback();
} finally {
if (conn != null)
conn.close();
}
}

public void insertBLOB(Connection conn, int contentid, byte[] content)
throws Exception {
PreparedStatement pstmt = null;
PreparedStatement pstmt2 = null;
int id = contentid;
try {
pstmt = conn.prepareStatement(PSQL);
pstmt2 = conn.prepareStatement(USQL);
pstmt.setInt(1, id);
try {
pstmt.executeUpdate();//在資料庫中插入空對象
} catch (SQLException ex) {
ex.printStackTrace();
}
pstmt = conn.prepareStatement(SSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();//查詢新插入的記錄
oracle.sql.BLOB pc = null;
while (rs.next()) {
pc = (oracle.sql.BLOB) rs.getBlob(1);
}
byte[] data = content;
pc.putBytes(1, data);
pstmt2.setBlob(1, pc);
pstmt2.setInt(2, id);
pstmt2.executeUpdate();
} finally {
try {
pstmt.close();
pstmt2.close();
} catch (Exception EE) {
}
}
return;
}

⑸ java blob byte[] 互換(jdbc中)

覺得文件麻煩就在存之前轉換為String型 資料庫取出後在轉換成byte[]

不知道還有沒有別的方法

⑹ Java怎樣能把Oracle的Blob類型的內容導出到本地

BLOB blob = (BLOB) rs.getBlob(5);後,使用blob的getBytes()獲取byte[]數據或者使用getBinaryStream() 獲取流對象,這樣你就可以輸出到JSP頁面了

⑺ java中 已獲取byte[]對象a,如何將a轉成java.sql.Blob類型

Blob的
setBytes(long pos,
byte[] bytes)
方法不就可以嗎

⑻ 資料庫里存儲的圖片欄位定義類型為blob,java里對應的類型為byte[],怎麼將圖片顯示到jsp頁面

我把我以前收集的給你貼出來,希望能對你有所幫助
jsp編程從資料庫中取出圖片

1、讀取圖片數據testimageout.jsp文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.io.*" %>
<html>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
Statement stmt=con.createStatement();
ResultSet rs=null;
int id=Integer.parseInt(request.getParameter("id"));
String sql = "select image from picturenews where id='"+id+"'";
rs=stmt.executeQuery(sql);
while(rs.next())
{
ServletOutputStream sout = response.getOutputStream();
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b);i!=-1)
{
sout.write(b);
in.read(b);
}
sout.flush();
sout.close();
}
%>
<body>
</html>

2、取出所要顯示的圖片showimage.jsp文件
<%@ page contentType="text/html;charset=bg2312"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>顯示資料庫圖片測試頁</title>
</head>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
Statement stmt=con.createStatement();
String sql=new String();
sql = "select id from picturenews";
ResultSet rs=stmt.executeQuery(sql);
//顯示最後一條記錄的圖片
rs.last();
%>
<table>
<tr><td><img src='testimageout.jsp?id=<%=rs.getInt("id")%>'></td></tr>
</table>
</body>
</html>

⑼ java 如何將ByteArrayInputStream轉換成Blob類型 加急!!

private byte[] toByteArray(Blob fromBlob) throws SQLException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

return toByteArrayImpl(fromBlob, baos);

}

private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos) throws SQLException, IOException{
byte[] buf = new byte[10*1024*1024];
InputStream is = fromBlob.getBinaryStream();
try {
for (;;) {
int dataSize = is.read(buf);
if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
}
finally { if (is != null) {
try { is.close(); }
catch (IOException ex) { }
}
}
return baos.toByteArray();
}

⑽ java oracle數據blob的操作

//下面為一個完整的例子,如果用framework,需要做一定修改
寫入
publicbooleansaveWordFile(StringfilePath){
Filefile=new
File(filePath);
Connectionconn=getConnection();
try
{
java.sql.Statementst=conn.createStatement();

conn.setAutoCommit(false);
st.execute("insertintotable_name
values(1,empty_blob())");
ResultSetrs=
st
.executeQuery("selectid,wordfromtable_namewhere
id=1forupdate");
if(rs.next()){
BLOBblob
=(BLOB)rs.getBlob("word");
OutputStreamoutStream=
blob.getBinaryOutputStream();
InputStreamfin=new
FileInputStream(file);
byte[]b=new
byte[blob.getBufferSize()];
intlen=0;

while((len=fin.read(b))!=-1){
outStream.write(b,0,
len);
}
fin.close();

outStream.flush();
outStream.close();

conn.commit();
conn.close();
}
}
catch(SQLExceptione){
//TODOAuto-generatedcatch
block
e.printStackTrace();
}catch
(FileNotFoundExceptione){
//TODOAuto-generatedcatch
block
e.printStackTrace();
}catch(IOExceptione)
{
//TODOAuto-generatedcatchblock

e.printStackTrace();
}
returntrue;
}
讀取
publicvoidgetWordFile(Stringid){
Connectionconn=
getConnection();
java.sql.Statementst;
try
{
st=conn.createStatement();
ResultSetrs=
st.executeQuery("selectid,wordfromtable_namewhere
id='"+id+"'");
if(rs.next()){
BLOBblob=
(BLOB)rs.getBlob("word");
Filefile=new
File("c://filename.doc");
FileOutputStreamoutput=new
FileOutputStream(file);
InputStreaminput=
blob.getBinaryStream();
byte[]buffer=new
byte[1024];
inti=0;
while((i=
input.read(buffer))!=-1){
output.write(buffer,0,
i);
}
}
}catch(Exceptione)
{
//TODOAuto-generatedcatchblock

e.printStackTrace();
}
}
修改
publicvoipdateblob(Stringid){
Connection
conn=getConnection();
Statementstem=null;
ResultSetrs=null;

try{
conn.setAutoCommit(false);

stem=conn.createStatement();
rs=stem.executeQuery("selectword
fromtable_namewhereid='"+id+"'forupdate");

if
(rs.next()){
BLOBblob=(BLOB)rs.getBlob("word");

OutputStreamoutStream=blob.getBinaryOutputStream();

InputStreamfin=newFileInputStream("c://2.doc");

byte[]b=newbyte[blob.getBufferSize()];
int
len=0;
while((len=fin.read(b))!=-1){

outStream.write(b,0,len);
}

fin.close();
outStream.flush();

outStream.close();
conn.commit();

conn.close();
}
}catch(Exceptionex){

try{
conn.rollback();
}catch(SQLExceptione){
//TODO
Auto-generatedcatchblock
e.printStackTrace();
}

System.out.println(ex.getMessage());
}
}

閱讀全文

與javabyteblob相關的資料

熱點內容
電腦盤文件夾如何平鋪 瀏覽:267
相機卡滿了沒文件夾 瀏覽:749
如何批量快速壓縮視頻 瀏覽:432
我的世界如何加入ice伺服器 瀏覽:873
兄弟cnc編程說明書 瀏覽:204
php閃電入門教程學習 瀏覽:152
金岳霖邏輯pdf 瀏覽:938
linuxtomcat線程 瀏覽:77
pboc長度加數據加密 瀏覽:187
英雄聯盟國際服手游怎麼下安卓 瀏覽:297
程序員的思路 瀏覽:234
只能用命令獲得的四種方塊 瀏覽:358
怎麼用命令方塊防止開創造 瀏覽:807
掃描版的pdf 瀏覽:790
編程貓怎樣做3d游戲 瀏覽:207
怎麼查找雲伺服器上的ftp 瀏覽:156
我的世界伺服器如何注冊賬號 瀏覽:934
統計英文字元python 瀏覽:424
linux信息安全 瀏覽:910
壓縮機接線柱爆 瀏覽:1001