久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      mysql中怎樣插入圖片

      mysql中插入圖片的方法:首先要在數(shù)據(jù)庫(kù)中建表;然后裝載JDBC驅(qū)動(dòng),建立連接;最后創(chuàng)建Statement接口類(lèi),來(lái)執(zhí)行SQL語(yǔ)句即可。

      mysql中怎樣插入圖片

      mysql中插入圖片的方法:

      1、首先,先要在數(shù)據(jù)庫(kù)中建表。我在名為test的數(shù)據(jù)庫(kù)下建立了一個(gè)叫pic的表。該表包括3列,idpic, caption和img。其中idpic是主鍵,caption是對(duì)圖片的表述,img是圖像文件本身。建表的SQL語(yǔ)句如下:

      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;

      將上面的語(yǔ)句輸入到命令行中(如果安裝了Query Brower, 你可以按照參考[1]中的指示來(lái)建表,那樣會(huì)更加方便。),執(zhí)行,表建立成功。

      2、實(shí)現(xiàn)圖像存儲(chǔ)類(lèi)

      表完成后,我們就開(kāi)始寫(xiě)個(gè)Java類(lèi),來(lái)完成向數(shù)據(jù)庫(kù)中插入圖片的操作。我們知道,Java與數(shù)據(jù)庫(kù)連接是通過(guò)JDBC driver來(lái)實(shí)現(xiàn)的。我用的是MySQL網(wǎng)站上提供的MySQL Connector/J,如果你用的是其他類(lèi)型的driver, 在下面的實(shí)現(xiàn)過(guò)程中可能會(huì)有些許差別。

      2.1、裝載JDBC驅(qū)動(dòng),建立連接

      JDK中提供的DriverManager接口用來(lái)管理Java Application 和 JDBC Driver之間的連接。在使用這個(gè)接口之前, DriverManager需要知道要連接的JDBC 驅(qū)動(dòng)。最簡(jiǎn)單的方法就是用Class.forName()來(lái)向DriverManager注冊(cè)實(shí)現(xiàn)了java.sql.Driver 的接口類(lèi)。對(duì)MySQL Connector/J來(lái)說(shuō),這個(gè)類(lèi)的名字叫com.mysql.jdbc.Driver。

      下面這個(gè)簡(jiǎn)單的示例說(shuō)明了怎樣來(lái)注冊(cè)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注冊(cè)了驅(qū)動(dòng)后,我們就可以通過(guò)調(diào)用 DriverManager.getConnection()方法來(lái)獲得和數(shù)據(jù)庫(kù)的連接。其實(shí)在上面的例子中就有這條語(yǔ)句,只不過(guò)被注釋掉了。在后面的實(shí)現(xiàn)中會(huì)有完整的例子。

      2.2、PreparedStatement

      完成上面的步驟后,我們就可以同過(guò)建立的連接創(chuàng)建Statement接口類(lèi),來(lái)執(zhí)行一些SQL語(yǔ)句了。在下面的例子,我用的是PreparedStatement,還有CallableStatement,它可以執(zhí)行一些存儲(chǔ)過(guò)程和函數(shù),這里不多講了。

      下面的代碼片斷是向pic表中插入一條記錄。其中(1)處Connection接口的對(duì)象con通過(guò)調(diào)用prepareStatement 方法得到預(yù)編譯的SQL 語(yǔ)句(precompiled SQL statement);(2)處是為該insert語(yǔ)句的第一個(gè)問(wèn)號(hào)賦值,(3)為第二個(gè)賦值,(4)為第三個(gè),這步也是最該一提的,用的方法是setBinaryStream(),第一個(gè)參數(shù)3是指第三個(gè)問(wèn)號(hào),fis是一個(gè)二進(jìn)制文件流,第三個(gè)參數(shù)是該文件流的長(zhǎng)度。

      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(); …

      2.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.");     }   } }

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)