Java 原因是什么;不是由SQLite JDBC驱动程序实现的”;每次我运行;InputStream输入=rs.getBinaryStream(“文件名”)&引用;线路?

Java 原因是什么;不是由SQLite JDBC驱动程序实现的”;每次我运行;InputStream输入=rs.getBinaryStream(“文件名”)&引用;线路?,java,sqlite,blob,Java,Sqlite,Blob,我试图使用sqlite数据库中的查询blob数据,下面是我的完整代码 package team2.entity; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; im

我试图使用sqlite数据库中的查询blob数据,下面是我的完整代码

package team2.entity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import team2.controller.sqliteCon;

public class LectureFiles {
    private String code;
    private static String fileName;

public LectureFiles(String code, String fileName){
    this.code = code;
    this.fileName = fileName;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

private static LectureFiles convertToLectFiles(ResultSet rs) throws SQLException{
    LectureFiles LF;
    String code = rs.getString("Code");
    String fileName = rs.getString("FileName");
    LF = new LectureFiles(code, fileName);
    return LF;
}

/**
 * Read the file and returns the byte array
 * @param file
 * @return the bytes of the file
 */

private static byte[] readFile(String file){
    ByteArrayOutputStream bos = null;
    try {
        File f = new File(file);
        FileInputStream fis = new FileInputStream(f);
        byte[] buffer = new byte[1024];
        bos = new ByteArrayOutputStream();
        for(int len = 0; (fis.read(buffer))!=-1;){
            bos.write(buffer, 0, len);
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bos != null ? bos.toByteArray(): null;
}

public static boolean updateLectFiles(LectureFiles LF){
    // declare local variables
    boolean success = false;
    sqliteCon con = new sqliteCon();
    PreparedStatement pstmt;
    String dbQuery;
    // step 1 - establish connection to database
    con.getConnection();
    // step 2 - declare the SQL statement
    dbQuery = "INSERT INTO LectureFiles (code, filename) VALUES(?, ?)";
    // step 3 - to insert record using executeUpdate method
    pstmt = con.getPreparedStatement(dbQuery);
    try {
        pstmt.setString(1, LF.getCode());
        pstmt.setBytes(2, readFile(fileName));
        if(pstmt.executeUpdate()==1){
            success = true;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // step 4 - close connection
    con.terminate();
    return success;
}

public static void retrieveLectFilesByCode(LectureFiles LF){
    // declare local variables
    //ArrayList<LectureFiles> LFlist = new ArrayList<LectureFiles>();
    sqliteCon con = new sqliteCon();
    PreparedStatement pstmt = null;
    String dbQuery;
    ResultSet rs = null;
    FileOutputStream fos = null;

    // step 1 - connect to database
    con.getConnection();
    // step 2 - declare the SQL statement
    dbQuery = "SELECT * FROM LectureFiles WHERE Code = ?";
    pstmt = con.getPreparedStatement(dbQuery);

    //step 3 - execute query and write binary stream into file
    try {
        pstmt.setString(1, LF.getCode());
        rs = pstmt.executeQuery();
        File file = new File(fileName);
        fos = new FileOutputStream(file);

        System.out.println("Writing BLOB to file " + file.getAbsolutePath());
        while(rs.next()){
            InputStream input = rs.getBinaryStream("FileName");// this is where the error is
            byte[] buffer = new byte[1024];
            while(input.read(buffer) > 0){
                fos.write(buffer);
            }
        }
    } catch (SQLException | IOException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (con != null) {
                con.terminate();
            }
            if (fos != null) {
                fos.close();
            }

        } catch (SQLException | IOException e) {
            System.out.println(e.getMessage());
        }
    }
}



public static void main(String [] args){
    /*LectureFiles LF = new LectureFiles("Code", "C:\\Users\\Thura_98\\Desktop\\OOAD Images\\Lecture128.png");
    updateLectFiles(LF);*/
    LectureFiles LF2 = new LectureFiles("Code", "C:\\Users\\Thura_98\\Desktop\\OOAD Images\\Lecture128_Blob.png");
    retrieveLectFilesByCode(LF2);
}
package team2.entity;
导入java.io.ByteArrayOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.InputStream;
导入java.io.IOException;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.util.ArrayList;
导入team2.controller.sqliteCon;
公营讲座{
私有字符串码;
私有静态字符串文件名;
公共讲师文件(字符串代码、字符串文件名){
this.code=代码;
this.fileName=文件名;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共字符串getFileName(){
返回文件名;
}
public void setFileName(字符串文件名){
this.fileName=文件名;
}
私有静态讲师文件convertToLectFiles(ResultSet rs)引发SQLException{
讲师自我介绍;
字符串代码=rs.getString(“代码”);
字符串文件名=rs.getString(“文件名”);
LF=新讲师文件(代码、文件名);
返回LF;
}
/**
*读取文件并返回字节数组
*@param文件
*@返回文件的字节数
*/
私有静态字节[]读取文件(字符串文件){
ByteArrayOutputStream bos=null;
试一试{
文件f=新文件(文件);
FileInputStream fis=新的FileInputStream(f);
字节[]缓冲区=新字节[1024];
bos=新的ByteArrayOutputStream();
对于(int len=0;(fis.read(buffer))!=-1;){
写入(缓冲区,0,len);
}
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回bos!=null?bos.toByteArray():null;
}
公共静态布尔更新文件(讲师文件LF){
//声明局部变量
布尔成功=假;
sqliteCon=newsqlitecon();
编制报表;
字符串数据库查询;
//步骤1-建立到数据库的连接
con.getConnection();
//步骤2-声明SQL语句
dbQuery=“插入讲师文件(代码、文件名)值(?,)”;
//步骤3-使用executeUpdate方法插入记录
pstmt=con.getPreparedStatement(dbQuery);
试一试{
pstmt.setString(1,LF.getCode());
pstmt.setBytes(2,readFile(文件名));
如果(pstmt.executeUpdate()==1){
成功=真实;
}
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//步骤4-关闭连接
con.terminate();
回归成功;
}
公共静态无效检索electfilesbycode(讲师文件LF){
//声明局部变量
//ArrayList LFlist=新的ArrayList();
sqliteCon=newsqlitecon();
PreparedStatement pstmt=null;
字符串数据库查询;
结果集rs=null;
FileOutputStream=null;
//步骤1-连接到数据库
con.getConnection();
//步骤2-声明SQL语句
dbQuery=“从代码=?”的讲师文件中选择*;
pstmt=con.getPreparedStatement(dbQuery);
//步骤3-执行查询并将二进制流写入文件
试一试{
pstmt.setString(1,LF.getCode());
rs=pstmt.executeQuery();
文件=新文件(文件名);
fos=新文件输出流(文件);
System.out.println(“将BLOB写入文件”+file.getAbsolutePath());
while(rs.next()){
InputStream input=rs.getBinaryStream(“文件名”);//这就是错误所在
字节[]缓冲区=新字节[1024];
while(输入读取(缓冲区)>0){
写入(缓冲区);
}
}
}catch(SQLException | IOException e){
//TODO自动生成的捕捉块
System.out.println(e.getMessage());
}最后{
试一试{
如果(rs!=null){
rs.close();
}
如果(pstmt!=null){
pstmt.close();
}
如果(con!=null){
con.terminate();
}
如果(fos!=null){
fos.close();
}
}catch(SQLException | IOException e){
System.out.println(e.getMessage());
}
}
}
公共静态void main(字符串[]args){
/*讲师文件LF=新讲师文件(“代码”,“C:\\Users\\Thura\u 98\\Desktop\\OOAD Images\\讲师128.png”);
更新文件(LF)*/
讲师文件LF2=新讲师文件(“代码”,“C:\\Users\\Thura\u 98\\Desktop\\OOAD Images\\讲师128\u Blob.png”);
retrieveLectFilesByCode(LF2);
}
}

我能够成功地将文件作为blob保存到数据库中,但无法从sqlite数据库查询blob数据。每次编译器运行InputStream input=rs.getBinaryStream(“文件名”);行,它总是给出“不是由SQLite JDBC驱动程序实现的”。因此,它创建了文件,但没有内容。顺便说一下,我使用了这些源代码来帮助我
任何形式的帮助都将不胜感激,谢谢。

消息“未由SQLite JDBC驱动程序实现”是否足够?您是否尝试过
getBytes()
getBlob()
?也许这些方法已经实现了。@a_horse_和a_no_名称我在updateLectFiles方法中使用了getBytes,那么为什么不在
retrieveLectFilesByCode
中也使用它呢?@a_horse_和a_no_名称我尝试过,但它给出了java.io.FileNotFoundException错误