Home » Developer & Programmer » JDeveloper, Java & XML » how to insert a file into oracle blob object
how to insert a file into oracle blob object [message #90949] Fri, 28 December 2001 02:52 Go to next message
vinoth
Messages: 7
Registered: October 2001
Junior Member
Hi,
can any body help me to insert a file into oracle table containing blob object. The file preferrably msword document.

i tried a program , but it is getting an error
The program i tried is

import java.io.*;
import java.sql.*;

public class InsertFile
{
public static void main(String args[[]])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:oracledsn","scott","tiger");
PreparedStatement pstmt=con.prepareStatement("insert into myform (id,name,resume) values (?,?,?)");
File file=new File(args[[0]]);
InputStream is=new FileInputStream(args[[0]]);
pstmt.setBinaryStream(3,is,(int)file.length());
pstmt.setString(1,"abcde12345");
pstmt.setString(2,"mydoc");
System.out.println("abcde12345"+"mydoc"+args[[0]]);
if(pstmt.execute())
System.out.println("Inserted the record");
else
System.out.println("Inserted the record");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

for msword file i am getting an error
java.sql.SQLException: [[Oracle]][[ODBC]][[Ora]]ORA-24307: invalid length for piece

for other file like txt,xml,jsp
java.sql.SQLException: [[Oracle]][[ODBC]]No data at execution values pending.

Thanks in advance
bye
Vinoth

----------------------------------------------------------------------
Re: how to insert a file into oracle blob object [message #91018 is a reply to message #90949] Fri, 15 February 2002 01:48 Go to previous messageGo to next message
vinoth
Messages: 7
Registered: October 2001
Junior Member
problem is get solved with this code

Basically before inserting a file in blob field of oracle we have to initialize the field by EMPTY_BLOB() and then update the field with the file.Now no problem of inserting the file in blob field.

import java.io.*;
import java.sql.*;

public class InsertWordFile
{
public static void main(String args[[]])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:oracledsn","scott","tiger");
Statement st=con.createStatement();
String initstr="insert into myform values ('"+args[[0]]+"','"+args[[1]]+"',EMPTY_BLOB())";
st.execute(initstr);
st.close();
con.commit();
new BufferedReader(new InputStreamReader(System.in)).read();
PreparedStatement pstmt=con.prepareStatement("update myform set resume = (?) where fileid ='"+args[[1]]+"'");
File file=new File(args[[2]]);
InputStream is=new FileInputStream(args[[2]]);
pstmt.setBinaryStream(1,is,(int)file.length());
System.out.println("Length of the file "+file.length());
System.out.println("Length of the file "+(int)file.length());
if(pstmt.execute())
System.out.println("Inserted the record");
else
System.out.println("Inserted the record");
con.commit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
How to insert a binary file into oracle table through a C program? [message #91102 is a reply to message #90949] Thu, 04 April 2002 23:55 Go to previous messageGo to next message
Akshay Marathe
Messages: 2
Registered: April 2002
Junior Member
Can anyone please tell me the command/solution for the query mentioned. I've tried to read the binary file in a string, which doesn't work for binary file. Hence it is going as null value in oracle table (column type long raw).

Is there any utility/command/query to directly dump the binary file into the column of type long raw? Please let me know as early as possible.
please help [message #91124 is a reply to message #91018] Thu, 18 April 2002 06:38 Go to previous messageGo to next message
smichi
Messages: 4
Registered: April 2002
Junior Member
hello
i have a very big problem

How can i connect to an oracle object database and insert an object into an object table with a java programm . and how can i use the jpublisher class generated of object type .
thanks .
Re: how to insert a file into oracle blob object [message #91223 is a reply to message #90949] Tue, 04 June 2002 10:08 Go to previous messageGo to next message
Madhan
Messages: 4
Registered: June 2002
Junior Member
Probably, u need to call the EmptyBlob() on the blob column before inserting into it. The default insert lets you insert data of size<4K.
Re: how to insert a file into oracle blob object [message #91229 is a reply to message #90949] Wed, 05 June 2002 21:02 Go to previous messageGo to next message
Vinoth P
Messages: 1
Registered: June 2002
Junior Member
Hello madhan,
Thanks for your kind information. I already solved that problem and also given the solution in orafaq itself as reply to my forum

bye
Vinoth P
Re: how to insert a file into oracle blob object [message #91348 is a reply to message #91018] Wed, 24 July 2002 06:49 Go to previous messageGo to next message
Venkata Reddy Valluri
Messages: 2
Registered: July 2002
Junior Member
Hi
As you told, I inserted files of any size into oracle blob, thks for good solution, but when i tried to get blob and convert it into file it is working fine for files of size < 4k, but giving problem for files of size > 4k
here is my code
InputStream in = rs.getBinaryStream(6);
----> here if file size > 4k it is returning "in" as null, but if < 4k it is ok and works fine
String filename = "sample.doc";
FileOutputStream file = new FileOutputStream(filename);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[[]] ch=new byte[[4]];
int n;
System.out.print("Odd: ");
while ((n=in.read(ch,0,ch.length))!=-1) baos.write(ch,0,n);
file.write(baos.toByteArray());

Any help gr8ly appreciated
Thks
Venkata Reddy Valluri
Re: how to insert a file into oracle blob object [message #91379 is a reply to message #90949] Fri, 02 August 2002 10:10 Go to previous messageGo to next message
Ami
Messages: 5
Registered: August 2002
Junior Member
please help me clearly in using blob in oracle.
Re: how to insert a file into oracle blob object [message #92022 is a reply to message #90949] Wed, 04 February 2004 10:44 Go to previous messageGo to next message
spaskal
Messages: 1
Registered: February 2004
Junior Member
how did you solve this problem:

for msword file i am getting an error
java.sql.SQLException: [[Oracle]][[ODBC]][[Ora]]ORA-24307: invalid length for piece

I have image and I have to upload it to the blob field. Can you help me, I am new in JAVA
Re: how to insert a file into oracle blob object [message #92152 is a reply to message #90949] Fri, 26 March 2004 13:04 Go to previous messageGo to next message
piter
Messages: 2
Registered: December 2000
Junior Member
package prueba;
import java.sql.*;
import java.io.*;
import java.util.*;
// Importing the Oracle Jdbc driver package makes the code more readable
import oracle.jdbc.driver.*;
//needed for new CLOB and BLOB classes
import oracle.sql.*;
public class uploadFile
{
public static void main (String args [[]])
{
uploadFile upload=new uploadFile();
upload.insertFile(new File("c:\tutorial.pdf"));
}

public void insertFile(File file)
{
Connection conn=null;
Statement stmt =null;
ResultSet rset =null;
try
{
String name=file.getName();
// Register the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn =DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:dbdes", "scott", "tiger");
// It's faster when auto commit is off
conn.setAutoCommit (false);
// Create a Statement
stmt = conn.createStatement ();
long size=file.length();
Long lg=new Long(size);
int k=lg.intValue();
// Populate the table
String query="INSERT INTO PRUEBA(ID,titulo,archivo) ";
query+="VALUES(1,'Arechivo de Prueba',EMPTY_BLOB())";
try
{
stmt.execute(query);
}catch(Exception co)
{
String msgError=co.getMessage();
}

// Select the BLOB
rset = stmt.executeQuery ("SELECT archivo FROM PRUEBA WHERE ID=1 FOR UPDATE");
if (rset.next ())
{
BLOB blob = ((OracleResultSet)rset).getBLOB (1);
FileInputStream istream = new FileInputStream (file);
// Create an OutputStram object to write the BLOB as a stream
OutputStream ostream = blob.getBinaryOutputStream ();
// Create a tempory buffer
byte[[]] buffer = new byte[[1024]];
int length = 0;
// Use the read() method to read the GIF file to the byte
// array buffer, then use the write() method to write it to
// the BLOB.
while ((length = istream.read(buffer)) != -1)
ostream.write(buffer, 0, length);
// Close the inputstream and outputstream
istream.close();
ostream.close();
}
conn.commit();
}catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
e.printStackTrace();
try{
conn.rollback();;
}catch(Exception er){}
}finally
{
try
{
if(rset!=null) rset.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(Exception ex){ex.printStackTrace();}
}
}

}
Re: how to insert a file into oracle blob object [message #92441 is a reply to message #92152] Mon, 19 July 2004 22:50 Go to previous message
tiger
Messages: 11
Registered: August 2002
Junior Member
what to get package prueba ???
Previous Topic: Compiling java stored procedure
Next Topic: oracore9.dll jdbc and f60jdapi.jar
Goto Forum:
  


Current Time: Fri Nov 22 14:12:27 CST 2024