当然应当先建立一个数据库:还是上次的那个
create table picture_db(
file_name varchar(255) not null,
content longblob,
primary key (file_name));
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class UpImage {
protected static Connection dbConnection;
protected static String driverName = "com.mysql.jdbc.Driver";
protected static String dbURL = "jdbc:mysql://localhost:3306/sample_db";
protected static String userID = "root";
protected static String passwd = "123";
protected static String str = "insert into picture_table values(?,?)";
public static void main(String[] args){
try{
Class.forName(driverName);
dbConnection = DriverManager.getConnection(dbURL,userID,passwd);
PreparedStatement pstmt = dbConnection.prepareStatement(str);
File f = new File("E:\\pictures-collect\\MUST logo.jpg");
FileInputStream fis = new FileInputStream(f);
pstmt.setString(1, f.getName());
pstmt.setBinaryStream(2, fis, (int)f.length());
if(pstmt.executeUpdate()!=1){
System.out.print("Photo insertion error\n");
}
pstmt.close();
fis.close();
dbConnection.close();
}catch (Exception e){
e.printS