添加分类例子:
数据库语句(Oracle):(如父类’A_1’,子类’A_!_2’)
-- Create table
create table TESTCLASS
(
ID NUMBER not null,
CATE_ID VARCHAR2(50),
CATE_PIDVARCHAR2(50),
CATE_NAME VARCHAR2(50)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table TESTCLASS
add constraint TID_PK primary key (ID)
disable;
create table TESTCLASS
(
ID NUMBER not null,
CATE_ID VARCHAR2(50),
CATE_PIDVARCHAR2(50),
CATE_NAME VARCHAR2(50)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table TESTCLASS
add constraint TID_PK primary key (ID)
disable;
Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>添加分类测试成功</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">
-->
<script language="javascript">
var XMLHttpReq; //创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e){
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
//发送请求函数
function sendRequest() {
createXMLHttpRequest();
var url = "optionsServlet";
XMLHttpReq.open("GET", url);
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
XMLHttpReq.send(null);// 发送请求
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息
display();
setTimeout("sendRequest()", 10000); //10秒后刷新
} else { //页面不正常
window.alert("您所请求的页面有异常。");
}
}
}
// 显示更新数据信息的函数
function display() {
var optionsText = XMLHttpReq.responseText;
document.getElementById("selects").innerHTML = optionsText;
}
</script>
</head>
<body onload="sendRequest()">
<!-- 注意:<option>选项改为动态显示就行<option> -->
<center>添加分类</center>
<form action="test/addShow.jsp" name="form1" method="post">
<table width="35%" border="0" align="center" class="maintbbg" cellspacing="1" cellpadding="0">
<tr class="maintbtr3" height=25>
<td>
<div align="right">用户分类名:</div>
</td>
<td> <input TYPE="text" NAME="cate_name" class="input" ></td>
<div id="selects" name="selects"></div>
</tr>
<tr>
<td><input TYPE="submit" name="提交" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
addShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="cc.yiyao.dbConn.JdbcUtil,java.sql.*" %>
<%
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 'addShow.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>
<%
Connection conn = JdbcUtil.getConnection();
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
String dbCate_id = "";//将来数据库中插入的字段
String temp_cateid ="";
String cate_pid = request.getParameter("cate_pid");
String sqlSel = "select * from testClass where cate_pid = '"+cate_pid+"' order by id desc"; //(实际上只要返回一条记录,不过oracle不能用top1
ResultSet rs = null;
rs= stmt.executeQuery(sqlSel);
if(rs.next()){//有相同类别的数据,取出最大值,再加上1;
temp_cateid = rs.getString("cate_id");
//分离出数字,后再加1,如A_2_1,分离出最后一个数字,再加1。
int k = temp_cateid.lastIndexOf("_");
//获得最后一部分的数字
String strLastPart = temp_cateid.substring(k+1,temp_cateid.length());
out.println("strLastPart="+strLastPart);
//获得字符串的前一部分
String strFirstPart = temp_cateid.substring(0,k+1);
out.println("strFirstPart="+strFirstPart);
//将最后的数字加1,最后合起来
dbCate_id = strFirstPart + (Integer.parseInt(strLastPart)+1);
out.println("dbCate_id"+dbCate_id);
}else{
//没有相同类别的数据,初始化开始值
if(!cate_pid.equals("0")){//没有相同类别的数据(开始的子类别为父类别+”_1",初始化开始值(select 中以"0"为开始,没有相同类别又等于0说明数据库中都没有数据)
dbCate_id = cate_pid+"_1";
}else{//说明数据库未插入 //select 中以"0"为开始,没有相同类别又等于0说明数据库中都没有数据
dbCate_id = "A_1";
}
}
String className = request.getParameter("cate_name");
String sql = "insert into testClass values (5,'"+dbCate_id+"','"+cate_pid+"','"+className+"')";
stmt.executeUpdate(sql);
conn.commit();
conn.setAutoCommit(true);
out.println("dbCate_id2"+dbCate_id);
if(stmt!= null) {
stmt.close();
}
if(conn!= null) {
conn.close();
}
%>
</body>
</html>
Servlet:
package test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RefreshOptionsServlet extends HttpServlet {
public RefreshOptionsServlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
response.setHeader("Cache-Control", "no-cache");//注意设置页面不缓存,不然就看不到刷新的效果了!!
PrintWriter out = response.getWriter();
StringBuffer optionsText = new StringBuffer("");
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:Oracle1",
"system",
"system"
);
Statement stmt = conn.createStatement();
String sql = "select * from testClass";//sql = "select * from testClass where cate_pid = 0";
ResultSet rs = null;
rs = stmt.executeQuery(sql);
String cate_pid = ""; //父编号
String cate_name = ""; //名称
String cate_id = ""; //编号
String sql2 = "select * from testClass where cate_pid='";
optionsText = optionsText.append("<select name='cate_pid'><option value='0'>无</option>");
while(rs.next()){
cate_id = rs.getString("cate_id");
cate_name = rs.getString("cate_name");
cate_pid = rs.getString("cate_pid");
optionsText = optionsText.append("<option value='"+cate_id+"'>"+cate_name+"</option>");
}
} catch (ClassNotFoundException e) {
e.p