实验题4 给student表增加一pic字段,使之能存放图片,并通过程序存入图片和读取图片。
【实验过程】本题主要要求将图片存入到数据库中,这里我们用到了前面学到的文件的诸多操作,我使用了将图片转化为二进制文件的形式,然后进行相应的插入操作和读取操作。
【核心代码】
package Program04;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.imageio.ImageIO;
import Program01.ConnectionDemo;
public class ImageStore {
static String sql;
public static Connection conn;
public ImageStore() {
ConnectionDemo connection = new ConnectionDemo();
try {
conn = connection.connected();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void InsertRow() {
sql = "alter table student add S_Pic image";
try {
java.sql.Statement statement = conn.createStatement();
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void InsertImage(String path,String No) {
try {
PreparedStatement prpdStatement;
sql = "update student set S_Pic = ? where Sno = ?";
prpdStatement = conn.prepareStatement(sql);
prpdStatement.setString(2, No);
File file = new File(path);
InputStream in = new BufferedInputStream(new FileInputStream(file));
prpdStatement.setBinaryStream(1, in, (int)file.length());
// 4.执行语句
int i = prpdStatement.executeUpdate();
in.close();
System.out.println("i=" + i);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void getImage(String No,String path) {
try {
try {
sql = "select S_Pic from student where Sno = ?";
java.sql.PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, No);
ResultSet rs = statement.executeQuery();
rs.next();//注意这儿的顺序
InputStream in = rs.getBinaryStream("S_Pic");
File file = new File(path);
OutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
byte[] buff = new byte[1024];
for (int i = 0; (i = in.read(buff)) > 0;) {
out.write(buff, 0, i);
}
out.flush();
out.close();
in.close();
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ImageStore imagestore = new ImageStore();
// imagestore.InsertRow();
String Oldpath = "img01.jpg";
String Newpath = "img02.jpg";
String No = "2009012983";
ImageStore.InsertImage(Oldpath,No);
ImageStore.getImage(No,Newpath);
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
【运行效果】
图片存入数据库之前:
插入数据库之后: