《JSP程序设计》第六章课后习题答案

1.

a.jsp

<!DOCTYPE html>

<%@ page contentType="text/html;charset=gb2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="Show"%>

<html>

<style>

table

{

border-collapse:collapse;

}

body

{

background-color:#FFE4CA;

font-size:1em;

</style>

<body>

  <Show:GetRecord dataBaseName="warehouse" tableName="product" />

   表<%=biao%>的所有记录: <%--biao是tag文件返回的对象--%>

   <br/> <%=queryResult %>

</body>

</html>

GetRecord.tag

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="dataBaseName" required="true" %>

<%@ attribute name="tableName" required="true" %>

<%@ variable name-given="biao" scope="AT_END" %>

<%@ variable name-given="queryResult" scope="AT_END" %>

<%

    StringBuffer result;    

    result=new StringBuffer();

    try {

        Class.forName("com.mysql.jdbc.Driver");

    }

    catch(Exception e){}

    Connection con;

    Statement sql;

    ResultSet rs;

    try{ 

        result.append("<table border=1>");

        String uri= "jdbc:mysql://127.0.0.1/"+dataBaseName;

        String user="root";

        String password="123";

        con=DriverManager.getConnection(uri,user,password);

        DatabaseMetaData metadata=con.getMetaData();

        ResultSet rs1=metadata.getColumns(null,null,tableName,null);

        int 字段个数=0;

        result.append("<tr>");

        while(rs1.next()){

        字段个数++;

            String clumnName=rs1.getString(4);

            result.append("<td>"+clumnName+"</td>");

        }

        result.append("</tr>");

        sql=con.createStatement();

        rs=sql.executeQuery("SELECT * FROM "+tableName);

        while(rs.next()){

           result.append("<tr>");

            for(int k=1;k<=字段个数;k++) { 

        result.append("<td>"+rs.getString(k)+"</td>");

            }

            result.append("</tr>");

        }

        result.append("</table>");

        con.close();

    }

    catch(SQLException e){ 

        result.append("请输入正确的用户名和密码");

        out.print(e);

    }

    jspContext.setAttribute("queryResult",new String(result));

    jspContext.setAttribute("biao",tableName);

%>

结果截图:

《JSP程序设计》第六章课后习题答案

2.

b.jsp

<%@ page contentType="text/html;charset=gb2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="add" %>

<html>

<style>

body

{

background-color:#FFE4CA;

font-size:1em;

</style>

<body>

<add:AddRecord tableName="product" number="c1002" name="荣耀手机" madeTime="2011-01-01" price="2545.7" />

向<%=biao%>添加的记录是:

<br/><%=newRecord%>

</body>

</html>

reNewRecord.tag

<%@ tag pageEncoding="gb2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="tableName" required="true"%>

<%@ attribute name="number" required="true" %>

<%@ attribute name="name" required="true" %>

<%@ attribute name="madeTime" required="true" %>

<%@ attribute name="price" required="true" %>

<%@ variable name-given="biao" scope="AT_END" %>

<%@ variable name-given="newRecord" scope="AT_END" %>

<%

   double p=Double.parseDouble(price);

   String condition="insert into "+tableName+" values"+"("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")";

   try {

       Class.forName("com.mysql.jdbc.Driver");

   }

   catch(Exception e){}

   Connection con;

   Statement sql;

   ResultSet rs;

   try{

        String uri= "jdbc:mysql://127.0.0.1/warehouse";

        String user="root";

        String password="123";

        con=DriverManager.getConnection(uri,user,password);

        sql=con.createStatement();

        sql.executeUpdate(condition);

        con.close();

        String str="("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")";

        jspContext.setAttribute("newRecord",str);

   }

   catch(Exception e){

      jspContext.setAttribute("newRecord",""+e);

   }

   jspContext.setAttribute("biao",tableName);

%>

结果截图:

《JSP程序设计》第六章课后习题答案

3.

c.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="reNew"%>

<html>

<body bgcolor=cyan>

<font size=4>

<reNew:RenewRecord tableName="product" number="c1001" name="华为手机" madeTime="2008-10-10" price="2379.9"/>

表<%=biao%>更新后的记录是:

<br/> <%=reNewRecord %>

</font>

</body>

</html>

RenewRecord.tag

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="tableName" required="true"%>

<%@ attribute name="number" required="true" %>

<%@ attribute name="name" required="true" %>

<%@ attribute name="madeTime" required="true" %>

<%@ attribute name="price" required="true" %>

<%@ variable name-given="biao" scope="AT_END" %>

<%@ variable name-given="reNewRecord" scope="AT_END" %>

<% 

    float p=Float.parseFloat(price);

    String condition1="UPDATE product SET name= '"+name+"' WHERE number="+"'"+number+"'" ,

           condition2="UPDATE product SET madeTime= '"+madeTime+"' WHERE number="+"'"+number+"'",

           condition3="UPDATE product SET price= "+price+" WHERE number="+"'"+number+"'" ;

    try{  

         Class.forName("com.mysql.jdbc.Driver");

    }

    catch(Exception e) {}

    Connection con;

    Statement sql;

    ResultSet rs;

    try{

         String uri= "jdbc:mysql://127.0.0.1/Warehouse";

         con=DriverManager.getConnection(uri,"root","123");

         sql=con.createStatement();

         sql.executeUpdate(condition1);

         sql.executeUpdate(condition2);

         sql.executeUpdate(condition3);

         con.close();

         String str=("("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")");

         jspContext.setAttribute("reNewRecord",str);

       }

    catch(Exception e)

      {  

        jspContext.setAttribute("reNewRecord",""+e);

      }

    jspContext.setAttribute("biao",tableName);

%>

结果截图:

《JSP程序设计》第六章课后习题答案

d.jsp

<%@ page contentType="text/html;charset=GB2312" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="del"%>

<html>

<body bgcolor=cyan>

<font size=2>

<del:DelRecord tableName="product" number="c1002" />

表<%=biao%>删除的记录的键字段的值是:

<br/> <%=deletedRecord %>

</font>

</body>

</html>

DelRecord.tag

<%@ tag pageEncoding="GB2312" %>

<%@ tag import="java.sql.*" %>

<%@ attribute name="tableName" required="true"%>

<%@ attribute name="number" required="true" %>

<%@ variable name-given="biao" scope="AT_END" %>

<%@ variable name-given="deletedRecord" scope="AT_END" %>

<% 

    String condition="DELETE FROM product WHERE number = '"+number+"'";

    try{  

         Class.forName("com.mysql.jdbc.Driver");

    }

    catch(Exception e) {}

    Connection con;

    Statement sql;

    ResultSet rs;

    try{

         String uri= "jdbc:mysql://127.0.0.1/warehouse";

         con=DriverManager.getConnection(uri,"root","123");

         sql=con.createStatement();

         sql.executeUpdate(condition);

         con.close();

         jspContext.setAttribute("deletedRecord",number);

    }

    catch(Exception e){  

        jspContext.setAttribute("deletedRecord",""+e);

    }

    jspContext.setAttribute("biao",tableName);

%>

结果截图:

《JSP程序设计》第六章课后习题答案