使用Jsp編寫網頁並實現資料庫的增刪改查

使用Jsp編寫網頁並實現資料庫的增刪改查

首先jsp 和Servlet語句基本一樣,這裡的實現需要用到上篇http://blog.csdn.net/qq_32539825/article/details/70494788裡的部分內容
包括UserCoon.java UserDao.java User.java

在這裡使用jsp語句也實現了分頁內容。

這裡寫圖片描述

1 login.jsp

<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.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>
<form action="servlet/Login" method="post">  
使用者名稱:<input type="text" name="username" value=""><br>  
密 碼:<input type="password" name="password" value=""><br>  
使用者型別:  
<select name="type">
<option value="管理員">管理員</option>
<option value="普通使用者">普通使用者</option>
</select><br>  
<input type="submit" value="提交">  
<input type="reset" value="取消">  
</form>  
</body>
</html>
  1. dologin.jsp
    首先用javabean得到login中的內容,並呼叫Insert.java插入到資料庫
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>
<%
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 'dologin.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>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="user" class="nuc.test.user.User">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>
<%
UserDao usera=new UserDao();
usera.Insert(user);
%>
<body>
</html>

3 queryBena.java
查詢資料庫中的內容
這裡寫圖片描述

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>
<%
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 'queryBean.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>
<%
UserDao user=new UserDao();
User usera=new User();
ResultSet rst=user.Query();
%>
<table border=1>
<tr><td>使用者名稱</td><td>密碼</td><td>使用者型別</td><td colspan="2" align="center">資料操作</td></tr>
<%while(rst.next()) {%>
<tr><td><%=rst.getString("username") %></td><td><%=rst.getString("password") %></td><td><%=rst.getString("type") %></td><td><a href="deleteBean.jsp?id=<%=rst.getString("id")%>">刪除操作</a></td><td><a href="updateBean.jsp?id=<%=rst.getString("id")%>">更新操作</a></td></tr>
<%} %>
</table>
</body>
</html>

4 deleteBean.jsp
點選查詢頁面上的刪除鍵 便會超連結到deleteBean.jsp並帶著值

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>
<%
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 'deleteBean.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>
<%  
UserDao user=new UserDao();
User usera=new User();
usera.setId(request.getParameter("id"));
int rest=user.Delete(usera);
%>
</body>
</html>

5 updateBean.jsp
首先將需要更新的一條內容輸出到網頁上,修改後 點提交 呼叫doUpdateBean.jsp更新

這裡寫圖片描述

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'updateBean.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>
<%
request.setCharacterEncoding("utf-8");
UserDao user=new UserDao();
User usera=new User();
usera.setId(request.getParameter("id"));
ResultSet rs=user.Select(usera);
if(rs.next()){
%>
<form action="doUpdateBean.jsp?id=<%=rs.getString("id")%>" method="post">
使用者名稱:<input type="text" value="<%=rs.getString("username") %>" name="username"><br>
密 碼:<input type="text" value="<%=rs.getString("password") %>" name="password"><br>
使用者型別:<select name="type">
<option value="管理員">管理員</option>
<option value="普通使用者">普通使用者</option>
</select><br>
<input type="submit" value="提交">
<input type="reset" value="取消">
</form>
%} %>
</body>
</html>

6 doUpdateBean.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.UserDao"  %>
<%
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 'doUpdateBean.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>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="use" class="nuc.test.user.User">
<jsp:setProperty name="use" property="*"/>
</jsp:useBean>
<%
UserDao user=new UserDao();
//User usera=new User();
use.setId(request.getParameter("id"));
int rs=0;
rs=user.Update(use);
%>
<jsp:forward page="queryBean.jsp"/>
</body>
</html>

7 接下來的下個是用來顯示分頁的 並且設定每頁顯示5條資訊

Bar.jsp
用來編寫分頁資訊

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.*"  %>
<%@page import="java.util.*"  %>
<%
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 'Bar.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>
<%
int pages;
int currpage=1;
if(request.getParameter("page")!=null){
currpage=Integer.parseInt(request.getParameter("page"));
}
Find find=new Find();
int count=find.SelectCount();
if(count%User.PAGESIZE==0){
pages=count/User.PAGESIZE;
}
else{
pages=count/User.PAGESIZE 1;
}
StringBuffer sb=new StringBuffer();
for(int i=1;i<=pages;i  ){
if(i==currpage){
sb.append("[" i "]");
}else{
sb.append("<a href='showPage.jsp?page=" i "'>" i "</a>");
}
sb.append("  ");
}
out.print(sb);
request.setAttribute("bar",sb.toString());
%>
</body>
</html>

ShowPage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="nuc.test.user.User" %>
<%@page import="nuc.test.Dao.*"  %>
<%@page import="java.util.*"  %>
<%
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 'showPage.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>
<table align="center" width="800" border="1">
<tr>
<td align="center" colspan="5">
<h2>所有使用者資訊</h2>
</td>
</tr>
<tr align="center">
<td>使用者名稱</td><td>密碼</td><td>使用者型別</td><td colspan="2">操作方法</td>
</tr>
<%
int currpage=1;
if(request.getParameter("page")!=null){
currpage=Integer.parseInt(request.getParameter("page"));
}
Find find=new Find();
List<User> listall=new ArrayList<User>();
listall=find.Selectcontent(currpage);
Iterator<User> it=listall.iterator();
while(it.hasNext()){
User usera=it.next();
%>
<tr align="center">
<td><%=usera.getUsername() %></td>
<td><%=usera.getPassword() %></td>
<td><%=usera.getType() %></td>
<td><a href="servlet/Update?id=<%=usera.getId()%>">修改</a></td>
<td><a href="servlet/Delete?id=<%=usera.getId()%>">刪除</a></td>
</tr>
<%} %>
<tr>
<td align="center" colspan="3">
<jsp:include page="Bar.jsp"/>
</td>
</tr>
<tr align="center"><td colspan="5"><a href="first.jsp">新增使用者</a></td></tr>
</table>
</body>
</html>

這裡寫圖片描述