日期:2014-05-20 浏览次数:20921 次
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BlobDemo {
public static final String URL = "jdbc:mysql://localhost:3306/demo";
public static void main(String args[]) throws Exception{
Connection conn = null ;
PreparedStatement ps = null ;
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(URL,"root","admin");
ps = conn.prepareStatement("INSERT INTO demo(name,photo) VAKUES (?,?)");
File f = new File("e:" +File.separator+"123.jpg");
InputStream input = null ;
input = new FileInputStream(f);
ps.setString(1, "codefu");
ps.setBinaryStream(2, input,(int)(f.length()));
ps.executeUpdate();
ps.close();
conn.close();
}
}