interface Blob:
An SQL BLOB is a built-in type that stores a Binary Large Object as a column value in a row of a database table. By default drivers implement Blob using an SQL locator(BLOB), which means that a Blob object contains a logical pointer to the SQL BLOB data rather than the data itself. A Blob object is valid for the duration of the transaction in which is was created.
Method Summary
|
Methods |
|
|
Modifier and Type |
Method and Description |
|
Retrieves the BLOB value designated by this Blob instance as a stream. |
|
|
getBinaryStream(long pos, long length) Returns an InputStream object that contains a partial Blob value, starting with the byte specified by pos, which is length bytes in length. |
|
|
byte[] |
getBytes(long pos, int length) Retrieves all or part of the BLOB value that this Blob object represents, as an array of bytes. |
|
long |
length() Returns the number of bytes in the BLOB value designated by this Blob object. |
|
setBinaryStream(long pos) Retrieves a stream that can be used to write to the BLOB value that this Blob object represents. |
|
|
int |
setBytes(long pos, byte[] bytes) Writes the given array of bytes to the BLOB value that this Blob object represents, starting at position pos, and returns the number of bytes written. |
|
int |
setBytes(long pos, byte[] bytes, int offset, int len) Writes all or part of the given byte array to the BLOB value that this Blob object represents and returns the number of bytes written. |
Program to insert image to table:
import java.sql.*;
import java.io.*;
class InsertImg
{
public static void main(String args[])
{
try
{
Class.forName(“com.mysql.cj.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/sydb”,”root”,”pass123″);
File f= new File(“kisi.jpg”);
FileInputStream fis = new FileInputStream(f);
PreparedStatement ps=con.prepareStatement(“insert into studinfo values(?,?,?)”);
ps.setInt(1,201);
ps.setString(2,”trishna”);
ps.setBinaryStream(3, fis, (int) f.length());
ps.executeUpdate();
fis.close();
ps.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Program to read Images from table:
import java.sql.*;
import java.io.*;
import java.util.Scanner;
class ReadImgage
{
public static void main(String args[])
{
try
{
Class.forName(“com.mysql.cj.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/sydb”,”root”,”pass123″);
Statement st=con.createStatement();
System.out.println(“Enter rollno:”);
Scanner sc=new Scanner(System.in);
int roll=sc.nextInt();
ResultSet rs=st.executeQuery(“select photo from studinfo where rollno=”+roll);
byte img[]=new byte[6500];
while(rs.next() )
{ img=rs.getBytes(“photo”);
}
FileOutputStream fos = new FileOutputStream(“e://”+roll+”.jpg”);
fos.write(img);
}
catch(Exception e)
{
System.out.println(e);
} } }
Interface CLOB:
An SQL CLOB is a built-in type that stores a Character Large Object as a column value in a row of a database table. By default drivers implement a Clob object using an SQL locator(CLOB), which means that a Clob object contains a logical pointer to the SQL CLOB data rather than the data itself. A Clob object is valid for the duration of the transaction in which it was created.
Method Summary
|
Modifier and Type |
Method and Description |
|
Retrieves the CLOB value designated by this Clob object as an ascii stream. |
|
|
Retrieves the CLOB value designated by this Clob object as a java.io.Reader object (or as a stream of characters). |
|
|
getCharacterStream(long pos, long length) Returns a Reader object that contains a partial Clob value, starting with the character specified by pos, which is length characters in length. |
|
|
getSubString(long pos, int length) Retrieves a copy of the specified substring in the CLOB value designated by this Clob object. |
|
|
long |
length() Retrieves the number of characters in the CLOB value designated by this Clob object. |
|
long |
position(Clob searchstr, long start) Retrieves the character position at which the specified Clob object searchstr appears in this Clob object. |
|
long |
position(String searchstr, long start) Retrieves the character position at which the specified substring searchstr appears in the SQL CLOB value represented by thisClob object. |
|
setAsciiStream(long pos) Retrieves a stream to be used to write Ascii characters to the CLOB value that this Clob object represents, starting at position pos. |
|
|
setCharacterStream(long pos) Retrieves a stream to be used to write a stream of Unicode characters to the CLOB value that this Clob object represents, at positionpos. |
|
|
int |
setString(long pos, String str) Writes the given Java String to the CLOB value that this Clob object designates at the position pos. |
|
int |
setString(long pos, String str, int offset, int len) Writes len characters of str, starting at character offset, to the CLOB value that this Clob represents. |