struts连接数据库:用Struts向MYSQL数据库中储存图片例子

  这个例子是通过用StrutsFormFile来写入到MySQL中用户通过选个图片然后按submit就可以存入数据库中其中先要建立个表:

create table test
( name varchar(20),
pic blob );
在MySQLtest库中
<%@ page language="java"%>
<%@ taglib uri=
"http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri=
"http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<html>
<head>
 <title>JSP for multiForm form</title>
</head>
<body>
 <html:form action="/multi"
 enctype="multipart/form-data">
 定要用enctype=“multipart/form-data“
 不然就提交的后就会有抛出异常
  file : <html:file property="file"/>
  <html:errors property="file"/></br>
  name : <html:text property="name"/>
  <html:errors property="name"/></br>
  <html:submit/><html:cancel/>
 </html:form>
</body>
</html>
  2. 相对应ActionForm:

//Created by MyEclipse Struts
// XSL source (default):
platform:/plugin/com.genuitec.eclipse.
cross.easystruts.eclipse_3.8.1
/xslt/JavaClass.xsl
package saoo.struts.form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/**
* MyEclipse Struts
* Creation date: 08-24-2004
*
* XDoclet definition:
* @struts:form name="multiForm"
*/
public MultiForm extends ActionForm
{
  // ----------------
Instance Variables
  /** file property */
  private FormFile file;
  /** name property */
  private String name;
  // -----------------
Methods
  /**
   * Returns the file.
   * @ FormFile
   */
  public FormFile getFile
  {
     file;
  }
  /**
   * Set the file.
   * @param file The file to
   */
  public void File(FormFile file)
  {
    this.file = file;
  }
  /**
   * Returns the name.
   * @ String
   */
  public String getName
  {
     name;
  }
  /**
   * Set the name.
   * @param name The name to
   */
  public void Name(String name)
  {
    this.name = name;
  }
}
  3. 对就Action:

//Created by MyEclipse Struts
// XSL source (default):
platform:/plugin/com.genuitec.eclipse.
cross.easystruts.eclipse_3.8.1
/xslt/JavaClass.xsl
package saoo.struts.action;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import saoo.struts.form.MultiForm;
/**
* MyEclipse Struts
* Creation date: 08-24-2004
*
* XDoclet definition:
* @struts:action path="/multi"
name="multiForm" input="/form/multi.jsp"
scope="request"
*/
public MultiAction extends Action
{
  // ---------------
Instance Variables
  // ---------------
Methods
  /**
   * Method execute
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @ ActionForward
   */
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    {
    MultiForm multiForm = (MultiForm)
    form;
    FormFile file = multiForm.getFile;
    String name = multiForm.getName;
    try {
      Class.forName
      ("org.gjt.mm.mysql.Driver");
      String url="jdbc:mysql:
      ///test";
      Connection con=DriverManager.getConnection
      (url,"root","password");
      String sql="insert o pic values (?,?)";
      PreparedStatement ps
      =con.prepareStatement(sql);
      ps.String(1, name);
//加入图片到数据库
  
ps.BinaryStream
(2,file.getInputStream,
file.getFileSize);
      ps.executeUpdate;
      ps.close;
      con.close;
    } catch (SQLException se)
    {
      se.prStackTrace;
       mapping.findForward("error");
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.prStackTrace;
       mapping.findForward("error");
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.prStackTrace;
       mapping.findForward("error");
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.prStackTrace;
       mapping.findForward("error");
    }
     mapping.findForward("success");
  }
}


Tags:  mysql数据库 strutsmysql struts数据库 struts连接数据库

延伸阅读

最新评论

发表评论