一、项目文件目录结构图示
二、运行效果显示
三、代码示例
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);
}
%>