一.问题概述

        实现了数据库的增删改查和分页显示。

        分页显示:mysql

String cmd = "select * from t_user limit "+firstResult+","+pageSize;
mysql> SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
--为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
--如果只给定一个参数,它表示返回最大的记录行数目:
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行
--换句话说,LIMIT n 等价于 LIMIT 0,n。 

                  分页显示:sql server

select top 10 * from table --这是选出前10条
select top 100 *from table where id not in(select Top 50 id from table)
--这是选出第51到100条,使用存储过程也好。

        文档结构如下:

           

二.代码实现

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="insert.jsp" method="post">
<table>
	<tr>
		<td>用户名:</td>
		<td><input type="text" name="username"></td>
	</tr>
	<tr>
		<td>密码:</td>
		<td><input type="text" name="password"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="提交"/> </td>
	</tr>
</table>

</form>
</body>
</html>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String username = request.getParameter("username");
	String password = request.getParameter("password");

	String cmd = "insert into t_user (username,password) values('"+username+"','"+password+"')";
	int count = 0;
	try{
		Class.forName("com.mysql.jdbc.Driver");
		String url= "jdbc:mysql://127.0.0.1/db_user";
		String uid = "root";
		String pwd = "root";
		Connection conn = DriverManager.getConnection(url,uid,pwd);
		Statement st = conn.createStatement();
		count = st.executeUpdate(cmd);
		conn.close();
	}catch(Exception e){
		e.printStackTrace();
	}
	if(count>0) response.sendRedirect("list.jsp");
	else response.sendRedirect("error.jsp");
%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="fill.html">添加新人员</a>
	<br />
	<table border="1">
		<tr>
			<td>ID</td>
			<td>username</td>
			<td>password</td>
			<td>操作</td>
		</tr>
		<%
	String pageNum = request.getParameter("pageNum");
	int rowCount = 0;//数据库表总纪录数
	int pageSize = 3;//每页显示纪录数,假设为3
	int pageCount =0;//共有多少页
	//pn做pageNum的中间变量使用的
	int pn = 1;
	if(null != pageNum && pageNum.length()>0)
		pn = Integer.parseInt(pageNum);


	try{
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://127.0.0.1/db_user";
		String uid = "root";
		String pwd = "root";
		Connection conn = DriverManager.getConnection(url,uid,pwd);

		Statement st2 = conn.createStatement();
		String cmd2 = "select count(*) from t_user ";
		ResultSet rs2 = st2.executeQuery(cmd2);
		if(rs2.next()){
			rowCount = rs2.getInt(1);
		}
		rs2.close();

		pageCount = rowCount%pageSize ==0 ? (rowCount/pageSize) : (rowCount/pageSize+1);
		if(pn < 1 ) pn = 1;
		if(pn >pageCount) pn = pageCount;

		int firstResult = (pn-1)*pageSize;


		Statement st = conn.createStatement();
		/*MySql数据库有limit关键字,注意用法
		第一个参数是从0开始的,不是一
		SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
		*/
		String cmd = "select * from t_user limit "+firstResult+","+pageSize;
		ResultSet rs = st.executeQuery(cmd);
		while(rs.next()){

%>
		<tr>
			<td><%=rs.getInt(1) %></td>
			<td><%=rs.getString(2) %></td>
			<td><%=rs.getString(3) %></td>
			<td>
				<a href="edit.jsp?id=<%=rs.getInt(1) %>">编辑</a>
				<a href="delete.jsp?id=<%=rs.getInt(1) %>"> 删除</a>
			</td>
		</tr>
		<%
		}
		conn.close();

	}catch(Exception e){
		e.printStackTrace();
	}
%>
		<tr>
			<td colspan="4">共有<%=rowCount %>条记录,每页显示<%=pageSize %>条记录,共显示<%=pageCount %>页,当前是第<%=pn %>页。
			</td>

		</tr>
		<tr>
			<td colspan="4"><a href="list.jsp?pageNum=1">第一页</a>
				<a href="list.jsp?pageNum=<%=pn-1 %>">上一页</a>
				<a href="list.jsp?pageNum=<%=pn+1 %>">下一页</a>
				<a href="list.jsp?pageNum=<%=pageCount %>">最后页</a>
			</td>
		</tr>
	</table>
</body>
</html>
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("id");
String username = "";
String password = "";
try{
	Class.forName("com.mysql.jdbc.Driver");
	String url= "jdbc:mysql://127.0.0.1/db_user";
	String uid = "root";
	String pwd = "root";
	Connection conn = DriverManager.getConnection(url,uid,pwd);
	Statement st = conn.createStatement();
	String cmd = "select * from t_user where id="+id;
	ResultSet rs = st.executeQuery(cmd);
	if(rs.next()){
		username = rs.getString(2);
		password = rs.getString(3);
	}
	conn.close();
}catch(Exception e){
	e.printStackTrace();
}

%>

<form action="update.jsp" method="post">
<input type="hidden" name="id" value="<%=id %>"/>
<table>

	<tr>
		<td>用户名:</td>
		<td><input type="text" name="username" value="<%=username %>"></td>
	</tr>
	<tr>
		<td>密码:</td>
		<td><input type="text" name="password" value="<%=password %>"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="提交"/> </td>
	</tr>
</table>

</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%
	int count = 0;
	String id = request.getParameter("id");
    if(null != id && id.length()>0){

		String cmd = "delete from t_user where id="+id;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String url= "jdbc:mysql://127.0.0.1/db_user";
			String uid = "root";
			String pwd = "root";
			Connection conn = DriverManager.getConnection(url,uid,pwd);
			Statement st = conn.createStatement();
			count = st.executeUpdate(cmd);
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
    }
	if(count>0) response.sendRedirect("list.jsp");
	else response.sendRedirect("error.jsp");
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%
	int count = 0;
	String id = request.getParameter("id");
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	if(null != id && id.length()>0){

		String cmd = "update t_user set username='"+username+"',password='"+password+"' where id="+id;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String url= "jdbc:mysql://127.0.0.1/db_user";
			String uid = "root";
			String pwd = "root";
			Connection conn = DriverManager.getConnection(url,uid,pwd);
			Statement st = conn.createStatement();
			count = st.executeUpdate(cmd);
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	if(count>0) response.sendRedirect("list.jsp");
	else response.sendRedirect("error.jsp");
%>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>db</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

JSP数据库综合练习的更多相关文章

  1. jsp 传值jsp 数据库 乱码解决的攻略 全套

    jsp传值给jsp中文乱码 传值给数据库乱码的解决方法 所有的用到编码的所有统一utf-8 1.装mysql的时候有选择编码的界面的那个地方选utf-8编码 2 建数据库的时候选择 字符集 排序规则所 ...

  2. Servlet+JSP+JDBC综合案例

    层级关系: 一.Util包 包里面写一个JDBCTools.java文件 功能:实现数据库连接返回一个Connection对象,并且可以实现数据库相应资源的关闭! 注意事项: 1.定义成员变量 1 p ...

  3. 基于MVC模式的数据库综合练习

    一.准备 没什么好说的,直接上代码.... 下面是web.xml <servlet> <servlet-name>list_user</servlet-name> ...

  4. 3.QT数据库综合案例,模糊查询等操作

     1 新建一个项目: Database01.pro SOURCES += \ main.cpp \ Contact.cpp QT += gui widgets sql CONFIG += C++1 ...

  5. jsp数据库开发

    完全卸载mysql数据库图文教程 https://jingyan.baidu.com/article/f96699bbaa8fc1894f3c1b5a.html MySQl:123456 JDBC概述 ...

  6. (纠错)JSP数据库判断是否存在

  7. JSP数据库插入判断

  8. JSP-Runoob:JSP 链接数据库

    ylbtech-JSP-Runoob:JSP 链接数据库 1.返回顶部 1. JSP 连接数据库 本教程假定您已经了解了 JDBC 应用程序的工作方式.在您开始学习 JSP 数据库访问之前,请访问 J ...

  9. JSP应用开发 -------- 电纸书(未完待续)

    http://www.educity.cn/jiaocheng/j9415.html JSP程序员常用的技术   第1章 JSP及其相关技术导航 [本章专家知识导学] JSP是一种编程语言,也是一种动 ...

随机推荐

  1. VSLAM技术框架详述

    最早的SLAM雏形是在军事(核潜艇的海底定位)上的应用,主要传感器是军用雷达.SLAM技术发展到如今已经几十年,目前以激光雷达作为主传感器的SLAM技术比较稳定.可靠,仍然是主流的技术方案.但随着最近 ...

  2. 多线程 NSOpeartion 的使用

    NSOperation简介 相对于 GCD ,具有面向对象的特征,比 GCD 更简单易用,代码可读性强 NSOperatioin 单独使用时, 不具有开辟新线程的能力, 只是同步执行操作, 需要配合 ...

  3. css和js带参数v或version

    1 <span style="font-size:14px;">css和js带参数(形如.css?v=与.js?v= 或 .css?version=与.js?versi ...

  4. JavaWeb学习笔记(六)—— Cookie&Session

    一.会话技术简介 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 在日常生活中,从拨通电话到挂断电话之间的一连串的你问我答的过程 ...

  5. JavaScript 中this 初步理解笔记

    Javascript中函数中的this通常指向的是函数的拥有者,这个拥有者就是上下文执行对象:另外一点需要注意,this只能在javascript函数内部使用.

  6. I Hate It(线段树区间最值,单点更新)-------------蓝桥备战系列

    很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让很多学生很反感.  不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老 ...

  7. 2017西安区域赛A / UVALive - 8512 线段树维护线性基合并

    题意:给定\(a[1...n]\),\(Q\)次询问求\(A[L...R]\)的异或组合再或上\(K\)的最大值 本题是2017的西安区域赛A题,了解线性基之后你会发现这根本就是套路题.. 只要用线段 ...

  8. web 前端提交方式

    1.get提交    如: <a href="actions/Student_Add?name=a&age=8">路径问题说明add</a>< ...

  9. Oracle 序列(自增ID)

    -- 创建序列CREATE SEQUENCE "JPADMIN"."SEQ_JP_BAS_USER_ID" MINVALUE 1 // 最小值MAXVALUE ...

  10. PIE SDK矢量透明度、标注控制

    1. 功能简介 透明度和标注是矢量图层的相关属性:图层透明值是0-100间的整数,标注是显示在地图上的文字信息,它是出图中不可或缺的重要元素.标注的样式丰富,并且放置位置灵活. 2. 功能实现说明 2 ...