This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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 dbc racledsn","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
vinoth prabakaran
Greenhorn
Joined: Dec 28, 2001
Posts: 3
posted
0
Hi, i found the solution to insert and retrive file using blob object of oracle. Table Description Name Null? Type ----------------------------------------- -------- ---------------------------- FILEID NOT NULL VARCHAR2(255) NAME NOT NULL VARCHAR2(255) RESUME NOT NULL BLOB here is the code (InsertFile.java) 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 dbc racledsn","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(); 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.setAsciiStream(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(); } } }