【发布时间】:2012-11-18 19:57:47
【问题描述】:
我正在使用 BLOb 支持从 MySQl 插入和读取。(JDBC) 我可以这样做,但是当它读取时,它只有几个 kb。我不知道为什么。 这是工作代码:
import java.sql.*;
import java.io.*;
public class InsertAndRetrieveImage {
public static void main(String[] args) throws SQLException, FileNotFoundException, IOException {
int id=7;
String connectionURL = "jdbc:mysql://127.0.0.1:3306/newdb";;
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connectionURL, "root", "sesame");
PreparedStatement ps = null;
ps=con.prepareStatement("INSERT IGNORE INTO image VALUES(?,?,?)");
File file = new File("e:/anarkali.wav");
FileInputStream fs = new FileInputStream(file);
try{
System.out.println(fs.available());
ps.setInt(1,id);
ps.setBinaryStream(2,fs,fs.available());
String filen=file.getName();
ps.setString(3,filen);
int i = ps.executeUpdate();
String filename="filename";OutputStream os=null;byte[] content=null;
ps= con.prepareStatement("SELECT fs from image where id=7");
ResultSet rs = ps.executeQuery();
System.out.println(rs);
while(rs.next()) {
Blob blob = rs.getBlob("fs");
content = blob.getBytes(1, (int) blob.length());
os = new FileOutputStream(new File("e://testanar.wav"));
}
os.write(content);
os.close();
con.close();
ps.close();}
catch(SQLException e)
{System.out.println(e.getMessage());
}
}catch(Exception ex){}
}
是读写时的问题吗?我的 BLOb 的大小有点65535。这是错误吗?
【问题讨论】:
-
blob.length()的实际值是多少(就在Blob blob = rs.getBlob("fs");之后)? -
@YaK 它没有显示 blob.length()。它抛出异常 null。有什么问题??我知道它的大小和内容,但是什么?
标签: java mysql database file blob