當前位置:首頁 » 編程語言 » mysqlblobjava

mysqlblobjava

發布時間: 2023-03-21 12:29:00

⑴ jsp/java讀取mysql的blob類型欄位中文亂碼問題 代碼如下,但是出來的是亂碼

知道了.
(2):手動插入中文數據,insert.....然後在mysql的窗口中查看是否正常顯示.
(3):根據測試結果確定是頁面問題還是資料庫設置問題
1:如果是頁面問題,相信你知道,怎麼解決.
2:如果是數據插入以後就變成了亂碼,那就是你的資料庫字元集有問題,找到my.ini
[client]
port=3306
[mysql]
加上:default-character-set=gbk
也可以通過資料庫設置向導來設置(windows下才有)
3:如果還是亂碼的話那就把頁面和資料庫的字元設置成一樣的就好了
希望對你有幫助
一般還是建議使用jbk
另外,虛機團上產品團購,超級便宜

⑵ mysql blob在java中轉化成string類型,列印出來[B@a99013是什麼類型的數據,怎樣在頁面顯示

從mysql中去取出的是Blob類型,要轉String通過new String(blob.getBytes((long)1, (int)blob.length()));來轉,就是想要的數據

⑶ 使用java語言操作,如何來實現MySQL中Blob欄位的存取

/**
* Title: BlobPros.java
* Project: test
* Description: 把圖片存入mysql中的blob欄位,並取出
* Call Mole: mtools資料庫中的tmp表
* File: C:downloadsluozsh.jpg
* Copyright: Copyright (c) 2003-2003
* Company: uniware
* Create Date: 2002.12.5
* @Author: ChenQH
* @version 1.0 版本*
*
* Revision history
* Name Date Description
* ---- ---- -----------
* Chenqh 2003.12.5 對圖片進行存取
*
* note: 要把資料庫中的Blob欄位設為longblob
*
*/

//package com.uniware;

import java.io.*;
import java.util.*;
import java.sql.*;

public class BlobPros
{
private static final String URL = "jdbc:mysql://10.144.123.63:3306/mtools?user=wind&password=123&useUnicode=true";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;

public BlobPros()
{
}

/**
* 向資料庫中插入一個新的BLOB對象(圖片)
* @param infile 要輸入的數據文件
* @throws java.lang.Exception
*/
public void blobInsert(String infile) throws Exception
{
FileInputStream fis = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);

file = new File(infile);
fis = new FileInputStream(file);
//InputStream fis = new FileInputStream(infile);
pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");
pstmt.setString(1,file.getName()); //把傳過來的第一個參數設為文件名
//pstmt.setBinaryStream(2,fis,(int)file.length()); //這種方法原理上會丟數據,因為file.length()返回的是long型
pstmt.setBinaryStream(2,fis,fis.available()); //第二個參數為文件的內容
pstmt.executeUpdate();
}
catch(Exception ex)
{
System.out.println("[blobInsert error : ]" + ex.toString());
}
finally
{
//關閉所打開的對像//
pstmt.close();
fis.close();
conn.close();
}
}

/**
* 從資料庫中讀出BLOB對象
* @param outfile 輸出的數據文件
* @param picID 要取的圖片在資料庫中的ID
* @throws java.lang.Exception
*/

public void blobRead(String outfile,int picID) throws Exception
{
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];

try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select pic from tmp where id=?");
pstmt.setInt(1,picID); //傳入要取的圖片的ID
rs = pstmt.executeQuery();
rs.next();

file = new File(outfile);
if(!file.exists())
{
file.createNewFile(); //如果文件不存在,則創建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("pic");
int size = 0;
/* while(size != -1)
{
size = is.read(Buffer); //從資料庫中一段一段的讀出數據
//System.out.println(size);
if(size != -1) //-1表示讀到了文件末
fos.write(Buffer,0,size);
} */
while((size = is.read(Buffer)) != -1)
{
//System.out.println(size);
fos.write(Buffer,0,size);
}

}
catch(Exception e)
{
System.out.println("[OutPutFile error : ]" + e.getMessage());
}
finally
{
//關閉用到的資源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}

public static void main(String[] args)
{
try
{

BlobPros blob = new BlobPros();
//blob.blobInsert("C:Downloadsluozsh1.jpg");
blob.blobRead("c:/downloads/1.jpg",47);
}
catch(Exception e)
{
System.out.println("[Main func error: ]" + e.getMessage());
}
}
}

⑷ java mysql資料庫 Blob亂碼 求救。

應該是
字元集
的問題。
最好都設成utf-8
包括資料庫,後台,還有前台頁面的編碼

⑸ java 以二進制流的方式讀取mysql 中的blob文件,並寫入本地文件夾

//配置資料庫連接驅動

String sql = xxxxxxxx;//要查詢的sql
PreparedStatement ps = conn.prepareStatement(sql);
String path = xxxxxxx;
ResultSet rs = ps.executeQuery();
while (rs.next()) {
InputStream is = rs.getBlob(x).getBinaryStream();//x為要取的BLOB位置

FileOutputStream os = new FileOutputStream(path + "//"
+ "存放的文件名"+「.zip」);
byte[] buff = new byte[1024];
while ((is.read(buff)) != -1) {
os.write(buff);
}
os.close();
is.close();
}
ps.close();
conn.close();

熱點內容
我的世界ec伺服器怎麼獲得 發布:2024-03-29 21:21:44 瀏覽:708
小米4設置限制的訪問 發布:2024-03-29 21:21:10 瀏覽:405
linux向伺服器上傳文件 發布:2024-03-29 21:17:20 瀏覽:928
腳本健康cpu佔用率報警 發布:2024-03-29 21:16:42 瀏覽:254
vivox9什麼配置參數 發布:2024-03-29 21:08:09 瀏覽:936
傳奇計時器刷怪腳本 發布:2024-03-29 20:50:18 瀏覽:653
哇哇賺錢腳本 發布:2024-03-29 20:23:53 瀏覽:997
兩氣體壓縮 發布:2024-03-29 20:18:09 瀏覽:994
易語言教程解壓 發布:2024-03-29 20:18:00 瀏覽:786
我老爸的密碼是什麼 發布:2024-03-29 20:03:50 瀏覽:248