准备:Dreamweaver8,tomcate,
在tomcte的webapps目录下建立一个文件夹,以你将来站点的名称来命名该文件夹。并在该文件夹下命名一个“WEB-INT”文件夹,并在该文件夹下创建“lib”文件夹,
在“lib”文件夹下导入与mysql连接所需的jre包。说起来繁琐,其实就是建立几个文件夹而已。
注意下面文件路径:
准备工作做完之后,就打开所需要的工具Dreamweaver,当然在这之前,你必须把tomcate启动。
- 在Dreamweaver菜单栏里,点击创建站点。(这里笔者所用为Dreamweaver8)
站点的名称任意,只是起到标示作用而已。站点HTTP最好是你将来预览该站点的路径,当然也可以不填。
2.技术我们用一个简单的JSP技术作为示范,之后就是定义测试服务器了。一般都是选择第二个。但是因人而异。
文件储存位置就是刚刚我们一开始所建立的目录。
3.测试服务器的选择,如果你有ftp账号和密码,也可以直接建站在虚拟目录下,这里演示在本地测试服务器。
4.最后测试下URl就行了,注意这里路径一定要填写正确。
这样就完成了对站点的建立。之后我们需要在站点目录下建立一个页面。
回顾一下,这里我们要学习登录页面和简单的增删改查。所以数据库要建立相应的表单,这就不表了,若不明白如何建立表单,我之前写的JAVA基础篇里面有关于
mysql的建表。
这里我建了三个表,但是由于时间有限,我只讲两个表就行了,另一个同理,都是关于增删改查地简单应用。
这里登录所用地表为 t_admin,赛事信息的表为 t_informaton,t_player为选手的信息表,这里不表。
sql表建立完成之后,我问先建立一个index.jsp页面。
大家原谅,这里不再讲述关于jsp设计方面的东西了,这样这篇文章就太冗长,这里只是介绍一下Dreamweaver的记录集用法。因为记录集是一个强大但是
现在很少人去用的东西。虽然现在有很多技术取代了这个。但是了解一下还是很不错的。最后我会把代码,粘下来。
右侧栏目,点击“应用程序”——“数据库”——加号。
这里是一个与mysql连接的驱动程序,也是我们一开始导的jre。需要我们填写的就是连接名,URl。URL中间不填写默认就是//localhost:8080/
所以我们在最后面写上我们的数据库名即可。
插入记录集:应用程序——服务器行为——加号——用户身份验证——登录
如果一开始没有创建记录集会先创建记录集,之后再是登录,这里没什么需要注意的,表格选到我们的的t_admin即可。
登录用户这里唯一注意的就是,用户名字段和密码字段。因为我们t_admin有三个列,id,user,password
我们登录只需要用id , password即可,所以登录框与数据库之间一一对应,否则就会出现错误。
这样,登录页面就完成了,下面是登录页面的代码。由于刚刚接触jsp,页面并不是很规范,请见谅;
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <%@ include file="Connections/ACM.jsp" %> <% String Recordset6__MMColParam = "1"; if (request.getParameter("id") !=null) {Recordset6__MMColParam = (String)request.getParameter("id");} %> <% Driver DriverRecordset6 = (Driver)Class.forName(MM_ACM_DRIVER).newInstance(); Connection ConnRecordset6 = DriverManager.getConnection(MM_ACM_STRING,MM_ACM_USERNAME,MM_ACM_PASSWORD); PreparedStatement StatementRecordset6 = ConnRecordset6.prepareStatement("SELECT * FROM db_Manager.t_admin WHERE id = " + Recordset6__MMColParam + ""); ResultSet Recordset6 = StatementRecordset6.executeQuery(); boolean Recordset6_isEmpty = !Recordset6.next(); boolean Recordset6_hasData = !Recordset6_isEmpty; Object Recordset6_data; int Recordset6_numRows = 0; %> <% // *** Validate request to log in to this site. String MM_LoginAction = request.getRequestURI(); if (request.getQueryString() != null && request.getQueryString().length() > 0) { String queryString = request.getQueryString(); String tempStr = ""; for (int i=0; i < queryString.length(); i++) { if (queryString.charAt(i) == '<') tempStr = tempStr + "<"; else if (queryString.charAt(i) == '>') tempStr = tempStr + ">"; else if (queryString.charAt(i) == '"') tempStr = tempStr + """; else tempStr = tempStr + queryString.charAt(i); } MM_LoginAction += "?" + tempStr; } String MM_valUsername=request.getParameter("user"); if (MM_valUsername != null) { String MM_fldUserAuthorization=""; String MM_redirectLoginSuccess="findview.jsp"; String MM_redirectLoginFailed="logfalse.jsp"; String MM_redirectLogin=MM_redirectLoginFailed; Driver MM_driverUser = (Driver)Class.forName(MM_ACM_DRIVER).newInstance(); Connection MM_connUser = DriverManager.getConnection(MM_ACM_STRING,MM_ACM_USERNAME,MM_ACM_PASSWORD); String MM_pSQL = "SELECT user, password"; if (!MM_fldUserAuthorization.equals("")) MM_pSQL += "," + MM_fldUserAuthorization; MM_pSQL += " FROM db_Manager.t_admin WHERE user=\'" + MM_valUsername.replace('\'', ' ') + "\' AND password=\'" + request.getParameter("pwd").toString().replace('\'', ' ') + "\'"; PreparedStatement MM_statementUser = MM_connUser.prepareStatement(MM_pSQL); ResultSet MM_rsUser = MM_statementUser.executeQuery(); boolean MM_rsUser_isNotEmpty = MM_rsUser.next(); if (MM_rsUser_isNotEmpty) { // username and password match - this is a valid user session.putValue("MM_Username", MM_valUsername); if (!MM_fldUserAuthorization.equals("")) { session.putValue("MM_UserAuthorization", MM_rsUser.getString(MM_fldUserAuthorization).trim()); } else { session.putValue("MM_UserAuthorization", ""); } if ((request.getParameter("accessdenied") != null) && false) { MM_redirectLoginSuccess = request.getParameter("accessdenied"); } MM_redirectLogin=MM_redirectLoginSuccess; } MM_rsUser.close(); MM_connUser.close(); response.sendRedirect(response.encodeRedirectURL(MM_redirectLogin)); return; } %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>管理员登录</title> <style> h1 { background-color:#CC0099;width:200px;left:400px} #shubiao{background-image:url(imager/u=2326365784,3114316492&fm=23&gp=0.jpg); width:93px; height:78px; left:9px; position:absolute; top: 102px;} #shubiao:hover{background-image:url(imager/ac75323d6b6de243-959a0261b4ff28a1-2d23ac9558b16dfc8873a05f17941cb6.jpg);} #main{border:5px solid #3300FF;background-color: #99FFCC;position: absolute; width:400px; height:250px; left:258px;top:97px;} </style> </head> <body> <div id = "main"> <h align="center"> </h> <h1 align="center">管理员登录</h1> <form id="form1" name="form1" method="POST" action="<%=MM_LoginAction%>"> <div id ="shubiao"></div> <p align="center"> <strong>用户名</strong> <input name="user" type="text" /> </p> <p align="center"> <strong>密 码 </strong> <input name="pwd" type="password" /> </p> <label> <div align="center"> 记住密码 //这里是关于记住密码的功能,文章并没有叙述,所以这里酌情增删。 <input <%=(((((Recordset6_data = Recordset6.getObject("password"))==null || Recordset6.wasNull())?"":Recordset6_data).toString().equals(("1").toString()))?"checked=\"checked\"":"")%> type="checkbox" name="checkbox" value="checkbox" /> <label></label> </div> </label> <div align="center"> <input name="submit" type="submit"value ="登录" /> </div> </form> </div> </body> </html> <% Recordset6.close(); StatementRecordset6.close(); ConnRecordset6.close(); %>
登录页面告一段落。
下面是一个比赛信息的增删改查。
、首先是一个view页面:(请忽视渣布局....)
在表格上方写了一个链接,是添加比赛信息(增),操作1(删),操作2(改),本页面就是查了。
先说一下这个view页面的创建吧。
同样,先插入一个记录集:选定t_information表格。这里不能选择以id筛选,否则不会有任何显示。
动态表格:记录集名称要与上面记录集名称一致,由于项目我已经做好,不能再添加,所以另建一个页面作说明,
难免会有一些初入,请各位注意说明。
动态表格添加之后,就是多添加两个,写删除和修改两个操作。
可是这两个操作如何超链接呢?
<td><div align="center"><a href="del_information.jsp?id=<%=(((Recordset4_data = Recordset4.getObject("id"))==null || Recordset4.wasNull())?"":Recordset4_data)%>">取消比赛</a></div></td> <td><div align="center"><a href="update_information.jsp?id=<%=(((Recordset4_data = Recordset4.getObject("id"))==null || Recordset4.wasNull())?"":Recordset4_data)%>">修改比赛</a></div></td>
这里并不能直接复制,这里id是与你自己建立动态表格id栏目里的值一致即可。
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <%@ include file="Connections/ACM.jsp" %> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); Driver DriverRecordset4 = (Driver)Class.forName(MM_ACM_DRIVER).newInstance(); Connection ConnRecordset4 = DriverManager.getConnection(MM_ACM_STRING,MM_ACM_USERNAME,MM_ACM_PASSWORD); PreparedStatement StatementRecordset4 = ConnRecordset4.prepareStatement("SELECT * FROM db_Manager.t_information"); ResultSet Recordset4 = StatementRecordset4.executeQuery(); boolean Recordset4_isEmpty = !Recordset4.next(); boolean Recordset4_hasData = !Recordset4_isEmpty; Object Recordset4_data; int Recordset4_numRows = 0; %> <% int Repeat1__numRows = 10; int Repeat1__index = 0; Recordset4_numRows += Repeat1__numRows; %> <% int Repeat2__numRows = 10; int Repeat2__index = 0; Recordset4_numRows += Repeat2__numRows; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>赛事信息</title> <style type="text/css"> <!-- --> a.one{ color:#000099; text-decoration: underline blink; font-size: medium; } .STYLE4 {font-size: xx-large} .STYLE5 {color: #F0F0F0} </style> </head> <body> <div style="background-color:#FFCC99;position:relative;top:auto"> <div style="background-color:#FFCC99;position:static;height:50px"> <p class="STYLE4">网球比赛后台管理系统 </p> </div> <div style="background-color:#66FFFF;position:static; "> <p> </p> <p> </p> <p> </p> <p> </p> <div align="center"><a href="add_game.jsp">添加比赛</a></div> <table border="5" cellpadding="10" cellspacing="1" bgcolor="#FFCCFF" > <tr> <td width="500" ><div align="center">流水账号</div></td> <td width="2500" ><div align="center">比赛名称</div></td> <td width="1500" ><div align="center">比赛开始时间</div></td> <td width="2000" ><div align="center" >比赛地点</div></td> <td width="1500" ><div align="center">允许最大人数</div></td> <td width="1500" ><div align="center">报名截止时间</div></td> <td width="1500" ><div align="center">当前参赛人数</div></td> <td width="1500" >操作1</td> <td width="1500" >操作2</td> </tr> <% while ((Recordset4_hasData)&&(Repeat1__numRows-- != 0)) { %> <tr> <td height="56"><div align="center"><%=(((Recordset4_data = Recordset4.getObject("id"))==null || Recordset4.wasNull())?"":Recordset4_data)%> <table border="1" cellpadding="10" cellspacing="10"> <tr> <td>id</td> <td>name</td> <td>start_time</td> <td>place</td> <td>lage_people</td> <td>finally_time</td> <td>now_people</td> </tr> <% while ((Recordset4_hasData)&&(Repeat2__numRows-- != 0)) { %> <tr> <td><%=(((Recordset4_data = Recordset4.getObject("id"))==null || Recordset4.wasNull())?"":Recordset4_data)%></td> <td><%=(((Recordset4_data = Recordset4.getObject("name"))==null || Recordset4.wasNull())?"":Recordset4_data)%></td> <td><%=(((Recordset4_data = Recordset4.getObject("start_time"))==null || Recordset4.wasNull())?"":Recordset4_data)%></td> <td><%=(((Recordset4_data = Recordset4.getObject("place"))==null || Recordset4.wasNull())?"":Recordset4_data)%></td> <td><%=(((Recordset4_data = Recordset4.getObject("lage_people"))==null || Recordset4.wasNull())?"":Recordset4_data)%></td> <td><%=(((Recordset4_data = Recordset4.getObject("finally_time"))==null || Recordset4.wasNull())?"":Recordset4_data)%></td> <td><%=(((Recordset4_data = Recordset4.getObject("now_people"))==null || Recordset4.wasNull())?"":Recordset4_data)%></td> </tr> <% Repeat2__index++; Recordset4_hasData = Recordset4.next(); } %> </table> </div></td> <td><div align="center"><%=(((Recordset4_data = Recordset4.getObject("name"))==null || Recordset4.wasNull())?"":Recordset4_data)%></div></td> <td><div align="center"><%=(((Recordset4_data = Recordset4.getObject("start_time"))==null || Recordset4.wasNull())?"":Recordset4_data)%></div></td> <td><div align="center"><%=(((Recordset4_data = Recordset4.getObject("place"))==null || Recordset4.wasNull())?"":Recordset4_data)%></div></td> <td><div align="center"><%=(((Recordset4_data = Recordset4.getObject("lage_people"))==null || Recordset4.wasNull())?"":Recordset4_data)%></div></td> <td><div align="center"><%=(((Recordset4_data = Recordset4.getObject("finally_time"))==null || Recordset4.wasNull())?"":Recordset4_data)%></div></td> <td><div align="center"><%=(((Recordset4_data = Recordset4.getObject("now_people"))==null || Recordset4.wasNull())?"":Recordset4_data)%></div></td> <td><div align="center"><a href="del_information.jsp?id=<%=(((Recordset4_data = Recordset4.getObject("id"))==null || Recordset4.wasNull())?"":Recordset4_data)%>">取消比赛</a></div></td> <td><div align="center"><a href="update_information.jsp?id=<%=(((Recordset4_data = Recordset4.getObject("id"))==null || Recordset4.wasNull())?"":Recordset4_data)%>">修改比赛</a></div></td> </tr> <% Repeat1__index++; Recordset4_hasData = Recordset4.next(); } %> </table> <p> </p> <div align="right"> <input name="" type="submit" onClick="window.location.href='findview.jsp'" value="返回主页" /> </div> <p> </p> </div> <div align="center"> <hr /> <p class="style4"> </p> </div> </div> </body> </html> <% Recordset4.close(); StatementRecordset4.close(); ConnRecordset4.close(); %>
做完这个之后,做一个增加页面:注意:前提是建立一个表单
这里就简化说明了,通过上文,相信对记录集的使用已经很清晰了。
直接选择插入记录集。
完成之后,就是对页面的设计了。
下面是代码:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <%@ include file="Connections/ACM.jsp" %> <% // *** Edit Operations: declare variables // set the form action variable request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String MM_editAction = request.getRequestURI(); if (request.getQueryString() != null && request.getQueryString().length() > 0) { String queryString = request.getQueryString(); String tempStr = ""; for (int i=0; i < queryString.length(); i++) { if (queryString.charAt(i) == '<') tempStr = tempStr + "<"; else if (queryString.charAt(i) == '>') tempStr = tempStr + ">"; else if (queryString.charAt(i) == '"') tempStr = tempStr + """; else tempStr = tempStr + queryString.charAt(i); } MM_editAction += "?" + tempStr; } // connection information String MM_editDriver = null, MM_editConnection = null, MM_editUserName = null, MM_editPassword = null; // redirect information String MM_editRedirectUrl = null; // query string to execute StringBuffer MM_editQuery = null; // boolean to abort record edit boolean MM_abortEdit = false; // table information String MM_editTable = null, MM_editColumn = null, MM_recordId = null; // form field information String[] MM_fields = null, MM_columns = null; %> <% // *** Insert Record: set variables if (request.getParameter("MM_insert") != null && request.getParameter("MM_insert").toString().equals("form1")) { MM_editDriver = MM_ACM_DRIVER; MM_editConnection = MM_ACM_STRING; MM_editUserName = MM_ACM_USERNAME; MM_editPassword = MM_ACM_PASSWORD; MM_editTable = "db_Manager.t_information"; MM_editRedirectUrl = "player_information.jsp"; String MM_fieldsStr = "id|value|name|value|start_time|value|place|value|lage_people|value|finally_time|value|now_people|value"; String MM_columnsStr = "id|none,none,NULL|name|',none,''|start_time|',none,NULL|place|',none,''|lage_people|none,none,NULL|finally_time|',none,NULL|now_people|none,none,NULL"; // create the MM_fields and MM_columns arrays java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_fieldsStr,"|"); MM_fields = new String[tokens.countTokens()]; for (int i=0; tokens.hasMoreTokens(); i++) MM_fields[i] = tokens.nextToken(); tokens = new java.util.StringTokenizer(MM_columnsStr,"|"); MM_columns = new String[tokens.countTokens()]; for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] = tokens.nextToken(); // set the form values for (int i=0; i+1 < MM_fields.length; i+=2) { MM_fields[i+1] = ((request.getParameter(MM_fields[i])!=null)?(String)request.getParameter(MM_fields[i]):""); } // append the query string to the redirect URL if (MM_editRedirectUrl.length() != 0 && request.getQueryString() != null) { MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + request.getQueryString(); } } %> <% // *** Insert Record: construct a sql insert statement and execute it if (request.getParameter("MM_insert") != null) { // create the insert sql statement StringBuffer MM_tableValues = new StringBuffer(), MM_dbValues = new StringBuffer(); for (int i=0; i+1 < MM_fields.length; i+=2) { String formVal = MM_fields[i+1]; String elem; java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_columns[i+1],","); String delim = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:""; String altVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:""; String emptyVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:""; if (formVal.length() == 0) { formVal = emptyVal; } else { if (altVal.length() != 0) { formVal = altVal; } else if (delim.compareTo("'") == 0) { // escape quotes StringBuffer escQuotes = new StringBuffer(formVal); for (int j=0; j < escQuotes.length(); j++) if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\''); formVal = "'" + escQuotes + "'"; } else { formVal = delim + formVal + delim; } } MM_tableValues.append((i!=0)?",":"").append(MM_columns[i]); MM_dbValues.append((i!=0)?",":"").append(formVal); } MM_editQuery = new StringBuffer("insert into " + MM_editTable); MM_editQuery.append(" (").append(MM_tableValues.toString()).append(") values ("); MM_editQuery.append(MM_dbValues.toString()).append(")"); if (!MM_abortEdit) { // finish the sql and execute it Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance(); Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword); PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toString()); MM_editStatement.executeUpdate(); MM_connection.close(); // redirect with URL parameters if (MM_editRedirectUrl.length() != 0) { response.sendRedirect(response.encodeRedirectURL(MM_editRedirectUrl)); return; } } } %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>添加比赛信息</title> <style type="text/css"> <!-- --> a.one{ color:#000099; text-decoration: underline blink; font-size: medium; } .STYLE4 {font-size: xx-large} .STYLE5 {color: #333333} </style> </head> <body> <div style="background-color:#FFCC99;position:relative;height:650px;top:auto"> <div style="background-color:#FFCC99;position:static;height:50px"> <p class="STYLE4">网球比赛后台管理系统 </p> </div> <div style="background-color:#66FFFF;position:static;height:500px; "> <form method="post" action="<%=MM_editAction%>" name="form1"> <p> </p> <p> </p> <p> </p> <table align="center"> <tr valign="baseline"> <td nowrap align="right"><div align="center">比赛名称:</div></td> <td><input type="text" name="name" value="" size="32"> </td> </tr> <tr valign="baseline"> <td nowrap align="right"><div align="center">比赛开始时间:</div></td> <td><input type="text" name="start_time" value="" size="32"> <span class="STYLE5">格式:YYY-MM-DD</span></td> </tr> <tr valign="baseline"> <td nowrap align="right"><div align="center">地址:</div></td> <td><input type="text" name="place" value="" size="32"> </td> </tr> <tr valign="baseline"> <td nowrap align="right"><div align="center">允许最大人数:</div></td> <td><input type="text" name="lage_people" value="" size="32"> </td> </tr> <tr valign="baseline"> <td nowrap align="right"><div align="center">报名截止时间:</div></td> <td><input type="text" name="finally_time" value="" size="32"> <span class="STYLE5">格式:YYY-MM-DD</span></td> </tr> <tr valign="baseline"> <td nowrap align="right"><div align="center">当期人数:</div></td> <td><input type="text" name="now_people" value="" size="32"> </td> </tr> <tr valign="baseline"> <td nowrap align="right"> </td> <td> <input type="submit" value="确认添加"> </td> </tr> </table> <p> </p> <div align="center"> <input name="" type="button" onClick="window.location.href='player_information.jsp'" value="返回" /> <input type="hidden" name="MM_insert" value="form1"> </div> </form> <p> </p> </div> <div align="center"></div> </div> </body> </html>
下面 是删除操作:
新建页面——选择应用程序:注意:前提是建立一个带有提交按钮的表单
同样的步骤,但这里有个注意的地方,唯一键列,选择id之后,如果你数据库对其设置的是int型,则要勾选数字。
若是verchat型,则不能勾选。
代码:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="Connections/ACM.jsp" %>
<%
// *** Edit Operations: declare variables
// set the form action variable
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String MM_editAction = request.getRequestURI();
if (request.getQueryString() != null && request.getQueryString().length() > 0) {
String queryString = request.getQueryString();
String tempStr = "";
for (int i=0; i < queryString.length(); i++) {
if (queryString.charAt(i) == '<') tempStr = tempStr + "<";
else if (queryString.charAt(i) == '>') tempStr = tempStr + ">";
else if (queryString.charAt(i) == '"') tempStr = tempStr + """;
else tempStr = tempStr + queryString.charAt(i);
}
MM_editAction += "?" + tempStr;
}
// connection information
String MM_editDriver = null, MM_editConnection = null, MM_editUserName = null, MM_editPassword = null;
// redirect information
String MM_editRedirectUrl = null;
// query string to execute
StringBuffer MM_editQuery = null;
// boolean to abort record edit
boolean MM_abortEdit = false;
// table information
String MM_editTable = null, MM_editColumn = null, MM_recordId = null;
// form field information
String[] MM_fields = null, MM_columns = null;
%>
<%
// *** Delete Record: construct a sql delete statement and execute it
if (request.getParameter("MM_delete") != null &&
request.getParameter("MM_delete").toString().equals("form1") &&
request.getParameter("MM_recordId") != null) {
MM_editDriver = MM_ACM_DRIVER;
MM_editConnection = MM_ACM_STRING;
MM_editUserName = MM_ACM_USERNAME;
MM_editPassword = MM_ACM_PASSWORD;
MM_editTable = "db_Manager.t_information";
MM_editColumn = "id";
MM_recordId = "" + request.getParameter("MM_recordId") + "";
MM_editRedirectUrl = "player_information.jsp";
// append the query string to the redirect URL
if (MM_editRedirectUrl.length() != 0 && request.getQueryString() != null) {
MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + request.getQueryString();
}
}
%>
<%
// *** Delete Record: construct a sql delete statement and execute it
if (request.getParameter("MM_delete") != null &&
request.getParameter("MM_recordId") != null) {
// create the delete sql statement
MM_editQuery = new StringBuffer("delete from ").append(MM_editTable);
MM_editQuery.append(" where ").append(MM_editColumn).append(" = ").append(MM_recordId);
if (!MM_abortEdit) {
// finish the sql and execute it
Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance();
Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword);
PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toString());
MM_editStatement.executeUpdate();
MM_connection.close();
// redirect with URL parameters
if (MM_editRedirectUrl.length() != 0) {
response.sendRedirect(response.encodeRedirectURL(MM_editRedirectUrl));
return;
}
}
}
%>
<%
String Recordset5__MMColParam = "1";
if (request.getParameter("id") !=null) {Recordset5__MMColParam = (String)request.getParameter("id");}
%>
<%
Driver DriverRecordset5 = (Driver)Class.forName(MM_ACM_DRIVER).newInstance();
Connection ConnRecordset5 = DriverManager.getConnection(MM_ACM_STRING,MM_ACM_USERNAME,MM_ACM_PASSWORD);
PreparedStatement StatementRecordset5 = ConnRecordset5.prepareStatement("SELECT * FROM db_Manager.t_information WHERE id = " + Recordset5__MMColParam + "");
ResultSet Recordset5 = StatementRecordset5.executeQuery();
boolean Recordset5_isEmpty = !Recordset5.next();
boolean Recordset5_hasData = !Recordset5_isEmpty;
Object Recordset5_data;
int Recordset5_numRows = 0;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>删除比赛信息</title>
<style type="text/css">
<!--
-->
a.one{
color:#000099;
text-decoration: underline blink;
font-size: medium;
}
.STYLE4 {font-size: xx-large}
.STYLE5 {color: #3A413C}
.STYLE6 {color: #3300FF}
</style>
</head>
<body>
<div style="background-color:#FFCC99;position:relative;height:650px;top:auto">
<div style="background-color:#FFCC99;position:static;height:50px">
<p class="STYLE4">网球比赛后台管理系统 </p>
</div>
<div style="background-color:#66FFFF;position:static;height:500px; ">
<form name="form1" action="<%=MM_editAction%>" method="POST">
<div align="center">
<p>
<input type="hidden" name="MM_delete" value="form1">
<input type="hidden" name="MM_recordId" value="<%=(((Recordset5_data = Recordset5.getObject("id"))==null || Recordset5.wasNull())?"":Recordset5_data)%>">
</p>
<p> </p>
<p> </p>
<p> </p>
<p> 再次确认是否删除该比赛所有信息? <span class="STYLE5">一旦删除,将无法恢复。</span> </p>
<p> </p>
<p>
<input name="submit" type="submit" value="确认" />
<input name="" type="button" onClick="window.location.href='player_information.jsp'" value="返回" />
</p>
</div>
</form>
</div>
<div align="center"></div><hr />
</div>
</body>
</html>
<%
Recordset5.close();
StatementRecordset5.close();
ConnRecordset5.close();
%>
最后是修改页面了,同样语言注意的是,建立一个表单。
![]()
同样需要注意的是 唯一键列。
![]()
预览:![]()
代码:<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <%@ include file="Connections/ACM.jsp" %> <% // *** Edit Operations: declare variables // set the form action variable request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String MM_editAction = request.getRequestURI(); if (request.getQueryString() != null && request.getQueryString().length() > 0) { String queryString = request.getQueryString(); String tempStr = ""; for (int i=0; i < queryString.length(); i++) { if (queryString.charAt(i) == '<') tempStr = tempStr + "<"; else if (queryString.charAt(i) == '>') tempStr = tempStr + ">"; else if (queryString.charAt(i) == '"') tempStr = tempStr + """; else tempStr = tempStr + queryString.charAt(i); } MM_editAction += "?" + tempStr; } // connection information String MM_editDriver = null, MM_editConnection = null, MM_editUserName = null, MM_editPassword = null; // redirect information String MM_editRedirectUrl = null; // query string to execute StringBuffer MM_editQuery = null; // boolean to abort record edit boolean MM_abortEdit = false; // table information String MM_editTable = null, MM_editColumn = null, MM_recordId = null; // form field information String[] MM_fields = null, MM_columns = null; %> <% // *** Update Record: set variables if (request.getParameter("MM_update") != null && request.getParameter("MM_update").toString().equals("form1") && request.getParameter("MM_recordId") != null) { MM_editDriver = MM_ACM_DRIVER; MM_editConnection = MM_ACM_STRING; MM_editUserName = MM_ACM_USERNAME; MM_editPassword = MM_ACM_PASSWORD; MM_editTable = "db_Manager.t_information"; MM_editColumn = "id"; MM_recordId = "" + request.getParameter("MM_recordId") + ""; MM_editRedirectUrl = "player_information.jsp"; String MM_fieldsStr = "name|value|start_time|value|place|value|lage_people|value|finally_time|value|now_people|value"; String MM_columnsStr = "name|',none,''|start_time|',none,NULL|place|',none,''|lage_people|none,none,NULL|finally_time|',none,NULL|now_people|none,none,NULL"; // create the MM_fields and MM_columns arrays java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_fieldsStr,"|"); MM_fields = new String[tokens.countTokens()]; for (int i=0; tokens.hasMoreTokens(); i++) MM_fields[i] = tokens.nextToken(); tokens = new java.util.StringTokenizer(MM_columnsStr,"|"); MM_columns = new String[tokens.countTokens()]; for (int i=0; tokens.hasMoreTokens(); i++) MM_columns[i] = tokens.nextToken(); // set the form values for (int i=0; i+1 < MM_fields.length; i+=2) { MM_fields[i+1] = ((request.getParameter(MM_fields[i])!=null)?(String)request.getParameter(MM_fields[i]):""); } // append the query string to the redirect URL if (MM_editRedirectUrl.length() != 0 && request.getQueryString() != null) { MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + request.getQueryString(); } } %> <% // *** Update Record: construct a sql update statement and execute it if (request.getParameter("MM_update") != null && request.getParameter("MM_recordId") != null) { // create the update sql statement MM_editQuery = new StringBuffer("update ").append(MM_editTable).append(" set "); for (int i=0; i+1 < MM_fields.length; i+=2) { String formVal = MM_fields[i+1]; String elem; java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_columns[i+1],","); String delim = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:""; String altVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:""; String emptyVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:""; if (formVal.length() == 0) { formVal = emptyVal; } else { if (altVal.length() != 0) { formVal = altVal; } else if (delim.compareTo("'") == 0) { // escape quotes StringBuffer escQuotes = new StringBuffer(formVal); for (int j=0; j < escQuotes.length(); j++) if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\''); formVal = "'" + escQuotes + "'"; } else { formVal = delim + formVal + delim; } } MM_editQuery.append((i!=0)?",":"").append(MM_columns[i]).append(" = ").append(formVal); } MM_editQuery.append(" where ").append(MM_editColumn).append(" = ").append(MM_recordId); if (!MM_abortEdit) { // finish the sql and execute it Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance(); Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword); PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toString()); MM_editStatement.executeUpdate(); MM_connection.close(); // redirect with URL parameters if (MM_editRedirectUrl.length() != 0) { response.sendRedirect(response.encodeRedirectURL(MM_editRedirectUrl)); return; } } } %> <% String Recordset6__MMColParam = "1"; if (request.getParameter("id") !=null) {Recordset6__MMColParam = (String)request.getParameter("id");} %> <% Driver DriverRecordset6 = (Driver)Class.forName(MM_ACM_DRIVER).newInstance(); Connection ConnRecordset6 = DriverManager.getConnection(MM_ACM_STRING,MM_ACM_USERNAME,MM_ACM_PASSWORD); PreparedStatement StatementRecordset6 = ConnRecordset6.prepareStatement("SELECT * FROM db_Manager.t_information WHERE id = " + Recordset6__MMColParam + ""); ResultSet Recordset6 = StatementRecordset6.executeQuery(); boolean Recordset6_isEmpty = !Recordset6.next(); boolean Recordset6_hasData = !Recordset6_isEmpty; Object Recordset6_data; int Recordset6_numRows = 0; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>修改比赛信息</title> <style type="text/css"> <!-- --> a.one{ color:#000099; text-decoration: underline blink; font-size: medium; } .STYLE4 {font-size: xx-large} .STYLE5 {color: #333333} .STYLE6 {color: #0000FF} </style> </head> <body> <div style="background-color:#FFCC99;position:relative;height:650px;top:auto"> <div style="background-color:#FFCC99;position:static;height:50px"> <p class="STYLE4">网球比赛后台管理系统 </p> </div> <div style="background-color:#66FFFF;position:static;height:500px; "> <form method="POST" action="<%=MM_editAction%>" name="form1"> <p> </p> <p> </p> <p> </p> <table align="center"> <tr valign="baseline"> <td nowrap align="right">比赛名称:</td> <td><input type="text" name="name" value="<%=(((Recordset6_data = Recordset6.getObject("name"))==null || Recordset6.wasNull())?"":Recordset6_data)%>" size="32"> </td> </tr> <tr valign="baseline"> <td nowrap align="right">比赛时间</td> <td><input type="text" name="start_time" value="<%=(((Recordset6_data = Recordset6.getObject("start_time"))==null || Recordset6.wasNull())?"":Recordset6_data)%>" size="32"> <span class="STYLE5">格式:YYY-MM-DD</span> </td> </tr> <tr valign="baseline"> <td nowrap align="right">地址</td> <td><input type="text" name="place" value="<%=(((Recordset6_data = Recordset6.getObject("place"))==null || Recordset6.wasNull())?"":Recordset6_data)%>" size="32"> </td> </tr> <tr valign="baseline"> <td nowrap align="right">允许最大人数</td> <td><input type="text" name="lage_people" value="<%=(((Recordset6_data = Recordset6.getObject("lage_people"))==null || Recordset6.wasNull())?"":Recordset6_data)%>" size="32"> </td> </tr> <tr valign="baseline"> <td nowrap align="right">报名截止时间</td> <td><input type="text" name="finally_time" value="<%=(((Recordset6_data = Recordset6.getObject("finally_time"))==null || Recordset6.wasNull())?"":Recordset6_data)%>" size="32"><span class="STYLE5">格式:YYY-MM-DD</span> </td> </tr> <tr valign="baseline"> <td nowrap align="right">当前人数</td> <td><input type="text" name="now_people" value="<%=(((Recordset6_data = Recordset6.getObject("now_people"))==null || Recordset6.wasNull())?"":Recordset6_data)%>" size="32"> </td> </tr> <tr valign="baseline"> <td nowrap align="right"> </td> <td> <input type="submit" value="确认"> <input name="" type="button" onClick="window.location.href='player_information.jsp'" value="返回" /> </td> </tr> </table> <input type="hidden" name="MM_update" value="form1"> <input type="hidden" name="MM_recordId" value="<%=(((Recordset6_data = Recordset6.getObject("id"))==null || Recordset6.wasNull())?"":Recordset6_data)%>"> </p> </form> <p> </p> </div> <div align="center"></div><hr /> </div> </body> </html> <% Recordset6.close(); StatementRecordset6.close(); ConnRecordset6.close(); %>
这样就完成了今天的工程。谢谢