数据库不通过JSF更新

时间:2016-04-08 16:21:06

标签: java mysql jsf jsf-2

我正在使用JSF来更新数据库但是当我按下按钮后检查数据库时,信息就不存在了。我连接不正确吗?另外,我有4个按钮,1个用于更新数据库,1个用于查看,1个用于插入,1个用于清除表单。这些方法应该在信息bean中还是应该都有自己的bean?谢谢你们?

的index.html

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"

 <h:head>
    <title>Staff Information</title>
</h:head>
<h:body>
    <font size="10"> Staff Information </font>
    <h:form>

        <div style="height:30px">
            <h:outputLabel value="ID:  "/>
            <h:inputText id="ID"
                         value="#{information.ID}"/>
        </div>

        <div style="height:30px">
            <h:outputLabel value="Last Name  " />
            <h:inputText id="lastName"
                         value="#{information.lastName}"/>

            <h:outputLabel value="    First Name  " />
            <h:inputText id="firstName"
                         value="#{information.firstName}"/>

            <h:outputLabel value=" MI  "/>
            <h:inputText id="middle"
                         value="#{information.middle}"/>
        </div>

        <div style="height:30px">
            <h:outputLabel value="Address  "/>
            <h:inputText id="address"
                         value="#{information.address}"/>
        </div>

        <div style="height:30px">
            <h:outputLabel value="City   "/>
            <h:inputText id="city" 
                         value="#{information.city}"/>

            <h:outputLabel value="State  "/>
            <h:inputText id="state" 
                         value="#{information.state}"/>
        </div>

        <div style="height:30px">
            <h:outputLabel value="Telephone # "/>
            <h:inputText id="phone"
                         value="#{information.phone}"/>
        </div>

        <h:commandButton value="View"/> 
        <h:outputText value="&#160;" />

        <h:commandButton value="Insert"/>
        <h:outputText value="&#160;" />

        <h:commandButton value="Update" action="#{information.add()}"/>

        <h:outputText value="&#160;" />

        <h:commandButton type="reset" value="Clear" />
        <h:outputText value="&#160;" />

        </h:form>
    </h:body>
</html>

信息bean

import java.sql.*;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named(value = "information")
@RequestScoped
public class informationJSFBean {
private Integer id;
private String lastName;
private String firstName;
private String middle;
private String address;
private String city;
private String state;
private Long phone;

public informationJSFBean() {
}

public void add(){


    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection connect = DriverManager.getConnection("jdbc:mysql://local
host:3306/Staff","root","password")

                 String sql = "INSERT INTO Staff(id, lastName, firstName,"
                         + "middle, address, city, state, phone) "
                         + "VALUES(?,?,?,?,?,?,?,?)";
                PreparedStatement ps = connect.prepareStatement(sql);
                 ps.setLong(1,id);
                 ps.setString(2,lastName);
                 ps.setString(3, firstName);
                 ps.setString(4, middle);
                 ps.setString(5, address);
                 ps.setString(6, city);
                 ps.setString(7, state);
                 ps.setLong(8, phone);

                 int i = ps.executeUpdate(); 
                 System.out.println("Data Added Successfully"); 
    }
    catch (ClassNotFoundException | SQLException e)
    {
        System.out.println(e);
    }
}




public Integer getID(){
    return id;
}
public void setID(Integer id){
    this.id = id;
}
public String getLastName(){
    return lastName;
}
public void setLastName(String lastName){
    this.lastName = lastName;
}
public String getFirstName(){
    return firstName;
}
public void setFirstName(String firstName){
    this.firstName = firstName;
}
public String getMiddle(){
    return middle;
}
public void setMiddle(String middle){
    this.middle = middle;
}
public String getAddress(){
    return address;
}
public void setAddress(String address){
    this.address = address;
}
public String getCity(){
    return city;
}
public void setCity(String city){
    this.city = city;
}
public String getState(){
    return state;
}
public void setState(String state){
    this.state = state;
}
public Long getPhone(){
    return phone;
}
public void setPhone(Long phone){
    this.phone = phone;
}
public String getResponse(){
    if (lastName == null){
        return "";
}
else{
        return "Thank You, " + firstName + " "
            +lastName + "."+" Your information has been saved. ";
    }
}

}

查看课程

public void view(){
    try{
     Class.forName("com.mysql.jdbc.Driver");
     System.out.println("Driver loaded");

     Connection connect = DriverManager.getConnection("jdbc:mysql://"
             + "localhost:3306/Staff");
     System.out.println("Database connected");

     String sql = "SELECT * FROM Staff WHERE id="+id;

     System.out.println(sql);

    }
   catch (ClassNotFoundException | SQLException e)
    {
        System.out.println(e);
    }
}

用于视图类的HTML

<h:commandButton value="View"> 
            <f:param name="action" value="#{information.view()}" />
        </h:commandButton>

0 个答案:

没有答案