45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:JSP实现列表增删的方法

JSP实现列表增删的方法

2016-09-02 13:15:32 来源:www.45fan.com 【

JSP实现列表增删的方法

list.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>list.jsp</title>
</head>

<body>
<SCRIPT language="JavaScript">
function gotoEdit(id,name1,date1,salary){
var id,name1,date1,salary;
form1.action = "edit.jsp?userID="+id+"&name1="+name1+"&date1="+date1+"&salary="+salary+"";
form1.submit();
}
function gotoDel(id,name1,date1,salary){
var id,name1,date1,salary;
form1.action = "del.jsp?userID="+id+"&name1="+name1+"&date1="+date1+"&salary="+salary+"";
form1.submit();
}
</SCRIPT>
<%
//声明
Connection conn;
java.sql.Statement stmt;
ResultSet rslt;
//注册jdbc驱动程序
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//连接数据库
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JspTest","sa","sa");
//建立对驱动程序的引用
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//执行查询语句
rslt = stmt.executeQuery("Select * from Tbl_user");
%>
<form name="form1" method="post" action="">
<table width="70%" border="2">
<tr>
<td width="12%" height="33">
<div align="center">用户ID</div></td>
<td width="17%"><div align="center">姓名</div></td>
<td width="18%"><div align="center">出生日期</div></td>
<td width="28%"><div align="center">工资</div></td>
<td width="25%"><div align="center">操作</div></td>
</tr>
<%while(rslt.next()){
String userId=rslt.getString(1);
String name1=rslt.getString(2);
String date1=(String)(rslt.getString(3)).substring(0,10);
String salary=rslt.getString(4);
%>
<tr>
<td height="25">
<div align="center"><%=userId%></div></td>
<td><div align="center"><%=name1%></div></td>
<td><div align="center"><%=date1%></div></td>
<td><div align="center"><%=salary%></div></td>
<td><div align="center">
<input type="button" name="Edit" value="编辑" onclick=gotoEdit("<%=userId%>","<%=name1%>","<%=date1%>","<%=salary%>")>
<input type="button" name="Delete" value="删除" onclick=gotoDel("<%=userId%>","<%=name1%>","<%=date1%>","<%=salary%>")>
</div></td>
</tr>
<%}%>
</table>
<%
if(conn!=null&&!conn.isClosed()){
conn.close();
conn=null;
}
}catch(Exception e){
e.printStackTrace();
}
%>
</form>
<form name="form2" method="post" action="add.jsp">
<input type="submit" name="Add" value="添加记录">
</form>
<p>&nbsp;</p>
</body>
</html>

add.jsp

<%@ page language="java" pageEncoding="GB2312" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<SCRIPT language="JavaScript">
function isValid(form1){
if(form1.name1.value==""){
alert("姓名不能为空!");
form1.name1.focus();
return false;
} else if(form1.name1.value.length>10){
alert("姓名太长!");
return false;
}

if(form1.salary.value<0||isNumberString(form1.salary.value,".1234567890")!=1 ){
alert("工资栏为空或输入了负值或非法字符!");
form1.salary.focus();
return false;
}

//把表单里的值赋给相应变量
name1=form1.name1.value;

if(form1.month1.value<10)month1="0"+form1.month1.value
else month1=form1.month1.value;
if(form1.day1.value<10)day1="0"+form1.day1.value
else day1="0"+form1.day1.value;
date1=form1.year1.value+"-"+month1+"-"+day1;

salary=form1.salary.value;

//设置表单action
form1.action="add_end.jsp?name1="+name1+"&date1="+date1+"&salary="+salary+"";
form1.submit();
}

function isNumberString (InString,RefString)
{
if(InString.length==0) return (false);
for (Count=0; Count < InString.length; Count++) {
TempChar= InString.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1)
return (false);
}
return (true);
}


