oracle 用Java将blob上传到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3513888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading a blob to database in Java
提问by Smruti R Tripathy
I have written a java program where I can insert images to my Oracle database blob. It's working perfectly on other systems, but not working in my system when I want to insert an image of size greater than 2kb. But from my system I am able to retrieve images of any size. The same application is working well in other system but behaving abnormally, and some times can't read from socket exception when I try to insert a image bigger than 2 kb. Please help.
我编写了一个 java 程序,我可以在其中将图像插入到我的 Oracle 数据库 blob 中。它在其他系统上运行良好,但当我想插入大于 2kb 的图像时,它在我的系统中无法正常工作。但是从我的系统中,我可以检索任何大小的图像。同一个应用程序在其他系统中运行良好但行为异常,有时当我尝试插入大于 2 kb 的图像时无法从套接字异常中读取。请帮忙。
I am using typ4 driver.
我正在使用 typ4 驱动程序。
Following is my table structure::
以下是我的表结构::
Name Null? Type
----------------------------------------------------- -------- ----------------------
NAME NOT NULL VARCHAR2(20)
DESCRIPTION NOT NULL VARCHAR2(20)
IMAGE NOT NULL BLOB
The following is my Java program to insert the image. Again, it works on other systems in same network, which are connected to the same Oracle server. But from system showing problem when I try to upload an image having size more than 2kb.
下面是我插入图片的Java程序。同样,它适用于连接到同一 Oracle 服务器的同一网络中的其他系统。但是当我尝试上传大小超过 2kb 的图像时,系统显示问题。
package com.smruti.image;
import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.*;
public class InsertImage {
private static String url = "jdbc:oracle:thin:@server3:1521:server3";
private static String username = "system";
private static String password = "manager";
public static void main(String[] args) throws Exception {
Connection conn = null;
InputStream fis = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
String sql = "INSERT INTO pictures VALUES (?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "scare.jpg");
stmt.setString(2, "scare image");
File image = new File("C:\images.jpeg");
fis = new FileInputStream(image);
int ilen=(int) image.length();
System.out.println(ilen);
System.out.println(fis);
stmt.setBinaryStream(3, fis, ilen);
stmt.execute();
System.out.println("this is upto b4 commit");
conn.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
}
回答by Andreas Dolk
This external discussion threadindicates that it may be a jdbc driver issue. They recommend updating the oracle jdbc thin drivers.
这个外部讨论线程表明它可能是 jdbc 驱动程序问题。他们建议更新 oracle jdbc 瘦驱动程序。
回答by vickyreddi
i too faced the same issue i could not upload more than 2kb file ... upadting the oracle jdbc driver to ojdbc5.jar resolved this issue.
我也遇到了同样的问题,我无法上传超过 2kb 的文件……将 oracle jdbc 驱动程序更新到 ojdbc5.jar 解决了这个问题。