Java 需要帮助从sqlite数据库创建图像吗
我试图在JavaSwing应用程序中从SQLite数据库创建映像。但它显示了我的错误。我使用JDK1.6、Java6、SQLITE3、sqlitejdbc-v056.jar连接SQLite和Ubuntu11.04。我正在从中获取sqlite连接器。以下是我的代码:Java 需要帮助从sqlite数据库创建图像吗,java,swing,sqlite,Java,Swing,Sqlite,我试图在JavaSwing应用程序中从SQLite数据库创建映像。但它显示了我的错误。我使用JDK1.6、Java6、SQLITE3、sqlitejdbc-v056.jar连接SQLite和Ubuntu11.04。我正在从中获取sqlite连接器。以下是我的代码: pos.res = pos.statement.executeQuery("select * from customer where card_id = "+card_id+";"); while(pos.r
pos.res = pos.statement.executeQuery("select * from customer where card_id = "+card_id+";");
while(pos.res.next()){
if(pos.defaultParams != null){
pos.defaultParams.remove("pricelist_id");
}else{
pos.defaultParams = new HashMap<String, Object>();
}
pos.defaultParams.put("pricelist_id", pos.res.getString("pricelist"));
pos.jTextPane6.setText(pos.res.getString("name").toString());
pos.customderId = Integer.parseInt(pos.res.getString("customer_id"));
pos.jTextPane7.setText(pos.res.getString("department").toString());
byte[] byt = pos.res.getString("photo").toString().getBytes();
InputStream in = new ByteArrayInputStream(byt);
BufferedImage image = ImageIO.read(in);
ImageIO.write(image, "jpg",new File("/home/foo/Desktop/jj.jpg"));
Graphics2D g = (Graphics2D)pos.jPanel4.getGraphics();
g.drawImage(resize(image, 111, 100), null, 0, 0);
}
private static BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage resizedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
请帮助我?您的问题是
ImageIO.read()
失败。这可能是由于以下代码:
pos.res.getString("photo").toString().getBytes();
切勿将类型字符串
用于二进制数据,也切勿使用getBytes()
-这取决于您的平台默认编码。这意味着:它将获取字符串中的Unicode字符,并将它们转换为计算机认为合适的任何字符。这会破坏你的二进制数据
正确的解决方案是使用BLOB列。请参阅以下答案,了解如何将blob写入sqlite:
阅读的代码是类似的
如果这不是一个选项,那么使用编码ISO-8859-1
,如果字符串中没有Unicode字符(任何代码点>255的字符)
,则该编码是1:1映射
不要忘记编写一些单元测试来确保代码正常工作。您的问题是
ImageIO.read()
失败。这可能是由于以下代码:
pos.res.getString("photo").toString().getBytes();
切勿将类型字符串
用于二进制数据,也切勿使用getBytes()
-这取决于您的平台默认编码。这意味着:它将获取字符串中的Unicode字符,并将它们转换为计算机认为合适的任何字符。这会破坏你的二进制数据
正确的解决方案是使用BLOB列。请参阅以下答案,了解如何将blob写入sqlite:
阅读的代码是类似的
如果这不是一个选项,那么使用编码ISO-8859-1
,如果字符串中没有Unicode字符(任何代码点>255的字符)
,则该编码是1:1映射
不要忘记编写一些单元测试以确保代码正常工作。使用此代码获取字节:
Blob blob = pos.res.getBlob("photo");
bytes[] b = blob.getBytes(1, (int) blob.length());
使用以下代码获取字节:
Blob blob = pos.res.getBlob("photo");
bytes[] b = blob.getBytes(1, (int) blob.length());
哇。非常感谢你。它解决了我的问题。你救了我一天。非常感谢你。它解决了我的问题。你救了我一天。嗨,伙计。当我尝试编写您的代码时,我得到了“java.sql.SQLException:未由SQLite JDBC驱动程序实现”错误。知道吗?嗨,伙计。当我尝试编写您的代码时,我得到了“java.sql.SQLException:未由SQLite JDBC驱动程序实现”错误。有什么想法吗?