Anda di halaman 1dari 3

BLOB

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BlobClobEx {


public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL",


"yourName", "mypwd");

Statement stmt = conn.createStatement();

createBlobClobTables(stmt);

PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)");

File file = new File("blob.txt");


FileInputStream fis = new FileInputStream(file);
pstmt.setBinaryStream(1, fis, (int) file.length());

file = new File("clob.txt");


fis = new FileInputStream(file);
pstmt.setAsciiStream(2, fis, (int) file.length());
fis.close();

pstmt.execute();

ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40");


rs.next();

java.sql.Blob blob = rs.getBlob(2);


java.sql.Clob clob = rs.getClob(3);

byte blobVal[] = new byte[(int) blob.length()];


InputStream blobIs = blob.getBinaryStream();
blobIs.read(blobVal);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(blobVal);
blobIs.close();

char clobVal[] = new char[(int) clob.length()];


Reader r = clob.getCharacterStream();
r.read(clobVal);
StringWriter sw = new StringWriter();
sw.write(clobVal);

r.close();
conn.close();
}

public static void createBlobClobTables(Statement stmt) throws Exception {


String Sql = "CREATE TABLE BlobClob(Id NUMBER(3), b BLOB, c CLOB)";

try {
stmt.executeUpdate("DROP TABLE BlobClob");
} catch (SQLException se) {
if (se.getErrorCode() == 942)
System.out.println("Error dropping BlobClob table:" + se.getMessage());
}
if (stmt.executeUpdate(Sql) == 0)
System.out.println("BlobClob table created...");

}
}

CLOB

import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Main {


private static String url = "jdbc:oracle:thin:@localhost:1521:xe";

private static String username = "yourDatabase";

private static String password = "welcome";

public static void main(String[] args) throws Exception {

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);

String sql = "INSERT INTO documents (name, description, data) VALUES (?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "a.txt");
stmt.setString(2, "b");

File data = new File("C:\\a.txt");


FileReader reader = new FileReader(data);
stmt.setCharacterStream(3, reader, (int) data.length());
stmt.execute();

conn.commit();
reader.close();
conn.close();

}
}