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:
- CREATE TABLE "FILETABLE"
- ( "ID" NUMBER,
- "NAME" CLOB
- )
- /
Java Example to store file in database
- import java.io.*;
- import java.sql.*;
-
- public class StoreFile {
- public static void main(String[] args) {
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection con=DriverManager.getConnection(
- "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
-
- PreparedStatement ps=con.prepareStatement(
- "insert into filetable values(?,?)");
-
- File f=new File("d:\\myfile.txt");
- FileReader fr=new FileReader(f);
-
- ps.setInt(1,101);
- ps.setCharacterStream(2,fr,(int)f.length());
- int i=ps.executeUpdate();
- System.out.println(i+" records affected");
-
- con.close();
-
- }catch (Exception e) {e.printStackTrace();}
- }
- }
No comments:
Post a Comment