当我尝试从Sqlite数据库中检索图像(Blob)文件时,它会给我这些错误。我使用过很多jar,比如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");
}
}
答案 0 :(得分:2)
试试这个:
byte[] byteArr = rs.getBytes("image");
答案 1 :(得分:1)
请改用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);
答案 2 :(得分:0)
错误:强>
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);
<强> TRY:强>
Byte blob=rs.getByte(2);