Oracle saves and reads images,

Source: Internet
Author: User

Oracle saves and reads images,

Oracle image storage and reading tutorials

Package com. kevinb. image; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import oracle. jdbc. driver. oracleBlobInputStream; import com. kevinb. sys. db. connectOracleBase; public class ProImage {/** Database user */private static final String DBUSER = "scott";/** database password */private static final String DBPSW = "ffffff "; /** database server */private static final String DBSERVER = "orcl";/*** Save image information to the database; * @ param path */@ SuppressWarnings ({"static-access", "deprecation"}) public void insertImage (String path) {System. out. println ("---- insertImage"); Connection conn = null; Pr EparedStatement ps = null; conn = ConnectOracleBase. getConnection (this. DBUSER, this. DBPSW, this. DBSERVER); String SQL = "insert into user_manager u (u. user_id, u. user_name, u. user_photo) "+" values (scott. hibernate_sequence.nextval ,?,?) "; Try {ps = conn. prepareStatement (SQL); ps. setString (1, "Hello"); File file = new File (path); FileInputStream fStream = new FileInputStream (file); ps. setBinaryStream (2, fStream, (int) file. length (); ps.exe cute ();} catch (SQLException e) {e. printStackTrace ();} catch (FileNotFoundException e) {e. printStackTrace ();} finally {try {ConnectOracleBase. closeAllConnection (ps, conn);} catch (SQLExc Eption e) {e. printStackTrace () ;}} System. out. println ("image saved successfully! ");}/*** Get the image information through the employee's empId; * @ param userId */@ SuppressWarnings (" static-access ") public void getPhoto (int userId, string path) {System. out. println ("---- getPhoto"); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; conn = ConnectOracleBase. getConnection (this. DBUSER, this. DBPSW, this. DBSERVER); String SQL = "select u. user_id, u. user_name, u. user_photo "+" from user_manager u "+" Where u. user_id =? "; Try {ps = conn. prepareStatement (SQL); ps. setInt (1, userId); rs = ps.exe cuteQuery (); OracleBlobInputStream fStream = null; while (rs. next () {fStream = (OracleBlobInputStream) rs. getBinaryStream ("user_photo");} FileOutputStream fOutputStream = new FileOutputStream (path); byte [] buffer = new byte [1024]; int byteRead = 0; while (byteRead = fStream. read (buffer ))! =-1) {fOutputStream. write (buffer, 0, byteRead);} fOutputStream. close (); fStream. close ();} catch (SQLException e) {e. printStackTrace ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {ConnectOracleBase. closeAllConnection (rs, ps, conn);} catch (SQLException e) {e. printStackTrace () ;}} System. out. println ("get photo is succ Ess! ");}}

Oraclebase is a self-encapsulated database connection operation class;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.