How To Store File Into Database Using Java
How To Store File Into Database Using Java
How To Store File Into Database Using Java
It has 3 parameters
1 – It represents that data set to First column.
fr – Data read from fr.
(int)TxtFile.length() – length of the file.
Output
Java file Name: FileInsertExample.java
D:\jdk1.7.0_02\bin> javac FileInsertExample.java
D:\jdk1.7.0_02\bin> java FileInsertExample
No. of records updated = 1
4. Read the image using FileInputStream and store it into the table using
setBinaryStream() method of Statement interface.
pstmt.setBinaryStream(1,fis,(int)ImageFile.length());
It has 3 parameters.
1 represents First column of ImageTable where the image should be
stored.
fis is a object of FileInputStream that read image.
ImageFile.length() gives ImageFile size in bytes.
pstmt.executeUpdate();
Output
Java File Name: ImageInsert.java
D:\jdk1.7.0_02\bin> javac ImageInsert.java
D:\jdk1.7.0_02\bin> java ImageInsert
No. of records updated = 1
Conclusion
JVM is a program which used to execute java class files. JVM is a
platform dependent. For different operating systems different JVM
program available on internet. It is free available on internet. It converts
bytecodes instruction into the machine language instruction.
Conclusion
If we write a java program with X processor and Y operating system then
that program is executable on any other computer system with any
processor and any operating system. So one program or software which
is written in java. It can be execute to any system on internet but it is not
possible with C and C++. Thus java language is the best suitable
language for internet.
JRE provides libraries, java virtual machine and other supporting files to
run applets and applications written in java programming language.
If we want to store images into database then we use BLOB data type to
store images. BLOB(Binary Large Object) is a SQL data type that
represents binary data to be stored into a database. BLOBs used to
store images, audio files or multimedia files.
4. Read the image using FileInputStream and store it into the table using
setBinaryStream() method of Statement interface.
pstmt.setBinaryStream(1,fis,(int)ImageFile.length());
It has 3 parameters.
1 represents First column of ImageTable where the image should be
stored.
fis is a object of FileInputStream that read image.
ImageFile.length() gives ImageFile size in bytes.
pstmt.executeUpdate();
import java.sql.*;
2
import java.io.*;
3
public class ImageInsert
4
{
5
public static void main(String[] args)
6
{
7 try
8
9
{
10
Class.forName("oracle.jdbc.driver.OracleDriver");
11
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localho
12 //Use PreparedStatement to update table. Here table is ImageTable.
23 con.close();
24 }
25 catch (Exception e)
{
26
e.printStackTrace();
27
}
28
}
29
}
30
31
Output
Java File Name: ImageInsert.java
D:\jdk1.7.0_02\bin> javac ImageInsert.java
D:\jdk1.7.0_02\bin> java ImageInsert
No. of records updated = 1