--------我该如何把狗屎变成馒头--------

catail 2005-04-15 04:24:27
不好意思用一个恶心的标题吸引大家的目光。

我写了一个把数据的添加、删除和修改都在一个页面上完成的jsp页,请各位高手帮我把它搞成OO的。并指出各种不足,谢谢各位高人帮我提高。

--------------------------------------------

表:

Create Table table1 (Id Int, Name Char(10), Primary Key(Id));
--------------------------------------------

jsp:

<%@ page language="java" import="java.util.*,forask.*"%>

<jsp:useBean id="table1Bean" class="forask.Table1Bean" scope="page" />
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<script language="javascript" src="tableop.js"></script>
<script language="javascript">
<!--添加行的语法-->
var rowSyntax = "<td align=center>" +
"<input type=hidden name=id>" +
"<input type=hidden name=dFlag value=N>" +
"<input type=hidden name=iuFlag value=I>" +
"<input type=checkbox name=check onclick=\'delRow(0,1)\'>" +
"</td>" +
"<td align=center>" +
"<input type=text name=name onchange=\'mdfRow(0,2)\'></input>" +
"</td>";
</script>
</head>
<body bgcolor="#FFFFFF">
<%
//读取form提交的内容进行数据库操作。
request.setCharacterEncoding("GB2312");
String id[]= request.getParameterValues("id");
String iuFlag[] = request.getParameterValues("iuFlag");
String dFlag[] = request.getParameterValues("dFlag");
String name[] = request.getParameterValues("name");
if (iuFlag != null) {
for (int i = 0; i < iuFlag.length; i++) {
if ("I".equals(iuFlag[i]) && "N".equals(dFlag[i])) {
table1Bean.addRow(new String(name[i].getBytes("GB2312"),"ISO-8859-1"));
} else if (!"I".equals(iuFlag[i]) && "D".equals(dFlag[i])) {
table1Bean.removeRow(Integer.parseInt(id[i]));
} else if ("U".equals(iuFlag[i]) && "N".equals(dFlag[i])) {
table1Bean.modifyRow(Integer.parseInt(id[i]), new String(name[i].getBytes("GB2312"),"ISO-8859-1"));
}
}
}
%>
<form method=get>
<table id="tab" align="center" width="600" border="1" bgcolor="#EEF7FD" cellspacing="0" cellpadding="5" bordercolorlight="#006699" bordercolordark="#DDDDDD" >
<tr width="150" colspan="6" style="background-color:#6699CC;color:#FFFFFF">
<td align=center>删除标志</td>
<td align=center>名称</td>
</tr>
<%
//循环读出数据库中的数据。
Collection rows = table1Bean.getRows();
Iterator it = rows.iterator();

while(it.hasNext()) {
Row row = (Row)it.next();
%>
<tr>
<td align=center>
<input type=hidden name=id value=<%=row.getId()%>>
<input type=hidden name=dFlag value=N>
<input type=hidden name=iuFlag value=N>
<input type=checkbox name=check onclick='delRow(0,1)'>
</td>
<td align=center>
<input type=text name=name <%= "".equals(row.getName())||row.getName()==null?"":("value=" + row.getName())%> onchange='mdfRow(0,2)'>
</td>
</tr>
<%
}
%>
<tr align=left>
<td colspan=3>
<input type=button value="增加" onclick="addRow(tab,rowSyntax,-1)">
<input type=submit value="提交">
</td>
</tr>
</form>
</table>

</body>
</html>
...全文
226 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
catail 2005-04-18
  • 打赏
  • 举报
回复
to hellwindy(夜神·月)
不好意思我的注释写的太烂了。

to dafei0320(大飞)
^_^,其实也不是很多啊
dafei0320 2005-04-18
  • 打赏
  • 举报
回复
太多了--…………
catail 2005-04-18
  • 打赏
  • 举报
回复
to zhutouzip(Speak out!-shyboy)

