使用jsf在mysql数据库中添加多个字段的问题

时间:2016-07-25 08:52:48

标签: mysql jsf

我有一个问题,你可以帮帮我吗?我尝试使用jsf将用户名,名字,姓氏,电子邮件,密码,图像和插入日期以及时间插入到mysql数据库中。首先,我用2个字段做了 - 用户名和密码 - 一切正常,但是当我添加其他字段时 - 方法insertData()停止工作,当我点击注册按钮时发生的事情...... 请帮帮我!

1.signUp.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<f:view locale="en">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"      
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Sign Up</title>
<link href="css/style.css" rel="stylesheet" style="text/css" media="screen"/>
</head>
<body>
  <h:form id="signupForm" enctype="multipart/form-data">
  <h2>Sign Up</h2>
    <p><h:message for="password" style="color: red"></h:message></p>
    <h:messages showDetail="true"/>
    <h:panelGroup>
     <table border="0">
     <tr>
        <td><h:outputLabel for="userName" value="Username"></h:outputLabel></td>
        <td><h:inputText id="userName" value="#{manager_Bean.userName}" required="true" size="24"></h:inputText></td>
        <td><h:message for="userName" style="color: red"></h:message></td>
      </tr>
      <tr>
        <td><h:outputLabel value="First name"></h:outputLabel></td>
        <td><h:inputText id="firstName" value="#{manager_Bean.firstName}" required="true" size="24"></h:inputText></td>
     </tr>
     <tr>
       <td><h:outputLabel value="Last name"></h:outputLabel></td>
       <td><h:inputText id="lastName" value="#{manager_Bean.lastName}" required="true" size="24"></h:inputText></td>
     </tr>
     <tr>
       <td><h:outputLabel value="Email"></h:outputLabel></td>
       <td><h:inputText id="email" value="#{manager_Bean.email}" required="true" size="24"></h:inputText></td>
     </tr>
      <tr>
        <td><h:outputLabel for="password" value="Enter password"></h:outputLabel></td>
        <td><h:inputSecret id="password" value="#{manager_Bean.password}" required="true" size="25">
            <f:validator validatorId="passwordValidator"/>
            <f:attribute name="confirm" value="#{confirm}"/>
        </h:inputSecret></td>
        <td><h:message for="password" style="color: red"></h:message></td>
      </tr>
      <tr>
         <td><h:outputLabel for="confirm" value="Confirm password"></h:outputLabel></td>
         <td><h:inputSecret id="confirm" binding="#{confirm}" required="true" size="25"></h:inputSecret></td>
         <td><h:message for="confirm" style="color: red"></h:message></td>
      </tr>
      <tr>
       <td>Portrait Photo: </td>
       <td><input type="file" name="image" size="50"/></td>
      </tr>
       <tr>
        <td><h:commandButton id="button" value="Sign Up" action="#{manager_Bean.insertData}"></h:commandButton></td>
        <td><h:message for="button" style="color: green"></h:message></td>
       </tr>
      </table>
    </h:panelGroup>
  </h:form>
</body>
</html>
</f:view>

 2.bean file(must insert data into db)

@SuppressWarnings("serial")
@ManagedBean(name="manager_Bean", eager=true)
@SessionScoped
@MultipartConfig(maxFileSize= 5242880)
public class managerBean implements Serializable{
  static int c= 0;
  private int id;
  private String userName;
  private String firstName;
  private String lastName;
  private String email;
  private String password;
  private String message;
  private String databaseUserName;
  private String databasePassword;
  private UploadedFile image;
  private Date createdDate;

  Connection connection= null;
  Statement statement= null;
  ResultSet resultSet;
  String SQL;
     public int getId(){
         return id;
     }
     public void setId(int id){
         this.id= id; 
     }
     public String getUserName(){
         return userName;
     }
     public void setUserName(String userName){
         this.userName= userName;
     }
     public String getFirstName(){
         return firstName;
     }
     public void setFirstName(String firstName){
         this.firstName= firstName;
     }
     public String getLastName(){
         return lastName;
     }
     public void setLastName(String lastName){
         this.lastName= lastName;
     }
     public String getEmail(){
        return email;            
     }
     public void setEmail(String email){
         this.email= email;
     }
     public String getPassword(){
         return password;
     }
     public void setPassword(String password){
         this.password= password;
     }
     public String getMessage() {
        return message;
     }
     public void setMessage(String message) {
        this.message= message;
     }
     public String getDatabaseUserName(){
         return databaseUserName;
     }
     public void setDatabaseUserName(String databaseUserName){
         this.databaseUserName= databaseUserName;
     }
     public String getDatabasePassword(){
         return databasePassword;
     }
     public void setDatabasePassword(String databasePassword){
         this.databasePassword= databasePassword;
     }
     public UploadedFile getImage(){
         return image;
     }
     public void setImage(UploadedFile image){
         this.image= image;
     }
     public Date getCreatedDate(){
         return createdDate;
     }
     public void setCreatedDate(Date createdDate){
         this.createdDate= createdDate;
     }

     public void insertUser(){
            System.out.println("We are in insertUser()!");
            System.out.println("image= "+image);

            //if(image!= null){
            try {   
                Connection connection= dataConnect.getConnection();
                //execute a query
                System.out.println("Inserting records to the table...");
                 statement= connection.createStatement();
                 SQL= "INSERT INTO Registration     (userName,firstName,lastName,email,password,file,createdDate) VALUES (?,?,?,?,?,?,?)";
                 PreparedStatement prepStatement=    connection.prepareStatement(SQL);
                 prepStatement.setString(1, userName);
                 prepStatement.setString(2, firstName);
                 prepStatement.setString(3, lastName);
                 prepStatement.setString(4, email);
                 prepStatement.setString(5, password);
                 System.out.println(image.getFileName());
                 InputStream inStream= image.getInputstream();
                 prepStatement.setBinaryStream(6, inStream, image.getSize());
                 SimpleDateFormat dateFormat= new SimpleDateFormat(DaTime);
                  if(createdDate!= null){
                       String date= dateFormat.format(createdDate);
                       Object object= date;
                        if(object== null){
                            prepStatement.setDate(7, null);
                        }else{
                            Date datE= Date.valueOf(new String(date));
                            prepStatement.setDate(7, datE);
                        }
                   }
                 c= prepStatement.executeUpdate();
                   System.out.println("User added Successfully!");
                   FacesMessage facesMessage= new   FacesMessage("Successfully",image.getFileName()+"is uploaded!"); 
                   FacesContext.getCurrentInstance().addMessage("welcome",facesMessage);
                 statement.close();
                 connectionClose(connection);

            } catch (SQLException eX) {
                // TODO Auto-generated catch block
                eX.printStackTrace();
            }catch (IOException ioEx) {
                // TODO: handle exception
                ioEx.printStackTrace();
            }
            //end of if block}
      }
           public String insertData(){
               insertUser();
                System.out.println("We are in insertData()!");
                System.out.println("c= "+c);
                if(c> 0){
                    return "register";
                }else {
                    return "error";
                }
           }

   3. web.xml                                                                 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"                   xmlns="http://xmlns.jcp.org/xml/ns/javaee"    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"    version="3.1">
         <display-name>webProject</display-name>
         <welcome-file-list>
        <welcome-file>home.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
      </servlet-mapping>
      <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
      </context-param>
      <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
      </context-param>
      <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
      </listener>
      <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
      </servlet-mapping>
    </web-app>

0 个答案:

没有答案