package javaapplication6;
import oracle.sql.BLOB;
import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;
public class JavaApplication6 {
public static void main (String args [])
throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@xe", "system", "123");
conn.setAutoCommit(false);
//create table with Blob
Statement stmt = conn.createStatement ();
stmt.execute ("CREATE TABLE photos (pname varchar2 (30),photo blob)");
OraclePreparedStatement ps=(OraclePreparedStatement)conn.prepareStatement("insert into photos(pname,photo) values(?,?)");
BLOB blob=BLOB.createTemporary(conn, false,BLOB.MODE_READWRITE);
fillBlob(conn,blob);
ps.setString(1,"Ram");
ps.setBlob(2, blob);
ps.executeUpdate();
conn.commit();
PreparedStatement ps1=conn.prepareStatement("select * from photos where pname='Ram'");
ResultSet rs=ps1.executeQuery();
rs.next();
String pname=rs.getString(1);
blob=(BLOB)rs.getBlob(2);
dumpBlob(conn,blob);
conn.close();
}
static void fillBlob (Connection conn, BLOB blob)
throws Exception
{
OutputStream outstream = blob.getBinaryOutputStream();
FileInputStream fin=new FileInputStream("f://koala.jpg");
byte [] data =new byte[fin.available()];
fin.read(data);
outstream.write(data, 0, data.length);
//blob.setBytes(data);
fin.close();
outstream.close();
}
static void dumpBlob (Connection conn, BLOB blob)
throws Exception
{
InputStream instream = blob.getBinaryStream();
byte[] buffer = new byte[10];
int length = 0;
FileOutputStream fout=new FileOutputStream("f://new.jpg");
while ((length = instream.read(buffer)) != -1)
{
fout.write(buffer,0,length);
}
fout.close();
instream.close();
}
}
import oracle.sql.BLOB;
import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;
public class JavaApplication6 {
public static void main (String args [])
throws Exception
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@xe", "system", "123");
conn.setAutoCommit(false);
//create table with Blob
Statement stmt = conn.createStatement ();
stmt.execute ("CREATE TABLE photos (pname varchar2 (30),photo blob)");
OraclePreparedStatement ps=(OraclePreparedStatement)conn.prepareStatement("insert into photos(pname,photo) values(?,?)");
BLOB blob=BLOB.createTemporary(conn, false,BLOB.MODE_READWRITE);
fillBlob(conn,blob);
ps.setString(1,"Ram");
ps.setBlob(2, blob);
ps.executeUpdate();
conn.commit();
PreparedStatement ps1=conn.prepareStatement("select * from photos where pname='Ram'");
ResultSet rs=ps1.executeQuery();
rs.next();
String pname=rs.getString(1);
blob=(BLOB)rs.getBlob(2);
dumpBlob(conn,blob);
conn.close();
}
static void fillBlob (Connection conn, BLOB blob)
throws Exception
{
OutputStream outstream = blob.getBinaryOutputStream();
FileInputStream fin=new FileInputStream("f://koala.jpg");
byte [] data =new byte[fin.available()];
fin.read(data);
outstream.write(data, 0, data.length);
//blob.setBytes(data);
fin.close();
outstream.close();
}
static void dumpBlob (Connection conn, BLOB blob)
throws Exception
{
InputStream instream = blob.getBinaryStream();
byte[] buffer = new byte[10];
int length = 0;
FileOutputStream fout=new FileOutputStream("f://new.jpg");
while ((length = instream.read(buffer)) != -1)
{
fout.write(buffer,0,length);
}
fout.close();
instream.close();
}
}
No comments:
Post a Comment