我把添加删除、修改的操作都封装在了Table1Bean中。你说的将添加删除修改的操作封装在bean中是否和我说的不一样呢?请帮忙解释解释,谢谢!!!
99percent 2005-04-18
  • 打赏
  • 举报
回复

心中有佛.佛自然在我心中.

心中有馒头.狗屎都会变馒头.
waterwash 2005-04-18
  • 打赏
  • 举报
回复
够恶心~!同意楼上的~!
yinleiyoung 2005-04-18
  • 打赏
  • 举报
回复
楼主,如果你能把自己变成狗,那么狗屎自然就变馒头了!
呵呵……开个玩笑!!
用javabean吧,jsp+javabean才有出路!
qingzhuang 2005-04-18
  • 打赏
  • 举报
回复
看javabean
wind_rain 2005-04-15
  • 打赏
  • 举报
回复
有时间帮你看看
woodcord 2005-04-15
  • 打赏
  • 举报
回复
呵呵,我进来了~~~
weastsea 2005-04-15
  • 打赏
  • 举报
回复
拆了重来
全粘架构师 2005-04-15
  • 打赏
  • 举报
回复
JS?看不懂
grayfoxdie 2005-04-15
  • 打赏
  • 举报
回复
数据的添加、删除和修改都在一个页上会导致逻辑上的麻烦。建议把添加和修改分开,即使WEB页看上去非常相似,也一定要分开对待。删除的功能比较简单,可以考虑把它和修改放一起。
飞行的兔子 2005-04-15
  • 打赏
  • 举报
回复
你写一个javabean,将这些删除,插入,等操作封装在这个javabean中!
然后用<jsp:useBean id= />这样的方式来用,不是很难吧!自己写一个就行了!
catail 2005-04-15
  • 打赏
  • 举报
回复
在table中每行都记录了dFlag和iuFlag,如果该行选择了删除则dFlag设置为D;iuFlag记录行的插入修改情况,如果插入则设置为I修改则设置为U,新行修改后仍为I。如果行的dFlag为D,iuFlag为I则说明删除新插入的行即为空操作;如果dFlag为D,iuFlag为U则仍然删除。

两个标志位是通过javascript修改的。

本人很菜如果有些术语说的不着四六请见谅!!!!!!!
sjg008 2005-04-15
  • 打赏
  • 举报
回复
hehe 想要OO的馒头......
catail 2005-04-15
  • 打赏
  • 举报
回复
引用的bean Row.java


package forask;

public class Row {

private int id;
private String name;

public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}

-------------------------------------------

引用的bean Table1Bean.java


package forask;

import java.sql.*;
import java.util.Collection;
import java.util.ArrayList;

public class Table1Bean {

public Table1Bean() {}

public static Connection getCon() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String dbUrl = "jdbc:mysql://localhost:3306/report?useUnicode&characterEncoding=UTF-8&user=root";
Connection con = java.sql.DriverManager.getConnection(dbUrl);
return con;
}

public void addRow(String name) throws Exception {
int id = getMaxId() + 1;
Connection con = getCon();
PreparedStatement pstm = con.prepareStatement("insert into Table1 values (?, ?)");
pstm.setInt(1, id);
pstm.setString(2, name);
pstm.execute();
}

public void removeRow(int id) throws Exception {
Connection con = getCon();
PreparedStatement pstm = con.prepareStatement("delete from Table1 where Id = ? ");
pstm.setInt(1, id);
pstm.execute();
}

public void modifyRow(int id, String name) throws Exception {
Connection con = getCon();
PreparedStatement pstm = con.prepareStatement("update Table1 set Name = ? where Id = ?");
pstm.setString(1, name);
pstm.setInt(2, id);
pstm.execute();
}

public Collection getRows() throws Exception {
Collection ret = new ArrayList();
Connection con = getCon();
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select count(*) from Table1");
int roadCount = 0;
if (rs.next()) {
roadCount = rs.getInt(1);
rs.close();
}

if (roadCount > 0) {
rs = stm.executeQuery("select * from Table1");
while (rs.next()) {
Row row = new Row();
row.setId(rs.getInt(1));
row.setName(rs.getString(2));
ret.add(row);
}
}
rs.close();
stm.close();
return ret;
}

