日期:2014-05-20 浏览次数:20888 次
package com.wow.lob;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Test;
import com.wow.util.JDBCUtil;
/*
这个是创建的表
create table test2
(
id int primary key auto_increment,
image longblob
);
*/
public class Demo2 {
@Test
public static void insert() throws Exception{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = JDBCUtil.getConection();
String sql = "insert into test2(image) values(?)";
ps = conn.prepareStatement(sql);
URL url = Demo2.class.getClassLoader().getResource("1.jpg");
File file = new File(url.getPath());
ps.setBinaryStream(1, new FileInputStream(file), file.length());
int num = ps.executeUpdate();
if(num>0){
System.out.println("插入成功");
}
}finally{
JDBCUtil.myClose(rs, ps, conn);
}
}
}
int num = ps.executeUpdate();就出异常,说我sql语句有问题。二进制数据这么插入没错吧?求大神!!!
public class Test {
public static void main(String[] args) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConection();
String sql = "insert into pic values(?,?,?)";
ps = conn.prepareStatement(sql);
File file = new File("c:/a.jpg");
FileInputStream fis = new FileInputStream("c:/a.jpg");
ps.setInt(1, 1);
ps.setString(2, "a.jpg");
ps.setBinaryStream(3, fis, (int)file.length());
int num = ps.executeUpdate();
if (num > 0) {
System.out.println("插入成功");
}else{
System.err.println("插入失败");
}
}catch (Exception e) {
e.printStackTrace();
} finally {[code=Java]
int num = ps.executeUpdate();