使用JSP+SERVLET+JDBC实现对数据库的增删改查
首先,打开sql*plus,输入用户名(我用的scott)密码(我设置的是tiger)。
先建个表student,
Create table student (
id number(30) not nu
使用JSP+SERVLET+JDBC实现对数据库的增删改查
首先,打开sql*plus,输入用户名(我用的scott)密码(我设置的是tiger)。
先建个表student,
Create table student (
id number(30) not null primary key,
name varchar(50) ,
age number(30),
gender varchar(30),
major varchar(50) );
1,打开myeclipse(我用的是myeclipse8.5)新建一个web project
2,在project name 中输入合法名字,比如normal
3,新建的normal工程
4,在src目录下建一个包,右击src选择new在选择package
5,输入合法名字比如bean
6,在bean目录下建一个class,右击bean选择new再选择class
7,输入名字Page
完整的Page.java代码如下
package bean;
public class Page {
private int totalPage;
private int currentPage;
private int totalRecord;
private int currentRecord;
private int pageSize=8;
//获得和设置当前页
public int getCurrentPage(){
return currentPage;
}
public void setCurrentPage(int currentRecord,int pageSize){
if(currentRecord%pageSize==0){
currentPage=currentRecord/pageSize;
}
else{
currentPage=currentRecord/pageSize+1;
}
}
//获得和设置当前记录
public int getCurrentRecord(){
return currentRecord;
}
public void setCurrentRecord(int currentRecord){
this.currentRecord=currentRecord;
}
//获得和设置每页记录数量
public int getPageSize(){
return pageSize;
}
public void setPageSize(int pageSize){
this.pageSize=pageSize;
}
//获得和设置总页数
public int getTotalPage(){
return totalPage;
}
public void setTotalPage(int totalRecord,int pageSize){
if(totalRecord%pageSize==0){
totalPage=totalRecord/pageSize;
}
else{
totalPage=totalRecord/pageSize+1;
}
}
//获得和设置总记录
public int getTotalRecord(){
return totalRecord;
}
public void setTotalRecord(int totalRecord){
this.totalRecord=totalRecord;
}
}
8,用相同的方法建一个StudentInfo类
完整的StudentInfo.java代码如下
package bean;
public class StudentInfo {
privateint id; //学号
privateString name; //姓名
privateint age; //年龄
privateString gender; //性别
privateString major; //专业
publicStudentInfo(){
}
publicStudentInfo(int id,String name,int age,String gender,String major){
this.id=id;
this.name=name;
this.age=age;
this.gender=gender;
this.major=major;
}
publicint getId(){
returnid;
}
publicvoid setId(int id){
this.id=id;
}
publicString getName(){
returnname;
}
publicvoid setName(String name){
this.name=name;
}
publicint getAge(){
returnage;
}
publicvoid setAge(int age){
this.age=age;
}
publicString getGender(){
returngender;
}
publicvoid setGender(String gender){
this.gender=gender;
}
publicString getMajor(){
returnmajor;
}
publicvoid setMajor(String major){
this.major=major;
}
}
9,在src目录下添加另一个包dbservlet在该包中建立一个AllServlet类
完整的AllServlet.java代码如下
package dbservlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import bean.Page;
import bean.StudentInfo;
public class AllServlet extends HttpServlet{
/**
*
*/
privatestatic final long serialVersionUID = 1L;
//doPost方法
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
StringmethodName=request.getParameter("methodName");
intmethod=Integer.parseInt(methodName);
try{
switch(method)
{
case0:
insert(request,response);
case 1:
difpage(request,response);
break;
case2:
delete(request,response);
break;
case 3:
update(request,response);
break;
case 4:
update1(request,response);
break;
case 5:
dispatch(request,response);
break;
}
}catch (ClassNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
//doGet方法
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doPost(request,response);
}
//数据库连接方法
publicConnection connect() throws ClassNotFoundException, SQLException{
Connection conn=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
Stringpassword="tiger";
conn=DriverManager.getConnection(url,user,password);
returnconn;
}
//关闭数据库资源
publicvoid close(Statement stat,Connection conn) throws SQLException{
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
//插入方法
publicvoid insert(HttpServletRequest request, HttpServletResponse response) throwsClassNotFoundException, SQLException{
Connection conn=null;
Statement stat=null;
Stringid=request.getParameter("id");
String name=request.getParameter("name");
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String major=request.getParameter("major");
conn=connect();
stat=conn.createStatement();
stat.execute("insert intostudent(id,name,age,gender,major)values("+id+",'"+name+"',"+age+",'"+gender+"','"+major+"')");
close(stat,conn);
}
//查询方法
public ArrayList<StudentInfo> select(String id,String name) throwsClassNotFoundException, SQLException{
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
conn=connect();
stat=conn.createStatement();
ArrayList<StudentInfo> result=newArrayList<StudentInfo>();
if(id==""&&name==""){
rs=stat.executeQuery("select * fromstudent");
}
if(id!=""&&name==""){
rs=stat.executeQuery("select *from student where id="+id+"");
}
if(id==""&&name!=""){
rs=stat.executeQuery("select *from student where name='"+name+"'");
}
if(id!=""&&name!=""){
rs=stat.executeQuery("select * fromstudent where id="+id+" and name='"+name+"'");
}
while(rs.next())
{
StudentInfo st=newStudentInfo();
st.setId(rs.getInt("id"));
st.setName(rs.getString("name"));
st.setAge(rs.getInt("age"));
st.setGender(rs.getString("gender"));
st.setMajor(rs.getString("major"));
result.add(st);
}
if(rs!=null){
rs.close();
}
close(stat,conn);
return result;
}
//条件查询跳转
public void dispatch(HttpServletRequest request, HttpServletResponseresponse) throws ClassNotFoundException, SQLException, ServletException,IOException{
Stringid5=request.getParameter("id");
Stringname5=request.getParameter("name");
if(select(id5,name5).isEmpty()){
request.getRequestDispatcher("selectnothing.jsp").forward(request,response);
}
else{
request.setAttribute("result",select(id5,name5));
request.getRequestDispatcher("idnameselect.jsp").forward(request,response);
}
}
//设置分页相关参数方法
publicPage setpage(HttpServletRequest request, HttpServletResponse response) throwsClassNotFoundException, SQLException{
Stringcrd=request.getParameter("currentRecord");
//Stringid=request.getParameter("id");
// String name=request.getParameter("name");
ArrayList<StudentInfo>result=select("","");
Page pager=new Page();
pager.setTotalRecord(result.size());
pager.setTotalPage(result.size(),pager.getPageSize());
if(crd!=null)
{
int currentRecord=Integer.parseInt(crd);
pager.setCurrentRecord(currentRecord);
pager.setCurrentPage(currentRecord,pager.getPageSize());
}
return pager;
}
//获得分页显示的子集
public void difpage(HttpServletRequestrequest, HttpServletResponse response) throws ServletException, IOException,ClassNotFoundException, SQLException{
//String id=request.getParameter("id");
// String name=request.getParameter("name");
ArrayList<StudentInfo>result=select("","");
Page pager=new Page();
pager=setpage(request,response);
List<StudentInfo> subResult=null;
intcurrentRecord=pager.getCurrentRecord();
if(currentRecord==0){
if(pager.getTotalRecord()<8){
subResult=(List<StudentInfo>)result.subList(0,pager.getTotalRecord());
}
else{
subResult=(List<StudentInfo>)result.subList(0,pager.getPageSize());
}
}
else if(pager.getCurrentRecord()+pager.getPageSize()<result.size())
{
subResult=(List<StudentInfo>)result.subList(pager.getCurrentRecord(),pager.getCurrentRecord()+pager.getPageSize());
}
else
{
subResult=(List<StudentInfo>)result.subList(pager.getCurrentRecord(),result.size());
}
request.setAttribute("pager", pager);
request.setAttribute("subResult", subResult);
request.getRequestDispatcher("layout.jsp").forward(request,response);
}
//信息删除方法
public void delete(HttpServletRequest request, HttpServletResponseresponse) throws ClassNotFoundException, SQLException, ServletException,IOException{
Connection conn=null;
Statement stat=null;
conn=connect();
stat=conn.createStatement();
Stringid2=request.getParameter("id");
stat.execute("deletefrom student where id="+id2+"");
request.getRequestDispatcher("delete.jsp").forward(request,response);
}
//信息修改方法
public void update1(HttpServletRequest request, HttpServletResponseresponse) throws ClassNotFoundException, SQLException, ServletException,IOException{
Stringid4=request.getParameter("id");
request.setAttribute("result",select(id4,""));
request.getRequestDispatcher("update1.jsp").forward(request,response);
}
public void update(HttpServletRequest request, HttpServletResponseresponse) throws ClassNotFoundException, SQLException, ServletException,IOException{
Connection conn=null;
Statement stat=null;
String id3=request.getParameter("id");
String name3=request.getParameter("name");
String age3=request.getParameter("age");
String gender3=request.getParameter("gender");
String major3=request.getParameter("major");
conn=connect();
stat=conn.createStatement();
stat.execute("updatestudent setid="+id3+",name='"+name3+"',age="+age3+",gender='"+gender3+"',major='"+major3+"'where id="+id3+"");
request.setAttribute("result",select(id3,""));
request.getRequestDispatcher("update.jsp").forward(request,response);
}
}
10,在webRoot目录下添加以下.jsp文件
10.1 putin.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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
<scripttype="text/javascript"">
function validate()
{
var id=document.forms[0].id.value;
var name=document.forms[0].name.value;
var age=document.forms[0].age.value;
var major=document.forms[0].major.value;
if(id<=0){
alert("学号不能为空,请输入学号!");
return false;
}
else if(name.length<=0){
alert("姓名不能为空,请输入姓名!");
return false;
}
else if(age<=0){
alert("请输入合法年龄!");
return false;
}
else if(major.length<=0){
alert("专业不能为空,请输入所学专业!");
return false;
}
else{
return true;
}
//document.getElementById("form").submit();
}
</script>
</head>
<body>
<br>
<center>
<h2>学生信息输入</h2><hr>
<form action="AllServlet"method="post" id="form" onSubmit="returnvalidate()" >
<input type="hidden"name="methodName" value="0"/>
<h4> 学号:<inputtype="text" name="id" class="{required:true}"title="学号必须为数字"></input><br></h4>
<h4> 姓名:<inputtype="text" name="name"title="姓名不能为空"></input><br></h4>
<h4> 年龄:<inputtype="text" name="age"title="年龄必须为数字"></input><br></h4>
<h4> 性别:<inputtype="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br></h4>
<h4> 专业:<inputtype="text" name="major"title="专业不能为空"></input><br></h4>
<input type="submit" value="提交"/>
</form>
<br>
<a href="AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%=""%>">查看已输入信息</a>
</center>
</body>
</html>
10.2 layout.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ pageimport="bean.StudentInfo" %>
<%@ page import="bean.Page"%>
<%
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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
<scripttype="text/javascript">
function confirmdialog(){
if(window.confirm("您确定要删除此条信息?")){
return true;
}
else{
// alert("取消删除!");
return false;
}
}
</script>
</head>
<body>
<br>
<h1>学生信息</h1> <br> <hr>
<br>
<h3>全部学生信息如下</h3>
<table width="510" border="100" cellSpacing=1style="border: 1pt dashed ;font-size: 15pt;"height="31">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>专业</td>
</tr>
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
Page pager=(Page)request.getAttribute("pager");
List<StudentInfo>subResult=(List<StudentInfo>)request.getAttribute("subResult");
if(!subResult.isEmpty()){
for(int i=0;i<subResult.size();i++){
StudentInfo st=subResult.get(i);
out.print("<tr>");
out.print("<td>"+st.getId()+"</td>");
out.print("<td>"+st.getName()+"</td>");
out.print("<td>"+st.getAge()+"</td>");
out.print("<td>"+st.getGender()+"</td>");
out.print("<td>"+st.getMajor()+"</td>");
%>
<td><a href="AllServlet?id=<%=st.getId()%>&methodName=<%=2 %>" onclick="returnconfirmdialog()">删除</a></td>
<td><a href="AllServlet?id=<%=st.getId()%>&name=<%="" %>&methodName=<%=4 %>">修改</a></td>
<%
out.print("</tr>");
}
}
%>
</table>
<span><font size="2">总<%=pager.getTotalRecord() %>条记录|总<%=pager.getTotalPage() %>页
|当前<%=pager.getCurrentPage()+1%>页|每页<%=pager.getPageSize() %>条|
<%
int last=pager.getCurrentRecord()-pager.getPageSize();
int next=pager.getCurrentRecord()+pager.getPageSize();
int currentRecord;
if(last<0){
out.println("首页|");
}
else{
out.print("<a href='AllServlet?currentRecord="+last+"&methodName=1'>上一页</a>|");
}
if(next>=pager.getTotalRecord()){
out.println("尾页|");
}
else{
out.print("<ahref='AllServlet?currentRecord="+next+"&methodName=1'>下一页</a>|");
}
%>
</font>
</span>
<br>
<form action="AllServlet" method="post">
<input type="hidden" name="methodName"value="5"/>
<h3>按学号姓名查询:</h3>
学号:<input type="text"name="id" value=""title="学号必须为数字"></input>
姓名:<input type="text"name="name" value="" title=""></input>
<input type="submit" value="查询" />
</form>
<br>
<h3><a href=putin.jsp>返回信息输入页面</a></h3>
<br>
</body>
</html>
10.3 update1.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ pageimport="bean.StudentInfo" %>
<%
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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<br>
<h2>学生信息</h2> <hr>
<br>
<h3>要修改的学生信息如下</h3>
<table width="496" border="100" cellSpacing=1style="border: 1pt dashed ;font-size: 15pt;"height="31">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>专业</td>
</tr>
<%
int id=0;
ArrayList<StudentInfo> result=new ArrayList<StudentInfo>();
result=(ArrayList<StudentInfo>)request.getAttribute("result");
if(!result.isEmpty()){
for(int i=0;i<result.size();i++){
StudentInfo st=result.get(i);
id=st.getId();
out.print("<tr>");
out.print("<td>"+st.getId()+"</td>");
out.print("<td>"+st.getName()+"</td>");
out.print("<td>"+st.getAge()+"</td>");
out.print("<td>"+st.getGender()+"</td>");
out.print("<td>"+st.getMajor()+"</td>");
out.print("</tr>");
}
}
%>
</table>
<h3>将学生信息更改为:</h3>
<form action="AllServlet"method="post" >
<input type="hidden"name="methodName" value="3"/>
<h4> 学号:<input type="text"name="id"value="<%=id %>" title="学号不能改变"></input><br></h4>
<h4> 姓名:<inputtype="text" name="name"title="姓名不能为空"></input><br></h4>
<h4> 年龄:<inputtype="text" name="age"title="年龄不能为空"></input><br></h4>
<h4> 性别:<inputtype="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br></h4>
<h4> 专业:<inputtype="text" name="major"title="专业不能为空"></input><br></h4>
<input type="submit" value="修改"/>
</form>
<br>
<h3><a href=putin.jsp>返回信息输入页面</a></h3>
<h3><ahref=AllServlet?methodName=<%=1 %>&id=<%=""%>&name=<%="" %>>返回信息查询页面</a></h3>
</body>
</html>
10.4 update.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ pageimport="bean.StudentInfo" %>
<%
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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<br>
<h3>修改后的信息为:</h3>
<hr>
<br>
<br>
<table width="450"border="100" cellSpacing=1 style="font-size:15pt;border:dashed 1pt">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>专业</td>
</tr>
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
ArrayList<StudentInfo> result=new ArrayList<StudentInfo>();
result=(ArrayList<StudentInfo>)request.getAttribute("result");
if(!result.isEmpty()){
for(int i=0;i<result.size();i++){
StudentInfo st=result.get(i);
out.print("<tr>");
out.print("<td>"+st.getId()+"</td>");
out.print("<td>"+st.getName()+"</td>");
out.print("<td>"+st.getAge()+"</td>");
out.print("<td>"+st.getGender()+"</td>");
out.print("<td>"+st.getMajor()+"</td>");
out.print("</tr>");
}
}
%>
</table>
<br>
<br>
<h3><a href=putin.jsp>返回信息输入页面</a></h3>
<h3><a href=AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></h3>
</body>
</html>
10.5 deldete.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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<%
out.print("<center><br><br><h3>删除成功!</h3></center>");
%>
<br>
<br>
<center> <a href=putin.jsp>返回信息输入页面</a> <a href=AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></center>
</body>
</html>
10.6 idnameselect.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ pageimport="bean.StudentInfo" %>
<%
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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<br>
<h3>符合条件的学生信息</h3><hr>
<br>
<table width="450" border="100" cellSpacing=1style="font-size:15pt;border:dashed1pt">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>专业</td>
</tr>
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
ArrayList<StudentInfo> result=newArrayList<StudentInfo>();
result=(ArrayList<StudentInfo>)request.getAttribute("result");
if(!result.isEmpty()){
for(int i=0;i<result.size();i++){
StudentInfo st=result.get(i);
out.print("<tr>");
out.print("<td>"+st.getId()+"</td>");
out.print("<td>"+st.getName()+"</td>");
out.print("<td>"+st.getAge()+"</td>");
out.print("<td>"+st.getGender()+"</td>");
out.print("<td>"+st.getMajor()+"</td>");
%>
<td><a href="AllServlet?id=<%=st.getId()%>&&methodName=<%=2 %>">删除</a></td>
<td><a href="AllServlet?id=<%=st.getId()%>&&methodName=<%=4 %>">修改</a></td>
<%
out.print("</tr>");
}
}
%>
</table>
<br>
<br>
<h4><a href=AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></h4>
</body>
</html>
10.7 selectnothing.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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<%
out.print("<center><br><br><h3>没有符合此条件的信息!</h3></center>");
%>
<br>
<br>
<center> <a href=putin.jsp>返回信息输入页面</a> <a href=AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></center>
</body>
</html>
11, 修改web.xml文件(在WEB—INF目录下)
将index.jsp改为putint.jsp,修改或如下
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>putin.jsp</welcome-file>
</welcome-file-list>
</web-app>
ll primary key,
name varchar(50) ,
age number(30),
gender varchar(30),
major varchar(50) );
1,打开myeclipse(我用的是myeclipse8.5)新建一个web project
2,在project name 中输入合法名字,比如normal
3,新建的normal工程
4,在src目录下建一个包,右击src选择new在选择package
5,输入合法名字比如bean
6,在bean目录下建一个class,右击bean选择new再选择class
7,输入名字Page
完整的Page.java代码如下
package bean;
public class Page {
private int totalPage;
private int currentPage;
private int totalRecord;
private int currentRecord;
private int pageSize=8;
//获得和设置当前页
public int getCurrentPage(){
return currentPage;
}
public void setCurrentPage(int currentRecord,int pageSize){
if(currentRecord%pageSize==0){
currentPage=currentRecord/pageSize;
}
else{
currentPage=currentRecord/pageSize+1;
}
}
//获得和设置当前记录
public int getCurrentRecord(){
return currentRecord;
}
public void setCurrentRecord(int currentRecord){
this.currentRecord=currentRecord;
}
//获得和设置每页记录数量
public int getPageSize(){
return pageSize;
}
public void setPageSize(int pageSize){
this.pageSize=pageSize;
}
//获得和设置总页数
public int getTotalPage(){
return totalPage;
}
public void setTotalPage(int totalRecord,int pageSize){
if(totalRecord%pageSize==0){
totalPage=totalRecord/pageSize;
}
else{
totalPage=totalRecord/pageSize+1;
}
}
//获得和设置总记录
public int getTotalRecord(){
return totalRecord;
}
public void setTotalRecord(int totalRecord){
this.totalRecord=totalRecord;
}
}
8,用相同的方法建一个StudentInfo类
完整的StudentInfo.java代码如下
package bean;
public class StudentInfo {
privateint id; //学号
privateString name; //姓名
privateint age; //年龄
privateString gender; //性别
privateString major; //专业
publicStudentInfo(){
}
publicStudentInfo(int id,String name,int age,String gender,String major){
this.id=id;
this.name=name;
this.age=age;
this.gender=gender;
this.major=major;
}
publicint getId(){
returnid;
}
publicvoid setId(int id){
this.id=id;
}
publicString getName(){
returnname;
}
publicvoid setName(String name){
this.name=name;
}
publicint getAge(){
returnage;
}
publicvoid setAge(int age){
this.age=age;
}
publicString getGender(){
returngender;
}
publicvoid setGender(String gender){
this.gender=gender;
}
publicString getMajor(){
returnmajor;
}
publicvoid setMajor(String major){
this.major=major;
}
}
9,在src目录下添加另一个包dbservlet在该包中建立一个AllServlet类
完整的AllServlet.java代码如下
package dbservlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import bean.Page;
import bean.StudentInfo;
public class AllServlet extends HttpServlet{
/**
*
*/
privatestatic final long serialVersionUID = 1L;
//doPost方法
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
StringmethodName=request.getParameter("methodName");
intmethod=Integer.parseInt(methodName);
try{
switch(method)
{
case0:
insert(request,response);
case 1:
difpage(request,response);
break;
case2:
delete(request,response);
break;
case 3:
update(request,response);
break;
case 4:
update1(request,response);
break;
case 5:
dispatch(request,response);
break;
}
}catch (ClassNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
//doGet方法
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doPost(request,response);
}
//数据库连接方法
publicConnection connect() throws ClassNotFoundException, SQLException{
Connection conn=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
Stringpassword="tiger";
conn=DriverManager.getConnection(url,user,password);
returnconn;
}
//关闭数据库资源
publicvoid close(Statement stat,Connection conn) throws SQLException{
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
//插入方法
publicvoid insert(HttpServletRequest request, HttpServletResponse response) throwsClassNotFoundException, SQLException{
Connection conn=null;
Statement stat=null;
Stringid=request.getParameter("id");
String name=request.getParameter("name");
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String major=request.getParameter("major");
conn=connect();
stat=conn.createStatement();
stat.execute("insert intostudent(id,name,age,gender,major)values("+id+",'"+name+"',"+age+",'"+gender+"','"+major+"')");
close(stat,conn);
}
//查询方法
public ArrayList<StudentInfo> select(String id,String name) throwsClassNotFoundException, SQLException{
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
conn=connect();
stat=conn.createStatement();
ArrayList<StudentInfo> result=newArrayList<StudentInfo>();
if(id==""&&name==""){
rs=stat.executeQuery("select * fromstudent");
}
if(id!=""&&name==""){
rs=stat.executeQuery("select *from student where id="+id+"");
}
if(id==""&&name!=""){
rs=stat.executeQuery("select *from student where name='"+name+"'");
}
if(id!=""&&name!=""){
rs=stat.executeQuery("select * fromstudent where id="+id+" and name='"+name+"'");
}
while(rs.next())
{
StudentInfo st=newStudentInfo();
st.setId(rs.getInt("id"));
st.setName(rs.getString("name"));
st.setAge(rs.getInt("age"));
st.setGender(rs.getString("gender"));
st.setMajor(rs.getString("major"));
result.add(st);
}
if(rs!=null){
rs.close();
}
close(stat,conn);
return result;
}
//条件查询跳转
public void dispatch(HttpServletRequest request, HttpServletResponseresponse) throws ClassNotFoundException, SQLException, ServletException,IOException{
Stringid5=request.getParameter("id");
Stringname5=request.getParameter("name");
if(select(id5,name5).isEmpty()){
request.getRequestDispatcher("selectnothing.jsp").forward(request,response);
}
else{
request.setAttribute("result",select(id5,name5));
request.getRequestDispatcher("idnameselect.jsp").forward(request,response);
}
}
//设置分页相关参数方法
publicPage setpage(HttpServletRequest request, HttpServletResponse response) throwsClassNotFoundException, SQLException{
Stringcrd=request.getParameter("currentRecord");
//Stringid=request.getParameter("id");
// String name=request.getParameter("name");
ArrayList<StudentInfo>result=select("","");
Page pager=new Page();
pager.setTotalRecord(result.size());
pager.setTotalPage(result.size(),pager.getPageSize());
if(crd!=null)
{
int currentRecord=Integer.parseInt(crd);
pager.setCurrentRecord(currentRecord);
pager.setCurrentPage(currentRecord,pager.getPageSize());
}
return pager;
}
//获得分页显示的子集
public void difpage(HttpServletRequestrequest, HttpServletResponse response) throws ServletException, IOException,ClassNotFoundException, SQLException{
//String id=request.getParameter("id");
// String name=request.getParameter("name");
ArrayList<StudentInfo>result=select("","");
Page pager=new Page();
pager=setpage(request,response);
List<StudentInfo> subResult=null;
intcurrentRecord=pager.getCurrentRecord();
if(currentRecord==0){
if(pager.getTotalRecord()<8){
subResult=(List<StudentInfo>)result.subList(0,pager.getTotalRecord());
}
else{
subResult=(List<StudentInfo>)result.subList(0,pager.getPageSize());
}
}
else if(pager.getCurrentRecord()+pager.getPageSize()<result.size())
{
subResult=(List<StudentInfo>)result.subList(pager.getCurrentRecord(),pager.getCurrentRecord()+pager.getPageSize());
}
else
{
subResult=(List<StudentInfo>)result.subList(pager.getCurrentRecord(),result.size());
}
request.setAttribute("pager", pager);
request.setAttribute("subResult", subResult);
request.getRequestDispatcher("layout.jsp").forward(request,response);
}
//信息删除方法
public void delete(HttpServletRequest request, HttpServletResponseresponse) throws ClassNotFoundException, SQLException, ServletException,IOException{
Connection conn=null;
Statement stat=null;
conn=connect();
stat=conn.createStatement();
Stringid2=request.getParameter("id");
stat.execute("deletefrom student where id="+id2+"");
request.getRequestDispatcher("delete.jsp").forward(request,response);
}
//信息修改方法
public void update1(HttpServletRequest request, HttpServletResponseresponse) throws ClassNotFoundException, SQLException, ServletException,IOException{
Stringid4=request.getParameter("id");
request.setAttribute("result",select(id4,""));
request.getRequestDispatcher("update1.jsp").forward(request,response);
}
public void update(HttpServletRequest request, HttpServletResponseresponse) throws ClassNotFoundException, SQLException, ServletException,IOException{
Connection conn=null;
Statement stat=null;
String id3=request.getParameter("id");
String name3=request.getParameter("name");
String age3=request.getParameter("age");
String gender3=request.getParameter("gender");
String major3=request.getParameter("major");
conn=connect();
stat=conn.createStatement();
stat.execute("updatestudent setid="+id3+",name='"+name3+"',age="+age3+",gender='"+gender3+"',major='"+major3+"'where id="+id3+"");
request.setAttribute("result",select(id3,""));
request.getRequestDispatcher("update.jsp").forward(request,response);
}
}
10,在webRoot目录下添加以下.jsp文件
10.1 putin.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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
<scripttype="text/javascript"">
function validate()
{
var id=document.forms[0].id.value;
var name=document.forms[0].name.value;
var age=document.forms[0].age.value;
var major=document.forms[0].major.value;
if(id<=0){
alert("学号不能为空,请输入学号!");
return false;
}
else if(name.length<=0){
alert("姓名不能为空,请输入姓名!");
return false;
}
else if(age<=0){
alert("请输入合法年龄!");
return false;
}
else if(major.length<=0){
alert("专业不能为空,请输入所学专业!");
return false;
}
else{
return true;
}
//document.getElementById("form").submit();
}
</script>
</head>
<body>
<br>
<center>
<h2>学生信息输入</h2><hr>
<form action="AllServlet"method="post" id="form" onSubmit="returnvalidate()" >
<input type="hidden"name="methodName" value="0"/>
<h4> 学号:<inputtype="text" name="id" class="{required:true}"title="学号必须为数字"></input><br></h4>
<h4> 姓名:<inputtype="text" name="name"title="姓名不能为空"></input><br></h4>
<h4> 年龄:<inputtype="text" name="age"title="年龄必须为数字"></input><br></h4>
<h4> 性别:<inputtype="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br></h4>
<h4> 专业:<inputtype="text" name="major"title="专业不能为空"></input><br></h4>
<input type="submit" value="提交"/>
</form>
<br>
<a href="AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%=""%>">查看已输入信息</a>
</center>
</body>
</html>
10.2 layout.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ pageimport="bean.StudentInfo" %>
<%@ page import="bean.Page"%>
<%
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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
<scripttype="text/javascript">
function confirmdialog(){
if(window.confirm("您确定要删除此条信息?")){
return true;
}
else{
// alert("取消删除!");
return false;
}
}
</script>
</head>
<body>
<br>
<h1>学生信息</h1> <br> <hr>
<br>
<h3>全部学生信息如下</h3>
<table width="510" border="100" cellSpacing=1style="border: 1pt dashed ;font-size: 15pt;"height="31">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>专业</td>
</tr>
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
Page pager=(Page)request.getAttribute("pager");
List<StudentInfo>subResult=(List<StudentInfo>)request.getAttribute("subResult");
if(!subResult.isEmpty()){
for(int i=0;i<subResult.size();i++){
StudentInfo st=subResult.get(i);
out.print("<tr>");
out.print("<td>"+st.getId()+"</td>");
out.print("<td>"+st.getName()+"</td>");
out.print("<td>"+st.getAge()+"</td>");
out.print("<td>"+st.getGender()+"</td>");
out.print("<td>"+st.getMajor()+"</td>");
%>
<td><a href="AllServlet?id=<%=st.getId()%>&methodName=<%=2 %>" onclick="returnconfirmdialog()">删除</a></td>
<td><a href="AllServlet?id=<%=st.getId()%>&name=<%="" %>&methodName=<%=4 %>">修改</a></td>
<%
out.print("</tr>");
}
}
%>
</table>
<span><font size="2">总<%=pager.getTotalRecord() %>条记录|总<%=pager.getTotalPage() %>页
|当前<%=pager.getCurrentPage()+1%>页|每页<%=pager.getPageSize() %>条|
<%
int last=pager.getCurrentRecord()-pager.getPageSize();
int next=pager.getCurrentRecord()+pager.getPageSize();
int currentRecord;
if(last<0){
out.println("首页|");
}
else{
out.print("<a href='AllServlet?currentRecord="+last+"&methodName=1'>上一页</a>|");
}
if(next>=pager.getTotalRecord()){
out.println("尾页|");
}
else{
out.print("<ahref='AllServlet?currentRecord="+next+"&methodName=1'>下一页</a>|");
}
%>
</font>
</span>
<br>
<form action="AllServlet" method="post">
<input type="hidden" name="methodName"value="5"/>
<h3>按学号姓名查询:</h3>
学号:<input type="text"name="id" value=""title="学号必须为数字"></input>
姓名:<input type="text"name="name" value="" title=""></input>
<input type="submit" value="查询" />
</form>
<br>
<h3><a href=putin.jsp>返回信息输入页面</a></h3>
<br>
</body>
</html>
10.3 update1.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ pageimport="bean.StudentInfo" %>
<%
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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<br>
<h2>学生信息</h2> <hr>
<br>
<h3>要修改的学生信息如下</h3>
<table width="496" border="100" cellSpacing=1style="border: 1pt dashed ;font-size: 15pt;"height="31">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>专业</td>
</tr>
<%
int id=0;
ArrayList<StudentInfo> result=new ArrayList<StudentInfo>();
result=(ArrayList<StudentInfo>)request.getAttribute("result");
if(!result.isEmpty()){
for(int i=0;i<result.size();i++){
StudentInfo st=result.get(i);
id=st.getId();
out.print("<tr>");
out.print("<td>"+st.getId()+"</td>");
out.print("<td>"+st.getName()+"</td>");
out.print("<td>"+st.getAge()+"</td>");
out.print("<td>"+st.getGender()+"</td>");
out.print("<td>"+st.getMajor()+"</td>");
out.print("</tr>");
}
}
%>
</table>
<h3>将学生信息更改为:</h3>
<form action="AllServlet"method="post" >
<input type="hidden"name="methodName" value="3"/>
<h4> 学号:<input type="text"name="id"value="<%=id %>" title="学号不能改变"></input><br></h4>
<h4> 姓名:<inputtype="text" name="name"title="姓名不能为空"></input><br></h4>
<h4> 年龄:<inputtype="text" name="age"title="年龄不能为空"></input><br></h4>
<h4> 性别:<inputtype="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br></h4>
<h4> 专业:<inputtype="text" name="major"title="专业不能为空"></input><br></h4>
<input type="submit" value="修改"/>
</form>
<br>
<h3><a href=putin.jsp>返回信息输入页面</a></h3>
<h3><ahref=AllServlet?methodName=<%=1 %>&id=<%=""%>&name=<%="" %>>返回信息查询页面</a></h3>
</body>
</html>
10.4 update.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ pageimport="bean.StudentInfo" %>
<%
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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<br>
<h3>修改后的信息为:</h3>
<hr>
<br>
<br>
<table width="450"border="100" cellSpacing=1 style="font-size:15pt;border:dashed 1pt">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>专业</td>
</tr>
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
ArrayList<StudentInfo> result=new ArrayList<StudentInfo>();
result=(ArrayList<StudentInfo>)request.getAttribute("result");
if(!result.isEmpty()){
for(int i=0;i<result.size();i++){
StudentInfo st=result.get(i);
out.print("<tr>");
out.print("<td>"+st.getId()+"</td>");
out.print("<td>"+st.getName()+"</td>");
out.print("<td>"+st.getAge()+"</td>");
out.print("<td>"+st.getGender()+"</td>");
out.print("<td>"+st.getMajor()+"</td>");
out.print("</tr>");
}
}
%>
</table>
<br>
<br>
<h3><a href=putin.jsp>返回信息输入页面</a></h3>
<h3><a href=AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></h3>
</body>
</html>
10.5 deldete.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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<%
out.print("<center><br><br><h3>删除成功!</h3></center>");
%>
<br>
<br>
<center> <a href=putin.jsp>返回信息输入页面</a> <a href=AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></center>
</body>
</html>
10.6 idnameselect.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ pageimport="bean.StudentInfo" %>
<%
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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<br>
<h3>符合条件的学生信息</h3><hr>
<br>
<table width="450" border="100" cellSpacing=1style="font-size:15pt;border:dashed1pt">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>专业</td>
</tr>
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
ArrayList<StudentInfo> result=newArrayList<StudentInfo>();
result=(ArrayList<StudentInfo>)request.getAttribute("result");
if(!result.isEmpty()){
for(int i=0;i<result.size();i++){
StudentInfo st=result.get(i);
out.print("<tr>");
out.print("<td>"+st.getId()+"</td>");
out.print("<td>"+st.getName()+"</td>");
out.print("<td>"+st.getAge()+"</td>");
out.print("<td>"+st.getGender()+"</td>");
out.print("<td>"+st.getMajor()+"</td>");
%>
<td><a href="AllServlet?id=<%=st.getId()%>&&methodName=<%=2 %>">删除</a></td>
<td><a href="AllServlet?id=<%=st.getId()%>&&methodName=<%=4 %>">修改</a></td>
<%
out.print("</tr>");
}
}
%>
</table>
<br>
<br>
<h4><a href=AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></h4>
</body>
</html>
10.7 selectnothing.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>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description" content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<body>
<%
out.print("<center><br><br><h3>没有符合此条件的信息!</h3></center>");
%>
<br>
<br>
<center> <a href=putin.jsp>返回信息输入页面</a> <a href=AllServlet?methodName=<%=1%>&id=<%="" %>&name=<%="" %>>返回信息查询页面</a></center>
</body>
</html>
11, 修改web.xml文件(在WEB—INF目录下)
将index.jsp改为putint.jsp,修改或如下
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>putin.jsp</welcome-file>
</welcome-file-list>
</web-app>
本文介绍如何使用JSP、SERVLET和JDBC技术实现对学生信息数据库的基本操作,包括创建表结构、增删改查等功能,并通过分页展示查询结果。
1万+

被折叠的 条评论
为什么被折叠?