private int getMaxId() throws SQLException, ClassNotFoundException {
Connection con = getCon();
PreparedStatement pstmt = con.prepareStatement("select max(Id) from Table1");
ResultSet rs = pstmt.executeQuery();
rs.next();
int maxId = rs.getInt(1);
rs.close();
pstmt.close();
return maxId;
}
}
catail 2005-04-15
  • 打赏
  • 举报
回复
引用的js:

/*===================================================
动态表格操作函数。
===================================================*/

/*
功能:在指定的表中按给出的语法插入指定的行。
参数:
table: 指定的表
innerHtml: 插入行的代码
order: 插入的位置,正数为表的第order行,
负数为表的倒数order行(表头为第0行)。
调用举例:
addRow(tab1, "<td>col1</td><td>col2</td><td>col3</td>", -1)
*/
function addRow(table, innerHtml, order) {
if(table == null || table.rows.length < 1) {
return;
}

var row;
if (order > 0)
{
row = table.insertRow(order);
} else {
row = table.insertRow(table.rows.length + order);
}

innerHtml = innerHtml.replace(/(^\s*)|(\s*$)|(\'*)/g, "");
innerHtml = innerHtml.replace(/<\/td\s*>/gi,"</td>");
innerHtml = innerHtml.replace(/\s+/g, " ");
innerHtml = innerHtml.replace(/\s+=\s+/g, "=");
innerHtml = innerHtml.replace(/>\s+</g, "><");
innerHtml = innerHtml.replace(/\s*,\s*/g, ",");
innerHtml = innerHtml.replace(/\s*;\s*/g, ";");

var cell, td, iTd;
var innerHtmlArray = innerHtml.split("</td>");
//读取td
for (iTd = 0; iTd < innerHtmlArray.length - 1; iTd++) {
cell = row.insertCell(iTd);
td = innerHtmlArray[iTd];

//读取td的属性和内容
var property, prptEnd, content;
prptEnd = td.indexOf("><");
property = td.substr(4, prptEnd - 4);
content = td.substr(prptEnd + 1);
cell.innerHTML = content;

var subPrpt, prptArray, iPrpt, subPrptArray, subPrptName, subPrptValue;
prptArray = property.split(" ");
for (iPrpt = 0; iPrpt < prptArray.length; iPrpt++) {
subPrpt = prptArray[iPrpt];
subPrptArray = subPrpt.split("=");
subPrptName = subPrptArray[0];
subPrptValue = subPrptArray[1];
cell.setAttribute(subPrptName, subPrptValue);
}

}
}

/*
功能:获得当前行的指定对象。
参数:
cellIndex: 对象所在TD的序号(从0开始记)。
inCellIndex: 对象在TD中的序号(从0开始记)。
调用举例:
getElementInCurRow(0, 0)
*/
function getElementInCurRow(cellIndex, inCellIndex) {
return event.srcElement.parentElement.parentElement.children(cellIndex).children(inCellIndex);
}

/*
功能: 设置删除标志位。
参数:
cellIndex: 删除标志位所在TD的序号(从0开始记)。
inCellIndex: 删除标志位在TD中的序号(从0开始记)。
调用举例:
delRow(0, 0)
*/
function delRow(cellIndex, inCellIndex) {
var check = event.srcElement;
if (check.checked == true) {
getElementInCurRow(cellIndex, inCellIndex).value = "D";
} else {
getElementInCurRow(cellIndex, inCellIndex).value = "N";
}
}

/*
功能: 设置更新标志位。
参数:
cellIndex: 更新标志位所在TD的序号(从0开始记)。
inCellIndex: 更新标志位在TD中的序号(从0开始记)。
调用举例:
mdfRow(0, 0)
*/
function mdfRow(cellIndex, inCellIndex) {
var iuFlag = getElementInCurRow(cellIndex, inCellIndex);
if (iuFlag.value != "I") {
iuFlag.value = "U";
}
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