jdbcTemplate更新mysql的blob型別欄位
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.support.SqlLobValue;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/application*.xml"})
public class TestInsertDocContent {
@Autowired
private JdbcTemplate jdbcTemplate;
@SuppressWarnings("unchecked")
@Test
public void t(){
/*String sql="update t set ";*/
String s1="select * from t t where t.id=3";
String s2="update db.t set t.name= where t.id=3";
/*jdbcTemplate.query(s1, new RowMapper(){
@Override
public Object mapRow(ResultSet paramResultSet, int paramInt)
throws SQLException {
// TODO Auto-generated method stub
System.out.println(paramResultSet);
return null;
}
});*/
try{
final File image = new File("E:\\1.docx");
final InputStream imageIs = new FileInputStream(image);
LobHandler lobHandler = new DefaultLobHandler();
jdbcTemplate.update(
/* "INSERT INTO trn_imgs (img_title, img_data) VALUES (?, ?)",*/
"update db.t t set t.name=?,t.blob=? where t.id=3",
new Object[] {
"Puppy",
new SqlLobValue(imageIs, (int)image.length(), lobHandler),
},
new int[] {Types.VARCHAR, Types.BLOB});
}catch(Exception e){
e.printStackTrace();
}
/*jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);*/
/* MapSqlParameterSource parameters = new MapSqlParameterSource();*/
/*parameters.addValue("id", 3);*/
//parameters.addValue("blob_field", new SqlLobValue(new ByteArrayInputStream(bytes), bytes.length, new DefaultLobHandler()), OracleTypes.BLOB);
/*parameters.addValue("blob_field", new SqlLobValue(new ByteArrayInputStream(bytes), bytes.length, new DefaultLobHandler()), BLOB);
System.out.println(i);*/
System.out.println("over");
}
}