JDBC implementation file and Image Storage instances

Source: Internet
Author: User

/* Implemented functions: Use a database to store text data and read it and put it in the current project for analysis: the difficulty is not great. The key is to master the file stream, and there are not many database operations, but the file stream reads and writes are many dates: 20131003 Author: */public class Ckb_test {public static void main (String [] args) throws SQLException, IOException {// TODO Auto-generated method stubread ();} static void create () throws SQLException, IOException {Connection conn = null; PreparedStatement prest = null; ResultSet resultset = null; try {// 2. establish connection conn = JdbcUtils. getConnection (); // Singleton design mode conn = JdbcUtilsSingle. getInstance (). getConnection (); // 3. create statement String SQL = "insert into ckb_test (text) values (?) "; Prest = conn. prepareStatement (SQL); File file = new File ("src/cn/com/JDBC/JdbcUtils. java "); Reader reader = new BufferedReader (new FileReader (file); prest. setCharacterStream (1, reader, (int) file. length (); // 4. execute the statement int imo-prest.exe cuteUpdate (); reader. close (); System. out. println ("I =" + I);} finally {JdbcUtils. free (resultset, prest, conn) ;}} static void read () throws SQLException, IOException {Connection conn = null; Statement st = null; ResultSet resultset = null; try {// 2. establish connection conn = JdbcUtils. getConnection (); // Singleton design mode conn = JdbcUtilsSingle. getInstance (). getConnection (); // 3. create statement st = conn. createStatement (); // 4.execute the sentence resultset=st.exe cuteQuery ("select text from ckb_test"); // 5. processing result while (resultset. next () {Clob clob = resultset. getClob (1); Reader reader = clob. getCharacterStream (); // reader = resultset. getCharacterStream (1); File file = new File ("JdbcUtils. java "); Writer writer = new BufferedWriter (new FileWriter (file); char [] buff = new char [1024]; for (int I = 0; (I = reader. read (buff)> 0;) {writer. write (buff, 0, I);} writer. close (); reader. close () ;}finally {JdbcUtils. free (resultset, st, conn) ;}} functions/*: Stores image data in a database and stores the data in the current project. The analysis is not very difficult, the key is There are not many database operations for holding byte streams, but there are many reads and writes to the file streams. Note that you must change the image directory Date: 20131003 Author: */public class PictureBlob {public static void main (String [] args) throws SQLException, IOException {read ();} static void create () throws SQLException, IOException {Connection conn = null; PreparedStatement prest = null; ResultSet resultset = null; try {// 2. establish connection conn = JdbcUtils. getConnection (); // Singleton design mode conn = JdbcUtilsSingle. getInstance (). getConnection (); // 3. Create statement String SQL = "insert into blob_test (big_bit) values (?) "; Prest = conn. prepareStatement (SQL); File file = new File ("C: \ Documents ents and Settings \ Administrator \ My Documents ents \ My Pictures \ cxg.jpg "); inputStream in = new BufferedInputStream (new FileInputStream (file); prest. setBinaryStream (1, in, (int) file. length (); // 4. execute the statement int imo-prest.exe cuteUpdate (); in. close (); System. out. println ("I =" + I);} finally {JdbcUtils. free (resultset, prest, conn) ;}} static void read () throws SQLException, IOException {Connection conn = null; Statement st = null; ResultSet resultset = null; try {// 2. establish connection conn = JdbcUtils. getConnection (); // Singleton design mode conn = JdbcUtilsSingle. getInstance (). getConnection (); // 3. create statement st = conn. createStatement (); // 4.execute the sentence resultset=st.exe cuteQuery ("select big_bit from blob_test"); // 5. processing result while (resultset. next () {Blob blob = resultset. getBlob (1); InputStream in = blob. getBinaryStream (); // reader = resultset. getCharacterStream (1); File file = new File ("1.jpeg"); OutputStream out = new BufferedOutputStream (new FileOutputStream (file); byte [] buff = new byte [1024]; for (int I = 0; (I = in. read (buff)> 0;) {out. write (buff, 0, I);} out. close (); in. close () ;}finally {JdbcUtils. free (resultset, st, conn );}}}

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.