例子9:删除记录——删除数据库的表中的数据(JSP)

JSP 专栏收录该内容
11 篇文章 0 订阅

一、项目文件目录结构图示

二、运行效果显示

 

 

三、代码示例

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <BODY ><FONT size=2>
<FORM action="delete.jsp" method=post>
删除记录:<BR>输入被删除的记录的产品号:
<Input type="text" name="number" size=8>
<BR><Input type="submit" name="b" value="提交">
</FORM>
<BR>product表删除记录前的记录是:
 <inquire:QueryTag dataBaseName="warehouse"
                   tableName="product" 
                   user="root" password=""/> 
<BR> <%=queryResult %>
</Font></BODY>
</html>

delete.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'delete.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <BODY ><Font size=2>
<% String num=request.getParameter("number");
   if(num==null)
       num ="";
   byte [] bb=num.getBytes("iso-8859-1");
   num = new String(bb);
%>
 <inquire:DelRecord number="<%=num%>"/> 
 <BR>product表删除记录后的记录是:
 <inquire:QueryTag  dataBaseName="warehouse"
                    tableName="product" 
                    user="root" password=""/> 
<BR> <%=queryResult %> <%--queryResult是QueryTag.tag返回的对象--%>
</Font></BODY>
</html>

QueryTag.tag

<%@ tag pageEncoding="GB2312" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="dataBaseName" required="true" %>
<%@ attribute name="tableName" required="true" %> 
<%@ attribute name="user" required="true" %>  
<%@ attribute name="password" required="false" %>  
<%@ 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;
          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("请输入正确的用户名和密码");
}
//返回对象queryResult: 
    jspContext.setAttribute("queryResult",new String(result)); 
    jspContext.setAttribute("biao",tableName);          //返回biao对象
%> 

DeleteRecord.tag

<%@ tag pageEncoding="GB2312" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="number" required="true" %>
<%  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?"+
         "user=root&password=&characterEncoding=gb2312";
         con=DriverManager.getConnection(uri);
         con=DriverManager.getConnection(uri,"root","");
         sql=con.createStatement();
         sql.executeUpdate(condition);
         con.close();
    }
    catch(Exception e){
        out.print(""+e);
    }
%>

 

  • 0
    点赞
  • 0
    评论
  • 6
    收藏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:终极编程指南 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值