// if(form.replycontent.value==""){
// alert("内容不能为空!");
// return false;
// }
</SCRIPT>
<form name="form1" method="post" action="" >
<table width="75%" border="0">
<tr>
<td>姓名</td>
<td><input type="text" name="name1">
* </td>
</tr>
<tr>
<td>工资</td>
<td><input type="text" name="salary">
*</td>
</tr>
<tr>
<td>出生日期</td>
<td> <script language="JavaScript">
function mydate(dateid,datename){ //Powered by yayap
var dateid,datename,i;
var j=1;
document.write('<select name="'+datename+'" size="1">'+'onChange="'+'alert("ddd");'+'"');
// <select name="select" size="1" onChange="alert(document.all.form1.select[selectedIndex].value)">
if (dateid>1899) j=1900
else i=j
for (i=j;i<=dateid ;i++)
{
document.write('<option value="'+i+'">'+i+'</option>')
};
document.write('</select>');
}
mydate(2020,'year1');
mydate(12,'month1');
mydate(31,'day1');
//document.write(document.all.form1.year1.options[selectedIndex].value);
</script> </td>
</tr>
<tr>
<td height="53"> <p align="right">&nbsp; </p>
<p>&nbsp; </p>
<p>&nbsp;</p></td>
<td> <p align="left">
<input type="button" name="Submit" value="提交" onclick=isValid(form1)>
<input type="button" name="Cancel" value="取消" onclick="Javascript:form1.action='list.jsp';form1.submit();">
</p>
<p>&nbsp; </p>
<p>&nbsp; </p></td>
</tr>
</table>
</form>
<p>&nbsp;</p>

</body>
</html>

add_end.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>list.jsp</title>
</head>

<body>
<%!long userId;String name1,date1;float salary;%>
<SCRIPT language="JavaScript">
function gotoAdd(name1,date1,salary){
var name1,date1,salary;
form1.action = "add.jsp?name1="+name1+"&date1="+date1+"&salary="+salary+"";
form1.submit();
}
</SCRIPT>
<%
//声明
Connection conn;
java.sql.Statement stmt;
int rowCount;
//注册jdbc驱动程序
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//连接数据库
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JspTest","sa","sa");
//建立对驱动程序的引用
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//执行添加语句
name1 = new String(request.getParameter("name1").getBytes("8859_1"),"GB2312");
date1 = request.getParameter("date1");
salary = Float.valueOf(request.getParameter("salary")).floatValue();
String addReq = "INSERT INTO Tbl_user(Name,birthday,salary) VALUES (" + "'" + name1 + "'" + "," + "'" + date1 + "'" + "," + "'" + salary + "'"+")";
rowCount = stmt.executeUpdate(addReq);
if(rowCount>0){
%>
数据添加成功!
<%}
else{
%>
数据添加失败!
<%}
if(conn!=null&&!conn.isClosed()){
conn.close();
conn=null;
}
}catch(Exception e){
%>
数据添加失败!
<%
e.printStackTrace();
}
%>

</form>
<form name="form1" method="post" action="">
<input type="button" name="Add" value="返回插入" onclick=gotoAdd("<%=name1%>","<%=date1%>","<%=salary%>")>
<input type="button" name="List" value="返回列表" onclick="Javascript:form1.action='list.jsp';form1.submit();">
</form>
<p>&nbsp;</p>
</body>
</html>

del.jsp
<%@ page language="java" pageEncoding="GB2312" %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<SCRIPT language="JavaScript">
function gotoDelEnd(form1){
var id = form1.ID.value;
form1.action = "del_end.jsp?userId="+id+"";
form1.submit();
}
</SCRIPT>
<form name="form1" method="post" action="" >
 <table width="75%" border="0">
 <tr> 
 <td>用户</td>
 <td> <input type="text" name="ID" value="<%=request.getParameter
("userID")%>" readonly> </td>
 </tr>
 <tr> 
 <td>姓名</td>
<!--转换字符编码-->
 <td><input type="text" name="name1" value="<%=new String(request.
getParameter("name1").getBytes("8859_1"),"GB2312")%>" readonly>
 </td>
 </tr>
 <tr> 
 <td>工资</td>
 <td><input type="text" name="salary" value="<%=request.getParameter
("salary")%>" readonly>
 </td>
 </tr>
 <tr> 
 <td>出生日期</td>
 <td><input type="text" name="salary" value="<%=request.getParameter
("date1")%>" readonly>
 </td>
 </tr>
 <tr> 
 <td height="53"> <p align="right">&nbsp; </p>
 <p>&nbsp; </p>
 <p>&nbsp;</p></td>
 <td> <p align="left"> 
 <input type="button" name="Submit" value="确认删除" onclick=
gotoDelEnd(form1)>
 <input type="button" name="Cancel" value="取消删除" 
onclick="Javascript:form1.action='list.jsp';form1.submit();">
 </p>
 <p>&nbsp; </p>
 <p>&nbsp; </p></td>
 </tr>
 </table>
</form>
<p>&nbsp;</p>
</body>
</html>
----------------------------------------
del_end.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" 
import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>list.jsp</title>
</head>
<body>
<%
//声明
Connection conn;
java.sql.Statement stmt;
int rowCount;
//注册jdbc驱动程序
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//连接数据库
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;
DatabaseName=JspTest","sa","sa");
//建立对驱动程序的引用
stmt = conn.createStatement();
//执行删除语句
long userId = Long.valueOf(request.getParameter("userId")).longValue();

String delReq = "DELETE FROM Tbl_user WHERE Id=" +"'" + userId + "'";
rowCount = stmt.executeUpdate(delReq);
if(rowCount>0){
%>
数据删除成功!
<%}
else{
%>
数据删除失败!
<%}
if(conn!=null&&!conn.isClosed()){
conn.close();
conn=null;
} 
 }catch(Exception e){
 %>
 数据删除失败!
 <%
 e.printStackTrace();
 }
%>
</form>
<form name="form1" method="post" action="">
<input type="button" name="List" value="返回列表" 
onclick="Javascript:form1.action='list.jsp';form1.submit();"> 
</form>
<p>&nbsp;</p>
</body>
</html>
edit.jsp
<%@ page language="java" pageEncoding="GB2312" %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<SCRIPT language="JavaScript"> 
function isValid(form1){
var id1,name1,month1,day1,date1,salary;
 if(form1.name1.value==""){ 
 alert("姓名不能为空!"); 
 form1.name1.focus();
 return false; 
 } else if(form1.name1.value.length>10){
 alert("姓名太长!");
 return false;
 }
 
if(form1.salary.value<0||isNumberString(form1.salary.value,".1234567890")!=1 ){
alert("工资栏为空或输入了负值或非法字符!");
form1.salary.focus();
return false;
}
//把表单里的值赋给相应变量
id1=form1.ID.value;
name1=form1.name1.value;

if(form1.month1.value<10)month1="0"+form1.month1.value
else month1=form1.month1.value;
if(form1.day1.value<10)day1="0"+form1.day1.value
else day1="0"+form1.day1.value;
date1=form1.year1.value+"-"+month1+"-"+day1;

salary=form1.salary.value;

//设置表单action
form1.action="edit_end.jsp?userId="+id1+"&name1="+name1+"&date1=
"+date1+"&salary="+salary+"";
form1.submit();
}

function isNumberString (InString,RefString)
{
if(InString.length==0) return (false);
for (Count=0; Count < InString.length; Count++) {
TempChar= InString.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1) 
return (false);
}
return (true);
}
// if(form.replycontent.value==""){ 
 // alert("内容不能为空!"); 
 // return false; 
 // } 
 
</SCRIPT>
<form name="form1" method="post" action="" >
 <table width="75%" border="0">
 <tr> 
 <td>用户</td>
 <td> <input type="text" name="ID" value="<%=request.getParameter
("userID")%>" readonly> </td>
 </tr>
 <tr> 
 <td>姓名</td>
<!--转换字符编码-->
 <td><input type="text" name="name1" value="<%=new String(request.
getParameter("name1").getBytes("8859_1"),"GB2312")%>">
 * </td>
 </tr>
 <tr> 
 <td>工资</td>
 <td><input type="text" name="salary" value="<%=request.getParameter
("salary")%>">
 *</td>
 </tr>
 <tr> 
 <td>出生日期</td>
 <td> <script language="JavaScript">
function mydate(dateid,datename,oldValue){ //Powered by yayap
var dateid,datename,i,oldValue;
var j=1;
document.write('<select name="'+datename+'" size="1">'+'onChange="'+
'alert("ddd");'+'"');
// <select name="select" size="1" onChange="alert(document.all.form1.
select[selectedIndex].value)">
if (dateid>1899) j=1900
else i=j
for (i=j;i<=dateid ;i++)
{if(i!=oldValue)document.write('<option value="'+i+'">'+i+'</option>')
 else document.write('<option value="'+i+'" selected>'+i+'</option>')
};
document.write('</select>');
}
mydate(2020,'year1',<%=(String)(request.getParameter("date1")).
substring(0,4)%>);
mydate(12,'month1',<%=(String)(request.getParameter("date1")).
substring(5,7)%>);
mydate(31,'day1',<%=(String)(request.getParameter("date1")).
substring(8,10)%>);
//document.write(document.all.form1.year1.options[selectedIndex].value);
</script> </td>
 </tr>
 <tr> 
 <td height="53"> <p align="right">&nbsp; </p>
 <p>&nbsp; </p>
 <p>&nbsp;</p></td>
 <td> <p align="left"> 
 <input type="button" name="Submit" value="提交" 
onclick=isValid(form1)>
 <input type="button" name="Cancel" value="取消" 
onclick="Javascript:form1.action='list.jsp';form1.submit();">
 </p>
 <p>&nbsp; </p>
 <p>&nbsp; </p></td>
 </tr>
 </table>
</form>
<p>&nbsp;</p>
</body>
</html>
-------------------------------
edit_end.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" 
import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>list.jsp</title>
</head>
<body>
<%!long userId;String name1,date1;float salary;%>
<SCRIPT language="JavaScript">
function gotoEdit(id,name1,date1,salary){
var id,name1,date1,salary;
form1.action = "edit.jsp?userID="+id+"&name1="+name1+"&date1=
"+date1+"&salary="+salary+"";
form1.submit();
}
</SCRIPT>
<%
//声明
Connection conn;
java.sql.Statement stmt;
int rowCount;
//注册jdbc驱动程序
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//连接数据库
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;
DatabaseName=JspTest","sa","sa");
//建立对驱动程序的引用
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.
CONCUR_READ_ONLY);
//执行更新语句
userId = Long.valueOf(request.getParameter("userId")).longValue();
name1 = new String(request.getParameter("name1").getBytes("8859_1"),"GB2312");
date1 = request.getParameter("date1");
salary = Float.valueOf(request.getParameter("salary")).floatValue();
String updateReq = "UPDATE Tbl_user SET " + " Name=" + "'" + name1 + "'" 
+ "," + " birthday=" +"'" + date1 + "'" + "," + " salary=" +"'" + salary 
+ "'" + " WHERE Id=" +"'" + userId + "'";
rowCount = stmt.executeUpdate(updateReq);
if(rowCount>0){
%>
数据更新成功!
<%}
else{
%>
数据更新失败!
<%}
if(conn!=null&&!conn.isClosed()){
conn.close();
conn=null;
} 
 }catch(Exception e){
%>
数据更新失败!
<%
 e.printStackTrace();
 }
%>
</form>
<form name="form1" method="post" action="">
<input type="button" name="Edit" value="返回编辑" 
onclick=gotoEdit("<%=userId%>","<%=name1%>","<%=date1%>","<%=salary%>")> 
<input type="button" name="List" value="返回列表" 
onclick="Javascript:form1.action='list.jsp';form1.submit();"> 
</form>
<p>&nbsp;</p>
</body>
</html>
 

本文地址:http://www.45fan.com/a/question/71295.html
Tags: 实现 JSP 列表
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部