写个java下程序,用DBUtils操作数据库。代码如下:
@Test
public void blob()
{
try {
Connection connection=JDBCTools.getConnection();
QueryRunner queryRunner =new QueryRunner();
File file=new File("C:/1.png");
byte[] buf=new byte[(int)file.length()];
InputStream in=new FileInputStream(file);
in.read(buf);
SerialBlob blob=new SerialBlob(buf);
String sql="INSERT INTO table1 VALUES(?)";
queryRunner.update(connection, sql,blob);
System.out.println(connection);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}为啥插入不了??
@Test
public void blob()
{
try {
Connection connection=JDBCTools.getConnection();
QueryRunner queryRunner =new QueryRunner();
File file=new File("C:/1.png");
byte[] buf=new byte[(int)file.length()];
InputStream in=new FileInputStream(file);
in.read(buf);
SerialBlob blob=new SerialBlob(buf);
String sql="INSERT INTO table1 VALUES(?)";
queryRunner.update(connection, sql,blob);
System.out.println(connection);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}为啥插入不了??
是否提交了?
public void blob()
{
try {
//写入Blob字段之前 需要保存好其他字段
Connection connection=JDBCTools.getConnection();
String sql = "确定update table set Blob字段 where 确定需要写入Blob字段的条件";
File file=new File("C:/1.png");
byte[] buf=new byte[(int)file.length()];
PrepareStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
rs.updateBytes(columnIndex,buf);
}
System.out.println(connection);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}