1.
a.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="Show"%>
表<%=biao%>的所有记录: <%--biao是tag文件返回的对象--%>
<%=queryResult %>
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("");
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("");
while(rs1.next()){
字段个数++;
String clumnName=rs1.getString(4);
result.append(""+clumnName+" ");
}
result.append(" ");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM "+tableName);
while(rs.next()){
result.append("");
for(int k=1;k<=字段个数;k++) {
result.append(""+rs.getString(k)+" ");
}
result.append(" ");
}
result.append("
");
con.close();
}
catch(SQLException e){
result.append("请输入正确的用户名和密码");
out.print(e);
}
jspContext.setAttribute("queryResult",new String(result));
jspContext.setAttribute("biao",tableName);
%>
结果截图:
2.
b.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="add" %>
向<%=biao%>添加的记录是:
<%=newRecord%>
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);
%>
结果截图:
3.
c.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="reNew"%>
表<%=biao%>更新后的记录是:
<%=reNewRecord %>
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);
%>
结果截图:
d.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="del"%>
表<%=biao%>删除的记录的键字段的值是:
<%=deletedRecord %>
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);
%>
结果截图: