求个JSP页面中添加数据库简单实例

最好就添加一项数据的!!!... 最好就添加一项数据的!!! 展开
 我来答
宽厚又亲切丶不倒翁g
2011-12-07 · TA获得超过210个赞
知道答主
回答量:112
采纳率:0%
帮助的人:26.7万
展开全部
config.jsp:
<title>连接数据库</title><%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<%

String DRIVERNAME="com.mysql.jdbc.Driver";
String URL="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8";
String USERNAME="root";
String USERPASSWORD="admin";

%>

delect.jsp:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!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">
<!--
body {
background-color: #CCFFFF;
background-image: url();
}
-->
</style></head>

<body>

<p> </p>
<p> </p>
<div align="center">
<%@include file="config.jsp"%>

<%

String delNum=request.getParameter("stuNum");

Class.forName(DRIVERNAME);
Connection con=DriverManager.getConnection(URL,USERNAME,USERPASSWORD);
Statement stmt=con.createStatement();

String sql="delete from user where stuNum='"+delNum+"'";

stmt.executeUpdate(sql);

out.print("<h2>删除成功!</h2>");

stmt.close();
con.close();

%>

<a href="../welcome.jsp/index.jsp">返回首页</a></div>
</body>
</html>

query.jsp:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%request.setCharacterEncoding("utf-8");%>
<!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">
<!--
body {
background-color: #CCFFFF;
}
-->
</style></head>

<body>

<form action="query1.jsp" name="query" method="post">

<table border="0" align="center" bgcolor="#FFCC33">
<tr>
<td>
按姓名查询:
</td>
<td>
<div align="center">
<input type="text" name="queryName" />
</div></td>
</tr>

<tr>
<td>
</td>
<td align="right">
<input type="submit" value="查询" />
<input type="reset" value="重置" />
</td>
</tr>

</table>

<p> </p>
<table border="0" align="center" bgcolor="#FFCC33">
<tr>
<td> 按学号查询: </td>
<td><div align="center">
<input type="text" name="queryNum" />
</div></td>
</tr>
<tr>
<td></td>
<td align="right"><input name="submit" type="submit" value="查询" />
<input name="reset" type="reset" value="重置" />
</td>
</tr>
</table>
<p> </p>
<p> </p>
<p> </p>
</form>

</body>
</html>

query1.jsp:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" errorPage="" %>
<!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>
</head>

<body>

<div align="center">
<p>
<%@include file="config.jsp"%>

<%

String queName=request.getParameter("queryName");

Class.forName(DRIVERNAME);
Connection con=DriverManager.getConnection(URL,USERNAME,USERPASSWORD);
Statement stmt=con.createStatement();

String sql="select * from user where stuName like'%"+queName+"%'";

ResultSet rs=stmt.executeQuery(sql);

int intPageSize;
int intRowCount;
int intPageCount;
int intPage;
String strPage;
int i;
intPageSize=3;
strPage=request.getParameter("page");

if(strPage==null)
{intPage=1;}
else
{intPage=java.lang.Integer.parseInt(strPage);}

if(intPage<1)
{intPage=1;}
rs.last();
intRowCount=rs.getRow();
intPageCount=(intRowCount+intPageSize-1)/intPageSize;
if(intPage>intPageCount)
intPage=intPageCount;
if(intPageCount>0)
{rs.absolute((intPage-1)*intPageSize+1);}
i=0;

out.print("<h2>查询结果</h2>");

%>
</p>
<table border="1">
<tr>
<td>
学生学号 </td>
<td>
学生姓名 </td>
<td>
学生年龄 </td>
<td>
操作 </td>
</tr>

<%
while(i<intPageSize && !rs.isAfterLast()){
%>

<tr>
<td><%=rs.getString("stuNum")%></td>
<td><%=rs.getString("stuName")%></td>
<td><%=rs.getString("stuAge")%></td>
<td><a href="update.jsp?stuNum=<%=rs.getString("stuNum")%>">修改</a> <a href="delete1.jsp?stuNum=<%=rs.getString("stuNum")%>">删除</a></td>
</tr>

<%
rs.next();
i++;
}
%>
</table>
<p>  </p>
<p>
<%

rs.close();
stmt.close();
con.close();

%>

一共有<%=intRowCount%>个记录,分<%=intPageCount%>页显示,</p>
<p>当前页是:第<%=intPage%>页
<%

for(int j=1;j<=intPageCount;j++)
{
%>
<a href="query1.jsp?page=<%=j%>&queryName=<%=queName%>"><%=j%></a>
<%
}

%>

<a href="../welcome.jsp/index.jsp">返回首页</a>

</p>
</div>
</body>
</html>

update.jsp:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%request.setCharacterEncoding("utf-8");%>
<!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">
<!--
body {
background-color: #CCFFFF;
}
-->
</style></head>

<body>

<p> </p>
<div align="center">
<%
//String updNum=request.getParameter("stuNum");
//out.print(updNum);
%>

</div>
<form action="update1.jsp" name="update" method="post">

<div align="center">
<p> </p>
<input type="hidden" name="updNum" value="<%=request.getParameter("stuNum")%>" />
<table width="290" border="1">
<tr>
<td width="91">修改学号为:</td>
<td width="183"><input type="text" name="textfield" /></td>
</tr>
<tr>
<td>修改姓名为:</td>
<td><input type="text" name="updToName" /></td>
</tr>
<tr>
<td>修改年龄为:</td>
<td><input type="text" name="textfield2" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="提交" />
<input type="reset" name="reset" value="重置" /></td>
</tr>
</table>
</div>
</form>

</body>
</html>

update1.jsp:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<!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>update1</title>
<style type="text/css">
<!--
body {
background-color: #CCFFFF;
}
-->
</style></head>

<body>

<div align="center">
<%@include file="config.jsp"%>

<%

String updNum=request.getParameter("updNum");
String updToName=request.getParameter("updToName");
//updNum="123456";
//updToName="fffff";
Class.forName(DRIVERNAME);
Connection con=DriverManager.getConnection(URL,USERNAME,USERPASSWORD);
Statement stmt=con.createStatement();

String updsql="update user set stuName='"+updToName+"' where stuNum='"+updNum+"'";
String selsql="select * from user where stuNum='"+updNum+"'";

stmt.executeUpdate(updsql);
ResultSet rs=stmt.executeQuery(selsql);

out.print("<h2>修改结果</h2>");

%>

<table width="500" border="3" bordercolor="#FFCC66">
<tr>
<td>
<div align="center">学号 </div></td>
<td>
<div align="center">姓名 </div></td>
<td>
<div align="center">年龄 </div></td>
</tr>

<%
while(rs.next()){

%>

<tr>
<td><div align="center"><%=rs.getString("stuNum")%></div></td>
<td><div align="center"><%=rs.getString("stuName")%></div></td>
<td><div align="center"><%=rs.getString("stuAge")%></div></td>
</tr>

<%
}
%>
</table>
<%

rs.close();
stmt.close();
con.close();

%>

</div>
<p align="center">
<a href="../welcome.jsp/index.jsp">返回首页</a></p>
<p align="center">
<a href="../studentinfo/query1.jsp">显示列表</a></p>

</body>
</html>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式