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;