往MySQL中儲存圖片

來源:互聯網
上載者:User
往MySQL中儲存圖片 1 介紹在設計到資料庫的開發中,難免要將圖片或音頻檔案插入到資料庫中的情況。一般來說,我們可以同過插入圖片檔案相應的儲存位置,而不是檔案本身,來避免直接向資料庫裡插入的麻煩。但有些時候,向MySQL中插入圖片更加容易管理。那麼在MySQL中該怎麼儲存呢?參考資料[1]中有個相當清晰的例子,不過是基於MySQL圖形介面的查詢工具Query Brower的,你的機子上沒有安裝的話,可能得不到很好的理解。我在這裡不在贅述,更詳細的資料請看給出的連結吧。還有,[1]中的例子其實只是向我們說明了Query Brower的易用和強大,對我們在開發中實際應用不是很大。所以下面就讓我們用JAVA寫一個向MySQL中儲存的簡單一實例。2 建表首先,先要在資料庫中建表。我在名為test的資料庫下建立了一個叫pic的表。該表包括3列,idpic, caption和img。其中idpic是主鍵,caption是對圖片的表述,img是影像檔本身。建表的SQL語句如下:DROP TABLE IF EXISTS `test`.`pic`;CREATE TABLE `test`.`pic` ( `idpic` int(11) NOT NULL auto_increment, `caption` varchar(45) NOT NULL default '', `img` longblob NOT NULL, PRIMARY KEY (`idpic`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;將上面的語句輸入到命令列中(如果安裝了Query Brower, 你可以按照參考[1]中的指示來建表,那樣會更加方便。),執行,表建立成功。3 實現映像儲存類表完成後,我們就開始寫個Java類,來完成向資料庫中插入圖片的操作。我們知道,Java與資料庫連接是通過JDBC driver來實現的。我用的是MySQL網站上提供的MySQL Connector/J,如果你用的是其他類型的driver, 在下面的實現過程中可能會有些許差別。 3.1 裝載JDBC驅動,建立串連JDK中提供的DriverManager介面用來管理Java Application 和 JDBC Driver之間的串連。在使用這個介面之前, DriverManager需要知道要串連的JDBC 驅動。最簡單的方法就是用Class.forName()來向DriverManager註冊實現了java.sql.Driver 的介面類。對MySQL Connector/J來說,這個類的名字叫com.mysql.jdbc.Driver。下面這個簡單的樣本說明了怎樣來註冊Connector/J Driver。import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException; public class LoadDriver {    public static void main(String[] args) {        try {            // The newInstance() call is a work around for some            // broken Java implementations             Class.forName("com.mysql.jdbc.Driver").newInstance();                        // Connection con = DriverManager.getConnection(……)            // ……        } catch (Exception ex) {            // handle the error        }}向DriverManager註冊了驅動後,我們就可以通過調用 DriverManager.getConnection()方法來獲得和資料庫的串連。其實在上面的例子中就有這條語句,只不過被注釋掉了。在後面的實現中會有完整的例子。3.2 PreparedStatement完成上面的步驟後,我們就可以同過建立的串連建立Statement介面類,來執行一些SQL語句了。在下面的例子,我用的是PreparedStatement,還有CallableStatement,它可以執行一些預存程序和函數,這裡不多講了。下面的代碼片斷是向pic表中插入一條記錄。其中(1)處Connection介面的對象con通過調用prepareStatement 方法得到先行編譯的SQL 陳述式(precompiled SQL statement);(2)處是為該insert語句的第一個問號賦值,(3)為第二個賦值,(4)為第三個,這步也是最該一提的,用的方法是setBinaryStream(),第一個參數3是指第三個問號,fis是一個二進位檔案流,第三個參數是該檔案流的長度。 PreparedStatement ps;…ps = con.prepareStatement("insert into PIC values (?,?,?)"); // (1)ps.setInt(1, id); //(2)ps.setString(2, file.getName()); (3)ps.setBinaryStream(3, fis, (int)file.length()); (4)ps.executeUpdate();…3.3 完整代碼上面列出了完整的代碼。package com.forrest.storepic; import java.io.File;import java.io.FileInputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException; /** * This class describes how to store picture file into MySQL. * @author Yanjiang Qian * @version 1.0 Jan-02-2006 */public class StorePictures {        private String dbDriver;    private String dbURL;    private String dbUser;    private String dbPassword;    private Connection con;    private PreparedStatement ps;        public StorePictures() {        dbDriver = "com.mysql.jdbc.Driver";        dbURL = "jdbc:mysql://localhost:3306/test";        dbUser = "root";        dbPassword = "admin";        initDB();    }        public StorePictures(String strDriver, String strURL,            String strUser, String strPwd) {        dbDriver = strDriver;        dbURL = strURL;        dbUser = strUser;        dbPassword = strPwd;        initDB();    }     public void initDB() {        try {            // Load Driver            Class.forName(dbDriver).newInstance();            // Get connection            con = DriverManager.getConnection(dbURL,                    dbUser, dbPassword);                    } catch(ClassNotFoundException e) {            System.out.println(e.getMessage());        } catch(SQLException ex) {            // handle any errors            System.out.println("SQLException: " + ex.getMessage());            System.out.println("SQLState: " + ex.getSQLState());            System.out.println("VendorError: " + ex.getErrorCode());         } catch (Exception e) {            System.out.println(e.getMessage());        }     }     public boolean storeImg(String strFile) throws Exception {        boolean written = false;        if (con == null)            written = false;        else {            int id = 0;            File file = new File(strFile);            FileInputStream fis = new FileInputStream(file);                        try {                               ps = con.prepareStatement("SELECT MAX(idpic) FROM PIC");                ResultSet rs = ps.executeQuery();                                if(rs != null) {                    while(rs.next()) {                        id = rs.getInt(1)+1;                    }                } else {                            return written;                }                                ps = con.prepareStatement("insert "                         + "into PIC values (?,?,?)");                ps.setInt(1, id);                ps.setString(2, file.getName());                ps.setBinaryStream(3, fis, (int) file.length());                ps.executeUpdate();                                 written = true;            } catch (SQLException e) {                written = false;                System.out.println("SQLException: "                        + e.getMessage());                System.out.println("SQLState: "                        + e.getSQLState());                System.out.println("VendorError: "                        + e.getErrorCode());                e.printStackTrace();            } finally {                             ps.close();                fis.close();                // close db con                con.close();            }        }        return written;    }        /**     * Start point of the program     * @param args CMD line     */    public static void main(String[] args) {        if(args.length != 1) {            System.err.println("java StorePictures filename");            System.exit(1);        }        boolean flag = false;        StorePictures sp = new StorePictures();        try {            flag = sp.storeImg(args[0]);         } catch (Exception e) {            e.printStackTrace();        }        if(flag) {            System.out.println("Picture uploading is successful.");         } else {            System.out.println("Picture uploading is failed.");        }    }}4 總結到此,我們就介紹完了使用往MySQL儲存圖片的全過程。這個例子是個最簡單的執行個體,讀者可以根據自己的實際需要加入其它的一些功能,比如讀取檔案,刪除等,使整個程式更加完善。在寫這篇文章的時候主要參考了參考[2],在此謝過。參考[3]中是一個相當漂亮的例子,不僅有儲存還有讀取,而且是有非常直觀的圖形介面,感興趣的可以深入研究。

 參考資料[1] Michael Zinner, http://forums.mysql.com/read.php?20,17671,27914#msg-27914. Feb. 2, 06[2] Kasi Mono, http://www.jguru.com/forums/view.jsp?EID=720163. Feb. 2, 06[3] Nils Christian Svihus, http://www.jguru.com/faq/view.jsp?EID=1278882. Feb. 2, 06

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.