错误 java.sql.SQLException:从数据库中检索 Blob(图像)时 SQLite JDBC 驱动程序未实现

Posted

技术标签:

【中文标题】错误 java.sql.SQLException:从数据库中检索 Blob(图像)时 SQLite JDBC 驱动程序未实现【英文标题】:Error java.sql.SQLException: not implemented by SQLite JDBC driver when Retrieving Blob (Image) from Database 【发布时间】:2015-10-23 22:13:03 【问题描述】:

当我尝试从 Sqlite 数据库中检索图像(Blob)文件时,它给了我这些错误。我用过很多 jars,例如 rs2xml、sqlitejdbc-v056、sqlite-jdbc-3.8.11 甚至它的所有旧版本。 这是堆栈跟踪。

java.sql.SQLException: not implemented by SQLite JDBC driver
at org.sqlite.Unused.unused(Unused.java:31)
at org.sqlite.Unused.getBlob(Unused.java:86)
at Show$2.actionPerformed(Show.java:75)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$300(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是我的代码

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Show extends JFrame 

    private JPanel contentPane;
    private JTextField id;
    BufferedImage bufImg = null;
    JLabel img = null;
    InputStream in = null;
    ImageIcon imgs = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) 
        EventQueue.invokeLater(new Runnable() 
            public void run() 
                try 
                    Show frame = new Show();
                    frame.setVisible(true);
                 catch (Exception e) 
                    e.printStackTrace();
                
            
        );
    

    /**
     * Create the frame.
     */
    Connection con = null;

    public Show() 
        con = dB.Connect();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 588, 432);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        id = new JTextField();
        id.setBounds(158, 23, 86, 20);
        contentPane.add(id);
        id.setColumns(10);

        JButton show = new JButton("New button");
        show.addActionListener(new ActionListener() 
            public void actionPerformed(ActionEvent e) 
                try 
                    String q = "select image from showme where id='" + id.getText() + "'";
                    PreparedStatement ps = con.prepareStatement(q);
                    ResultSet rs = ps.executeQuery();
                    try 
                        while (rs.next()) 
                            // problems might be occurring in here
                            Blob blob = rs.getBlob(2);
                            ImageIcon icon = new ImageIcon(blob.getBytes(2, (int) blob.length()));
                            img.setIcon(icon);
                        
                     catch (SQLException x) 
                        x.printStackTrace();
                    

                    JOptionPane.showMessageDialog(null, "Done");

                    // in.close();
                    rs.close();
                    ps.close();

                 catch (Exception c) 
                    c.printStackTrace();
                
            
        );
        show.setBounds(302, 22, 89, 23);
        contentPane.add(show);

        img = new JLabel("");
        img.setBounds(151, 99, 325, 284);
        contentPane.add(img);
    

编辑: 我尝试使用 Bytearray 。虽然它没有向我显示任何错误,但它没有给我任何输出。 !这是修改的部分

   try 
        String q = "select image from showme where id='"+id.getText()+"'";
        PreparedStatement ps = con.prepareStatement(q);
        ResultSet rs = ps.executeQuery();       
         if(rs.next()) 
            byte[] imgss = rs.getBytes("image");
            //Resize The ImageIcon
            ImageIcon mage = new ImageIcon(imgss);
            Image im = mage.getImage();
            Image myImg = im.getScaledInstance(imgss.length, imgss.length,imgss.length);
            ImageIcon newImage = new ImageIcon(myImg);
            img.setIcon(newImage);
            JOptionPane.showMessageDialog(null, "Done");
        
    

【问题讨论】:

不使用 getBlob 方法如何检索 Blob 文件? 您可以将图像作为字节数组结果集 .getBytes("image"); 这个链接可能对***.com/questions/6090170/…有帮助 【参考方案1】:

错误:

java.sql.SQLException: not implemented by SQLite JDBC driver
at org.sqlite.Unused.unused(Unused.java:31)
at org.sqlite.Unused.getBlob(Unused.java:86)

在:

//problems might be occurring in here 
Blob blob=rs.getBlob(2);

试试:

Byte blob=rs.getByte(2);

【讨论】:

【参考方案2】:

请改用getBinaryStream()。类似于以下内容:

ResultSet rs=ps.executeQuery();     
InputStream in = rs.getBinaryStream("image");
ByteArrayOutpuStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = in.read(buffer);
while (bytesRead > -1)

  out.write(buffer, bytesRead)
  bytesRead = in.read(buffer);

byte[] picture = out.toByteArray();
in.close();

您当然需要在上面添加异常处理。

如果您使用的是 Apache Commons IO 库,您可以使用 IOUtils.toByteArray() 来简化它

ResultSet rs=ps.executeQuery();     
InputStream in = rs.getBinaryStream("image");
byte[] picture = IOUtils.toByteArray(in);

【讨论】:

问题是它没有向我显示 JLabel 中的图片。还有其他问题吗? @CodeHead: new ByteArrayOutputStream(); 非常好。您是否验证了您的字节数组确实包含正确的数据?以便我们知道这是否是检索显示图像的问题 我已验证我的数据库中的数据没有损坏且正确。 我不是指数据库中的数据in,我是指检索数据后字节数组的内容(例如使用调试器) 抱歉问了一个蹩脚的问题。我很nOob。你能告诉我如何验证吗?【参考方案3】:

试试这个:

byte[] byteArr = rs.getBytes("image"); 

【讨论】:

我用过 bytearray 。 .虽然它没有给我任何错误。但它没有将图像设置为 Jlabel !不知道为什么!

以上是关于错误 java.sql.SQLException:从数据库中检索 Blob(图像)时 SQLite JDBC 驱动程序未实现的主要内容,如果未能解决你的问题,请参考以下文章

java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 发生错误

错误 java.sql.SQLException: 未找到列 'id'

java.sql.SQLException:指定的 Oracle URL 无效错误

如何解决错误“java.sql.SQLException:结果集开始之前” [重复]

获取异常 java.sql.SQLException:套接字创建错误

错误:java.sql.SQLException:用户'root@localhost'@'localhost'的访问被拒绝(使用密码:是)