会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132359个问题
JAVA 全系列/第三阶段:数据库编程/JDBC技术 1666楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 1667楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1668楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 1669楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 1677楼

哪里错了

package com.itbaizhan;

import java.io.*;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* Blob类型操作测试类
*/
public class BlobTest {
   /**
    *movie表中插入数据
    */
   public void insertMoive(String moviename, InputStream is){
       Connection connection = null;
       PreparedStatement ps = null;
       try{
           //获取链接
           connection = JdbcUtils.getConnection();
           //获取PrepareStatement对象
           ps = connection.prepareStatement("insert into movie values(default,?,?)");
           //绑定参数
           ps.setString(1,moviename);
           ps.setBlob(2,is);
           ps.executeUpdate();
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           JdbcUtils.closeResource(ps,connection);
       }
   }
   /**
    * 根据影片ID查询影片信息
    * @param movieid
    */
   public void selectMovieBlob(int movieid){
       Connection conn =null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       try{
           //获取连接
           conn =JdbcUtils.getConnection();
           //创建PreparedStatement对象
           ps = conn.prepareStatement("select * from movie where movieid = ?");
           //绑定参数
           ps.setInt(1,movieid);
           //执行sql
           rs = ps.executeQuery();
           while(rs.next()){
               int id = rs.getInt("movieid");
               String name = rs.getString("moviename");
               System.out.println(id+" "+name);
               //获取blob类型的数据
               Blob blob = rs.getBlob("poster");
               //获取能够从Blob类型的列中读取数据的IO
               InputStream is = blob.getBinaryStream();
               //创建文件输出字节流对象
               OutputStream os = new FileOutputStream(id+"_"+name+".jpg");
               //操作流完成文件的输出处理
               byte[] buff = new byte[1024];
               int len;
               while((len = is.read(buff)) != -1){
                   os.write(buff,0,len);
               }
               os.flush();
               is.close();
               os.close();
           }
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           JdbcUtils.closeResource(rs,ps,conn);
       }
   }


   public static void main(String[] args) throws FileNotFoundException {
       BlobTest b = new BlobTest();
//        InputStream is = new FileInputStream(new File("D:/截图/数据库/MySQLDML操作/MySql Blob类型.jpg"));
//        b.insertMoive("Blob类型",is);
       b.selectMovieBlob(1);
   }
}


java.io.FileNotFoundException: 1Blob??.jpg (文件名、目录名或卷标语法不正确。)

at java.base/java.io.FileOutputStream.open0(Native Method)

at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)

at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)

at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)

at com.itbaizhan.BlobTest.selectMovieBlob(BlobTest.java:60)

at com.itbaizhan.BlobTest.main(BlobTest.java:83)


Process finished with exit code 0


JAVA 全系列/第三阶段:数据库编程/JDBC技术 1678楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 1680楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637