java JDBC实验题【实验题4 图片的…

实验题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();
  }
 }

}

【运行效果】

图片存入数据库之前:
java <wbr>JDBC实验题【实验题4 <wbr>图片的存入和取出操作 <wbr>数据库】

java <wbr>JDBC实验题【实验题4 <wbr>图片的存入和取出操作 <wbr>数据库】

插入数据库之后:

java <wbr>JDBC实验题【实验题4 <wbr>图片的存入和取出操作 <wbr>数据库】

java <wbr>JDBC实验题【实验题4 <wbr>图片的存入和取出操作 <wbr>数据库】



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值