日期:2014-05-20  浏览次数:20666 次

请各位帮忙看一下错误的所在,怎样解决啊.我尝试使用jdbc来存取大对象,如mp3,但是出现了如下的提示:
请各位帮忙看一下错误的所在,怎样解决啊.我尝试使用jdbc来存取大对象,如mp3,但是出现了如下的提示:

com.mysql.jdbc.PacketTooBigException:
Packet for query is too large (3846199 > 1048576). You can change this value on the server 

by setting the max_allowed_packet' variable.

数据库如下:
/*为BlobTest.java而创建的 */
create table blob_test (
id int primary key,
clob_content longblob); /*大对象,用来存取mp3音频的二进制信息*/


测试源代码如下:
BlobTest.java
使用了mysql的数据库

package ch14.binaryFile;
import java.sql.*;
import java.io.*;
public class BlobTest
{
private Connection getCon() throws Exception{
String className = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/Gjun";
String userName = "root";
String password = "850812";
/*
String className = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:it";
String userName = "tarena";
String password = "tarena";
*/
Connection con = null;
Class.forName(className);
con = DriverManager.getConnection(url,userName,password);
return con;
}
public void writeBlob(String fileName){
try{
FileInputStream fis = new FileInputStream(fileName);

Connection con = getCon();
String sql = "insert into blob_test values(?,?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,3);
ps.setBinaryStream(2,fis,fis.available());
int i = ps.executeUpdate();
System.out.println(i+" rows update success");
ps.close();
con.close();

}catch(Exception ex){
ex.printStackTrace();
}
}
public void readBlob(String fileName){
try{
Connection con = getCon();
String sql = "select blob_content from blob_test where id=3";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
rs.next();
Blob blob = rs.getBlob("blob_content");
int size = (int)blob.length();
byte[] buf = new byte[size];
InputStream fis = blob.getBinaryStream();
FileOutputStream fos = new FileOutputStream(fileName);
int byteTemp = 0;
while((byteTemp = fis.read(buf))!=-1){
fos.write(buf,0,byteTemp);
}
fos.flush();
System.out.println("Blob读取完毕");
fos.close();
fis.close();
rs.close();
ps.close();
con.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
BlobTest test = new BlobTest();
test.writeBlob("D:\\test.mp3");
//test.readBlob("c:\\aa.mp3");
}
}

错误显示:
com.mysql.jdbc.PacketTooBigException:Packet for query is too large (3846199 > 1048576). You 

can change this value on the server by setting the max_allowed_packet' variable.



------解决方案--------------------
与代码无关吧,应该与设置有关, 不过我不知道哪里设置