Using Oracle BLOB Types in Java

xiaoxiao2021-03-06  86

-------------------------------------------------- --- BLOB -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------ / ** ORACLE will be byte []-> blob -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------- Final String SQL_INSERT = "INSERT INTO TEST (PIC, ID) VALUES (EMPTY_BLOB (), 1)"; // where EMPTY_BLOB (), cannot be less

final String SQL_UPDATE = "select pic from test where id = 1 for update"; con.setAutoCommit (false); pstmt = con.prepareStatement (SQL_INSERT); pstmt.executeUpdate (); pstmt = con.prepareStatement (SQL_UPDATE); rs = Pstmt.executeQuery ();

Oracle.sql.blob b = null; while (rs.next ()) {b = (oracle.sql.blob) Rs.getblob (1);

// Updates blob.// byte [] content

OutputStream os = b.getBinaryOutputStream (); ByteArrayInputStream is = new ByteArrayInputStream (content); byte [] buffer = new byte [b.getBufferSize ()]; int bytesRead = 0; while ((bytesRead = is.read (buffer)) ! = -1) Os.Write (buffer, 0, bytesread); Os.close (); is.close (); con enabled ();

/ ** Oracle's BLOB type read blob -> OutputStream -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------- InputStream in = null; PSTMT = con .preparestatement ("SELECT PIC from Test where id = 1); rs = pstmt.executeQuery ();

IF (rs.next ()) {IN = rs.getbinaryStream ("PIC"); byte [] b = new byte [1024 * 1024]; int Len; outputstream outs = _RESPONSE.GETOUTPUTSTREAM (); // In servlet With Response, suitable for other OutputStream While ((len = in.read (b))> 0) {Outs.write (B, 0, LEN);} in.close (); outs.flush (); OUTS. CLOSE ();

// blob -> file ------------------------------------- BLOB B = RS. getBlob (Column_NO); inputStream inputStream = b.getBinaryStream (); long length = b.length (); byte [] buffer = new byte [length]; int lengthRead = 0; String fileName = "wwww.gif"; FileOutputStream outputStream = new FileOutputStream (fileName); while ((lengthRead = inputStream.read (buffer)) = -1!) {outputStream.write (buffer, 0, lengthRead);} inputStream.close (); outputStream.close (); / / File -> blob ------------------------------------- File File = New File fileName); int length = (int) file.length (); FileInputStream fileInputStream = new FileInputStream (file); preparedStatement.setBinaryStream (1, fileInputStream, length); preparedStatement.execute ();

-------------------------------------------------- --- CLOB ---------------------------------- For Long Row ----

// String type Introduction; String -> Clob -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -String value = new String ( "............"); Reader bodyReader = null; String nullString = ""; if (value == null) value = nullString; bodyReader = new StringReader (value); pstmt.setCharacterStream (parameterIndex , BodyReader, Value.Length ());

转载请注明原文地址:https://www.9cbs.com/read-124234.html

New Post(0)