CodeHead
CodeHead

Reputation: 197

Error java.sql.SQLException: not implemented by SQLite JDBC driver when Retrieving Blob (Image) from Database

When I try to retrieve an image(Blob) file from Sqlite database it gives me these error . I have used many jars , such as rs2xml , sqlitejdbc-v056 , sqlite-jdbc-3.8.11 and even all the old versions of it . here is the stacktrace .

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)

Here is my code

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);
    }
}

Edit: I Tried Using Bytearray . Though its not showing me any error but it is not giving me any out put . ! here is the modified portion

   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");
        }
    }

Upvotes: 5

Views: 4262

Answers (3)

Yinlong Liu
Yinlong Liu

Reputation: 69

ERROR:

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:

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

TRY:

Byte blob=rs.getByte(2);

Upvotes: 0

user330315
user330315

Reputation:

Use getBinaryStream() instead. Something like the following:

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();

You will of course need to add exception handling to the above.

If you are using the Apache Commons IO library you can simplify that using IOUtils.toByteArray()

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

Upvotes: 0

Cody
Cody

Reputation: 156

try this:

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

Upvotes: 4

Related Questions