write picture to oracle using java

imlihj2007發表於2008-01-06

import java.sql.*;
import java.io.*;
public class Blobtest {
public Blobtest() {
}
public static void main (String args [])
throws SQLException, IOException
{
write();
//read();
}
static void read() throws SQLException, IOException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:@ebizser:1521:serverdb", "scott", "tiger");
conn.setAutoCommit (false);
Statement stmt = conn.createStatement ();
ResultSet rset =
stmt.executeQuery ("select col2 from lobtest where userid=1");
if (rset.next ())
{
InputStream gif_data = rset.getBinaryStream (1);
FileOutputStream os = new FileOutputStream ("example.jpe");
int c;
while ((c = gif_data.read ()) != -1)
os.write (c);
os.close ();
}
if (rset != null)
rset.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
}
static void write() throws SQLException, IOException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@oralihj", "yssj", "misd");
conn.setAutoCommit (false);
File file = new File("C:1.jpg");
InputStream is = new FileInputStream ("C:1.jpg");
PreparedStatement pstmt =
conn.prepareStatement ("insert into pic (id, pic ) values (?, ?)");
pstmt.setBinaryStream(2, is, (int)file.length ());
pstmt.setInt (1, 2);
pstmt.execute ();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
}
}

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9879835/viewspace-996783/,如需轉載,請註明出處,否則將追究法律責任。

相關文章