如何在MySQL中存储图片

  介绍

如何在MySQL中存储图片?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

首先,先要在数据库中建表。我在名为测试的数据库下建立了一个叫图的表。该表包括3列,idpic,标题和img。其中idpic是主键,标题是对图片的表述,img是图像文件本身。建表的SQL语句如下:

DROP  TABLE  IF  EXISTS ‘测试’。‘图片’;   CREATE  TABLE ‘测试’。‘图片’,(   ,' idpic ' int (11), NOT  NULL  auto_increment,   ,“标题”varchar (45), NOT  NULL  default  & # 39; & # 39;   img的大敌;longblob  NOT 空,   ,PRIMARY  KEY  (“idpic”)   ),引擎=InnoDB  DEFAULT  CHARSET=use utf8;

将上面的语句输入到命令行中(如果安装了查询浏览器,你可以按照参考[1]中的指示来建表,那样会更加方便。),执行,表建立成功。

<强> 3实现图像存储类

表完成后,我们就开始写个Java类,来完成向数据库中插入图片的操作。我们知道,Java与数据库连接是通过JDBC驱动程序来实现的。我用的是MySQL网站上提供的MySQL连接器/J,如果你用的是其他类型的司机,在下面的实现过程中可能会有些许差别。

<强> 3.1装载JDBC驱动,建立连接

JDK中提供的DriverManager接口用来管理Java应用程序和JDBC驱动程序之间的连接。在使用这个接口之前,DriverManager需要知道要连接的JDBC驱动。最简单的方法就是用forname()来向DriverManager注册实现了java.sql。司机的接口类。对MySQL连接器/J来说,这个类的名字叫com.mysql.jdbc。司机。

下面这个简单的示例说明了怎样来注册连接器/J司机。

import  java.sql.Connection;   import  java.sql.DriverManager;   import  java.sql.SQLException;   ,   public  class  LoadDriver  {   public 才能;static  void  main (String [], args), {   ,,,try  {   ,,,,,//,,newInstance (), call  is  a  work  around  for 一些   ,,,,,//,broken  Java 实现   ,,,,,forname (“com.mysql.jdbc.Driver") .newInstance ();   ,,,,,   ,,,,,//,Connection  con =, DriverManager.getConnection (……)   ,,,,,//,……   ,,,},catch  (Exception 交货),{   ,,,,,//,handle 从而错误   ,,,}   }

向DriverManager注册了驱动后,我们就可以通过调用<代码> DriverManager.getConnection() 方法来获得和数据库的连接,其实在上面的例子中就有这条语句,只不过被注释掉了。在后面的实现中会有完整的例子。

<强> 3.2 PreparedStatement

完成上面的步骤后,我们就可以同过建立的连接创建声明接口类,来执行一些SQL语句了。在下面的例子,我用的是PreparedStatement,还有CallableStatement,它可以执行一些存储过程和函数,这里不多讲了。下面的代码片断是向图片表中插入一条记录。其中(1)处连接接口的对象con通过调用prepareStatement方法得到预编译的SQL语句(预编译的SQL语句);(2)处是为该插入语句的第一个问号赋值,(3)为第二个赋值,(4)为第三个,这步也是最该一提的,用的方法是setBinaryStream(),第一个参数3是指第三个问号,金融中间人是一个二进制文件流,第三个参数是该文件流的长度。

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;   ,/* *   ,*却;能够class  describes 请;用store  picture  file  into  MySQL。   ,* @author  Yanjiang 钱   ,* @version  1.0, 1月- 02 - 2006   ,*/public  class  StorePictures  {   ,,   private 才能;String  dbDriver;   private 才能;String  dbURL;   private 才能;String  dbus;   private 才能;String  dbPassword;   private 才能Connection 诈骗;   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 司机   ,,,,,forname (dbDriver) .newInstance ();   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何在MySQL中存储图片