我正在使用 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
。这是错误吗?