Java 如何将BLOB数组传递给存储oracle过程?
我允许用户上传多个文件到我的数据库。这些文件内容必须作为BLOB存储在我的oracle数据库中 如何编写oracle过程来实现这一点?(我对Oracle存储过程有一点了解) 完成后,如何使用jdbc的CallableStatement在java中使用存储过程Java 如何将BLOB数组传递给存储oracle过程?,java,oracle,stored-procedures,jdbc,Java,Oracle,Stored Procedures,Jdbc,我允许用户上传多个文件到我的数据库。这些文件内容必须作为BLOB存储在我的oracle数据库中 如何编写oracle过程来实现这一点?(我对Oracle存储过程有一点了解) 完成后,如何使用jdbc的CallableStatement在java中使用存储过程 请提供帮助。首先,您必须创建包含BLOB表的类型: CREATE OR REPLACE TYPE tab_blobs AS TABLE OF BLOB; 在Java中,您必须依赖Oracle sql提供的结构类型。 您将创建一个结构,其中
请提供帮助。首先,您必须创建包含BLOB表的类型:
CREATE OR REPLACE TYPE tab_blobs AS TABLE OF BLOB;
在Java中,您必须依赖Oracle sql提供的结构类型。
您将创建一个结构,其中包含要存储到DB中的BLOB数组
代码如下所示:
import java.sql.Connection;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleDriver;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
public class ArrayDemo
{
public static void passArray()
throws SQLException
{
Connection conn = new OracleDriver().defaultConnection();
byte[] fileInByteArray = "value".getBytes();
StructDescriptor itemDescriptor = StructDescriptor.createDescriptor("BLOB", conn);
Object[] itemAtributes = new Object[] {};
STRUCT itemObject1 = new STRUCT(itemDescriptor, conn, itemAtributes);
itemAtributes = new Object[] {};
STRUCT itemObject2 = new STRUCT(itemDescriptor, conn, itemAtributes);
STRUCT[] idsArray = { itemObject1, itemObject2 };
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("IDS_TABLE", conn);
ARRAY array_to_pass = new ARRAY(descriptor, conn, idsArray);
OraclePreparedStatement ps = (OraclePreparedStatement) conn.prepareStatement("begin getInfo(:x); end;");
ps.setARRAY(1, array_to_pass);
ps.execute();
}
}
但是,为什么不通过迭代文件,一个接一个地插入它们来简化处理:
public static void insererBlob(String name, String path) {
File file = new File(path);
try{
//link to DB
Connection connection = DriverManager.getConnection("url","user","password");
//link to file
FileInputStream stream = new FileInputStream(file);
//prepare the SQL instruction
String sql = "INSERT INTO file_table VALUES (?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
//blob insertion
statement.setString(1, name);
statement.setBinaryStream(2, stream, (int)file.length());
statement.executeUpdate();
}catch(Exception e){
//ERROR SQL, IO, etc .
}finally {
//close connection ?
}
}
这里有另一个尝试来帮助你。 您可以在此处从oracle找到更多信息: 另外,我还举了一个例子(当时我不得不这么做的时候,这个网站非常有帮助):
下面是我的代码,希望你能得到答案:
try {Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, db_user, password);
System.out.println("Connected to database");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date now = new java.sql.Date(simpleDateFormat.parse("12/02/2001").getTime());
String command2 = "{call USER1(?,?,?,?)}";
String path;
File[] roots = File.listRoots();
path=roots[0].getPath()+"dfg.jpg";
System.out.println("path: " + path);
//shows drives in you computer
for(int i = 0; i < roots.length ; i++){
System.out.println("drive: " + roots[i].getPath());
}
CallableStatement insertStatment = con.prepareCall(command2);
insertStatment.setInt(1, 18);
insertStatment.setString(2, "ankssaait");
insertStatment.setDate(3, now);
File file = new File(path);
//link to file
FileInputStream stream = new FileInputStream(file);
insertStatment.setBinaryStream(4, stream,(int)file.length());;
System.out.println("onExecute: "+ insertStatment.executeUpdate());
insertStatment.close();
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
什么是“值”;它应该是文件,您可以将其转换为字节[]。1。我的程序是:
try {Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, db_user, password);
System.out.println("Connected to database");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date now = new java.sql.Date(simpleDateFormat.parse("12/02/2001").getTime());
String command2 = "{call USER1(?,?,?,?)}";
String path;
File[] roots = File.listRoots();
path=roots[0].getPath()+"dfg.jpg";
System.out.println("path: " + path);
//shows drives in you computer
for(int i = 0; i < roots.length ; i++){
System.out.println("drive: " + roots[i].getPath());
}
CallableStatement insertStatment = con.prepareCall(command2);
insertStatment.setInt(1, 18);
insertStatment.setString(2, "ankssaait");
insertStatment.setDate(3, now);
File file = new File(path);
//link to file
FileInputStream stream = new FileInputStream(file);
insertStatment.setBinaryStream(4, stream,(int)file.length());;
System.out.println("onExecute: "+ insertStatment.executeUpdate());
insertStatment.close();
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
CREATE OR REPLACE PROCEDURE USER1 (U_ID IN o_user.id%TYPE, U_NAME in o_user.name%TYPE, u_DOB in o_user.dob%TYPE, u_image in o_user.image%TYPE) AS BEGIN insert into o_user(id,name,dob,image) values(u_id,u_name,u_dob,u_image); END USER1;