使用受管bean对数据库中数据进行增删改查操作

Student.java

package chen;

public class Student {
    private String xm;
    private String xb;
    private String ad;
public Student(String xm, String xb, String ad)
{this.xm=xm;
this.xb=xb;
this.ad=ad;
    }
    public String getXm() {
        return xm;
    }
    public void setXm(String xm) {
        this.xm = xm;
    }
    public String getXb() {
        return xb;
    }
    public void setXb(String xb) {
        this.xb = xb;
    }
    public String getAd() {
        return ad;
    }
    public void setAd(String ad) {
        this.ad = ad;
    }
}

编写受管bean Bean.java

package chen;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.jsp.jstl.sql.Result;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped; 

@ManagedBean(name = "bean")
@SessionScoped
public class Bean implements Serializable {
    private String xm;
    private String xb;
    private String ad;
    private ArrayList df;


    public ArrayList getDf() {
        return df;
    }

    public void setDf(ArrayList df) {
        this.df = df;
    }

    public String getXm() {
        return xm;
    }

    public void setXm(String xm) {
        this.xm = xm;
    }

    public String getXb() {
        return xb;
    }

    public void setXb(String xb) {
        this.xb = xb;
    }

    public String getAd() {
        return ad;
    }

    public void setAd(String ad) {
        this.ad = ad;
    }


    public Bean()
    {
        select();
    }

    private Connection getConnection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(
                    "jdbc:sqlserver://C215001:1433; databaseName=dskgo", "sa",
                    "123456");
            return con;
        } catch (Exception e) {
            return null;
        }
    }
        //通过select()方法得到对象的参数值添加学生到数据库中
    public String insert() {
            try {
                Connection con = getConnection();
                Statement statement = con.createStatement();
                statement.execute("insert into student values('" + xm + "','" + xb
                        + "','" + ad + "')");
                select();
                return "index";
                //得到学生的参数值返回到主页index.xhtml
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return "error";
            }
    }
        //得到学生的参数值
    public void select() {
        try {
            Connection con = getConnection();
            PreparedStatement statement = con
                    .prepareStatement("select * from student");
            ResultSet sdf = statement.executeQuery();
            df = new ArrayList();
            while (sdf.next()) {
                df.add(new Student(sdf.getString("xm"), sdf.getString("xb"),
                        sdf.getString("ad")));

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static String getFacesParamValue(String name) 
    {   
        return (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(name);
    }
        //通过学生姓名删除学生
    public String delete()
    {
        String strXm = getFacesParamValue("xm");

        try {
            Connection con = getConnection();
            Statement statement = con.createStatement();
            statement.execute("delete from student where xm='" + strXm + "'");
            select();
            return "index";
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "error";
        }
    }
        //通过学生姓名操作学生信息
    public String edit()
    {
        String strXm = getFacesParamValue("xm");

        try {
            Connection con = getConnection();
            PreparedStatement statement = con
                    .prepareStatement("select * from student where xm='" + strXm + "'");
            ResultSet sdf = statement.executeQuery();
            if (sdf.next()) {
                xm = sdf.getString("xm");
                xb = sdf.getString("xb");
                ad = sdf.getString("ad");
                return "edit";
            }
            else
                return "error";

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "error";
        }
    }

        //修改学生信息
    public String update()
    {

        try {
            Connection con = getConnection();
            Statement statement = con.createStatement();
            statement.execute("update student set xb='" + xb
                    + "',ad='" + ad + "' where xm='" + xm + "'");
            select();
            return "index";
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "error";
        }
    }


}

index.jsp 创建一个提供显示学生信息和注册学生的页面链接的主页

head>
  <body>
       <a href="index.xhtml">显示所有用户信息a>
         
       <a href="reg.xhtml">注册a>
  body>
html>

这里写图片描述


index.xhtml(显示所有学生信息,提供编辑和删除操作选项)




<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
    xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions" lang="en">
<h:head>
    <title>JSF 2.2 Pagetitle>
    <meta name="keywords" content="enter,your,keywords,here" />
    <meta name="description" content="A short description of this page." />
    <meta name="content-type" content="text/html; charset=UTF-8" />

    
h:head>
<h:body>
    <h:form>
    <h:dataTable value="#{bean.df}" var="student">
        <h:column>
            <f:facet name="header">namef:facet>
        #{student.xm}h:column>
        <h:column>
            <f:facet name="header">xyf:facet>#{student.xb}h:column>
        <h:column>
            <f:facet name="header">addressf:facet>#{student.ad}h:column>
        <h:column>
            <f:facet name="header">操作f:facet>
            <h:commandLink action="#{bean.edit}" value="编辑" style="margin-right:10px;" >
                <f:param name="xm" value="#{student.xm}" />
            h:commandLink>
            <h:commandLink action="#{bean.delete}" value="删除" >
                <f:param name="xm" value="#{student.xm}" />
            h:commandLink>
        h:column>

    h:dataTable>
    h:form>
h:body>
html>

这里写图片描述


注册页面 reg.xhtml

    ........
    h:head>
    <h:body>
        <h:form>
        姓名:<h:inputText required="true" name="xm" value="#{bean.xm}" >h:inputText><br>br>
        性别:<h:inputText required="true" name="xb" value="#{bean.xb}">h:inputText><br>br>
        地址:<h:inputText required="true" name="ad" value="#{bean.ad}">h:inputText><br>br>
        <h:commandButton type="submit" value="提交" action="#{bean.insert}" >h:commandButton>

        h:form>
    h:body>
html>

使用受管bean对数据库中数据进行增删改查操作_第1张图片


修改学生信息页面 update.xhtml

    ......
 h:head>
    <h:body>
        <h:form>
        姓名:<h:inputText required="true" name="xm" value="#{bean.xm}" readonly="true">h:inputText><br>br>
        性别:<h:inputText required="true" name="xb" value="#{bean.xb}">h:inputText><br>br>
        地址:<h:inputText required="true" name="ad" value="#{bean.ad}">h:inputText><br>br>
        <h:commandButton type="submit" value="提交" action="#{bean.update}" >h:commandButton>

        h:form>
    h:body>
html>

使用受管bean对数据库中数据进行增删改查操作_第2张图片


错误页面 error.xhtml

    ......
 h:head>
    <h:body>
        <p>
            <h1>errorh1>
        p>
    h:body>
html>

你可能感兴趣的:(JSF)