Friday, 1 May 2015

store file in Oracle database

Example to store file in Oracle database:
The setCharacterStream() method of PreparedStatement is used to set character information into the parameterIndex.
Syntax:
1) public void setBinaryStream(int paramIndex,InputStream stream)throws SQLException
2) public void setBinaryStream(int paramIndex,InputStream stream,long length)throws SQLException
For storing file into the database, CLOB (Character Large Object) datatype is used in the table. For example:
1.    CREATE TABLE  "FILETABLE"   
2.       (    "ID" NUMBER,   
3.        "NAME" CLOB  
4.       )  
5.    /  
1.    import java.io.*;  
2.    import java.sql.*;  
3.      
4.    public class StoreFile {  
5.    public static void main(String[] args) {  
6.    try{  
7.    Class.forName("oracle.jdbc.driver.OracleDriver");  
8.    Connection con=DriverManager.getConnection(  
9.    "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
10.               
11. PreparedStatement ps=con.prepareStatement(  
12. "insert into filetable values(?,?)");  
13.               
14. File f=new File("d:\\myfile.txt");  
15. FileReader fr=new FileReader(f);  
16.               
17. ps.setInt(1,101);  
18. ps.setCharacterStream(2,fr,(int)f.length());  
19. int i=ps.executeUpdate();  
20. System.out.println(i+" records affected");  
21.               
22. con.close();  
23.               
24. }catch (Exception e) {e.printStackTrace();}  
25. }  

26. }  

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...