package a1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) {
FileInputStream fis=null;
//用MYSQL存一个BLOB类型存不进去,换了个longblob就好了,为什么?
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:MySQL://127.0.0.1:3306/test";
String sql="insert into student1 values(?,?,?)";
Connection con=DriverManager.getConnection(url, "root", "root");
PreparedStatement smt=con.prepareStatement(sql);
smt.setString(1, "S01");
smt.setString(2, "ADMIN");
File file=new File("d://a.jpg");
fis=new FileInputStream(file);
smt.setBinaryStream(3, fis, file.length());
int i=smt.executeUpdate();
System.out.println(i);
smt.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
|
|