java向MYSQL中插入图片时为列属性为LONGBLOB,抛出异常?大侠们帮帮忙。谢谢了!
代码如下:
public void blobInsert(String infile,int id) throws Exception
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(infile);
String sql = "insert into t_photo values(?,?,?)";
PreparedStatement pstmt = ConnPool.request().getPreStmt(sql);
System.out.println(pstmt.toString());
pstmt.setInt(1, id);
pstmt.setString(2,infile); //把传过来的第二个参数设为文件名
pstmt.setBinaryStream(3,fis,fis.available()); //第三个参数为文件的内容
System.out.println(fis.available());
pstmt.executeUpdate();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
fis.close();
}
}
代码如下:
public void blobInsert(String infile,int id) throws Exception
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(infile);
String sql = "insert into t_photo values(?,?,?)";
PreparedStatement pstmt = ConnPool.request().getPreStmt(sql);
System.out.println(pstmt.toString());
pstmt.setInt(1, id);
pstmt.setString(2,infile); //把传过来的第二个参数设为文件名
pstmt.setBinaryStream(3,fis,fis.available()); //第三个参数为文件的内容
System.out.println(fis.available());
pstmt.executeUpdate();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
fis.close();
}
}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2022)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1940)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1925)
at com.dbpractise.beans.Photo.blobInsert(Photo.java:24)
at com.dbpractise.beans.Photo.main(Photo.java:111)