当前位置: 首页 > news >正文

建站之星模板制作云南城市建设职业学院网站

建站之星模板制作,云南城市建设职业学院网站,免费建站平台排行榜,烟台企业宣传片制作公司先来看一看分页的实现原理万能公式.jpg项目目录.PNG首先,新建Java Web项目一. 梳理业务逻辑重定向到URL(跳转到StudentViewAction页面)//index.jsp页面1.从页面接收可变的值2.接收值有问题时,初始化为13.如果没有问题,把String类型接收值强转成Integer4.实例DAO方法,调用findSt…先来看一看分页的实现原理万能公式.jpg项目目录.PNG首先,新建Java Web项目一. 梳理业务逻辑重定向到URL(跳转到StudentViewAction页面)//index.jsp页面1.从页面接收可变的值2.接收值有问题时,初始化为13.如果没有问题,把String类型接收值强转成Integer4.实例DAO方法,调用findStudentListByPageCount()方法(该方法得到总条数)5.计算总页数:总页数 总条数 % 页容量6.判断接收到页面传来的值是否小于1页7.调用DAO中findStudentListByPageCount()(该方法获取数据集合)8.封装打包页面9.转发页面request.getRequestDispatcher(list.jsp).forward(request, response);//request.getRequestDispatcher(list.jsp) 找到要转发的页面//forward(request, response); 实现转发二. 实现界面展示1.封装工具类JDBCUtil.java文件, 作用是连接数据库package com.fyl.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Scanner;/*** 文档注释(Java连接数据库的工具类)**/public class JDBCUtil {// 注意:四个属性:驱动, 地址(URL), 用户名, 密码// 驱动类:通过一个类名告诉java我现在使用的是什么数据库private static final String CONN_DRIVER com.mysql.jdbc.Driver;// URL:告诉Java我的数据库的具体位置(网络标识:通过什么端口哪台电脑获取什么资源)private static final String CONN_URL jdbc:mysql://127.0.0.1:3306/student_db?characterEncodingUTF-8;// 用户名private static final String CONN_USER_NAME root;// 密码private static final String CONN_USER_PASS 123456;public static Connection getConn() {// 创建方法的返回变量Connection conn null;try {// 1.加载驱动类 让Java知道我们创建什么数据库的实例Class.forName(CONN_DRIVER);// 通过已经加载好的驱动类给我们提供连接conn DriverManager.getConnection(CONN_URL, CONN_USER_NAME,CONN_USER_PASS);} catch (ClassNotFoundException e) {System.out.println(add DriverManager error!);e.printStackTrace();} catch (SQLException e) {System.out.println(SQL error!);e.printStackTrace();}return conn;}public static void closeAll(ResultSet set, PreparedStatement ps,Connection conn) {try {if (null ! set) {set.close();}if (null ! ps) {ps.close();}if (null ! conn) {conn.close();}} catch (SQLException e) {System.out.println(closeAll ERROR!);e.printStackTrace();}}public static void main(String[] args) {// 获取数据库连接Connection conn JDBCUtil.getConn();Scanner scan new Scanner(System.in);while (true) {System.out.println(请素输入要查看的数据:);int i scan.nextInt();int start (i - 1) * 10;int size 10;// 2.编写SQL语句(查询id 0的数据, 连续查询10条记录)String sql SELECT * FROM student WHERE s_id LIMIT ?,?;// SELECT * FROM student WHERE s_id 10 AND s_id (10 10)// 3.运行SQL语句try {PreparedStatement ps conn.prepareStatement(sql);ps.setInt(1, start);ps.setInt(2, size);// 4.执行后返回结果(execute执行Query结果)ResultSet set ps.executeQuery();System.out.println(学生编号\t学生姓名\t学生年龄\t入学时间\t学费);for (; set.next();) {System.out.print(set.getInt(S_ID) \t);System.out.print(set.getString(S_NAME) \t);System.out.print(set.getInt(S_AGE) \t);System.out.print(set.getDate(S_INTODATE) \t);System.out.print(set.getDouble(S_MONEY) \t);System.out.println();}} catch (SQLException e) {System.out.println(select error);e.printStackTrace();}}}}2.创建数据库实体类Student.java文件(Model层)package com.fyl.entity;import java.io.Serializable;import java.util.Date;/*** 实体类* author Administrator**/public class Student implements Serializable {private static final long serialVersionUID 1L;//添加唯一标识private Integer id;private String name;private int age;private Date date;private Double money;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public Date getDate() {return date;}public void setDate(Date date) {this.date date;}public Double getMoney() {return money;}public void setMoney(Double money) {this.money money;}}3.index.jsp界面(呈现给用户的第一个界面)系统首页// 重定向到URLrequest.getRequestDispatcher(StudentViewAction).forward(request, response);%4.新建servlet文件StudentViewAction.java(Controller层)package com.fyl.web;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.fyl.dao.StudentDAO;import com.fyl.dao.impl.StudentDAOImpl;import com.fyl.entity.Student;public class StudentViewAction extends HttpServlet {private static final long serialVersionUID 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//封装数据给页面//整理页面需要的数据int pageIndex 0;//页面 //页面每次请求传过来的int pageSize 10;//int totalCount 0;int totalPge 0;List list null;//从页面接收可变的值String pi request.getParameter(pageIndex);//pi有问题的时候,初始化为1if (null pi || .equals(pi)) {pi 1;}//如果pi没有问题的时候pageIndex Integer.parseInt(pi);//从数据接收值StudentDAO dao new StudentDAOImpl();//调用DAO方法totalCount dao.findStudentListByPageCount();//计算总页数totalPge totalCount % pageSize 0?totalCount/pageSize:totalCount/pageSize 1;//判断pageIndex的边界值if (pageIndex 1) {pageIndex 1;}if (pageIndex totalPge) {pageIndex totalPge;}//获取数据集合list dao.findStudentListByPage(pageIndex, pageSize);//封装打包页面request.setAttribute(pageIndex, pageIndex);request.setAttribute(pageSize, pageSize);request.setAttribute(totalCount, totalCount);request.setAttribute(totalPge, totalPge);request.setAttribute(list, list);//转发页面request.getRequestDispatcher(list.jsp).forward(request, response);}}新建list.jsp界面接收StudentViewAction传来的值数据展示function goUpdate(id){window.location.href StudentFindByIDViewAction?id id;}function goDelete(id){var con window.confirm(您确定删除ID为 id 这条数据吗? );if(con){//删除window.location.href StudentDeleteAction?id id;}}function goPage(pageIndex){window.location.href StudentViewAction?pageIndexpageIndex;}function goPage(pageIndex){window.location.href StudentViewAction?pageIndexpageIndex;}function goAdd(){window.location.href add.jsp;}一共查询出${totalCount}条数据,每页展示${pageSize}条,一共有${totalPage}页,当前浏览的是第${pageIndex}页学生ID学生姓名学生年龄入学时间学费操作${s.id}${s.name}${s.age} ||三. 实现增删查改创建接口, 新建StudentDAO.java接口文件, 添加增删查改方法package com.fyl.dao;import java.util.List;import com.fyl.entity.Student;public interface StudentDAO {/*** 更据id删除* param id* return* throws RuntimeException*/public boolean deleteStudent(Integer id) throws RuntimeException;/*** 根据ID查询单个学生对象* param id* return* throws RuntimeException*/public Student findStudentByID(Integer id) throws RuntimeException;/** 添加学生方法* param student 要添加的学生* return 添加成功返回true 添加失败返回false* throws RuntimeException*/public boolean insertStudent(Student student)throws RuntimeException;/*** 查询数据库的总条数* return 总条数* throws RuntimeException*/public int findStudentListByPageCount() throws RuntimeException;/*** 获取分页数集合* param pageIndex 页码* param pageSize 页容量* return 已经分页的list集合* throws RuntimeException*/public List findStudentListByPage(Integer pageIndex, Integer pageSize) throws RuntimeException;/** 更新学生信息* param student* return* throws RuntimeException*/public boolean updateStudent(Student student) throws RuntimeException;}2.新建StudentDAOImpl.java文件,实现接口package com.fyl.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.fyl.dao.StudentDAO;import com.fyl.entity.Student;import com.fyl.util.JDBCUtil;public class StudentDAOImpl implements StudentDAO {// TODOpublic int findStudentListByPageCount() throws RuntimeException {// 1.创建方法的返回变量int totalCount 0;// 3.获取数据库连接Connection conn JDBCUtil.getConn();// 4.编写SQL语句String sql SELECT COUNT(S_ID) FROM STUDENT;// 执行SQL语句PreparedStatement ps null;ResultSet set null;try {ps conn.prepareStatement(sql);set ps.executeQuery();//处理if (set.next()) {totalCount set.getInt(1);}} catch (SQLException e) {// TODOe.printStackTrace();} finally {JDBCUtil.closeAll(set, ps, conn);}return totalCount;}// TODOpublic List findStudentListByPage(Integer pageIndex,Integer pageSize) throws RuntimeException {List list new ArrayList();//2.1获取数据库连接Connection conn JDBCUtil.getConn();//3. 创建SQL语句String sql SELECT * FROM STUDENT WHERE S_ID LIMIT ?,?;//4.执行SQL语句PreparedStatement ps null;ResultSet set null;try {ps conn.prepareStatement(sql);ps.setInt(1, (pageIndex-1) * pageSize);ps.setInt(2, pageSize);set ps.executeQuery();Student s null;while (set.next()) {s new Student();//封装数据s.setId(set.getInt(S_ID));s.setName(set.getString(S_NAME));s.setAge(set.getInt(S_AGE));s.setMoney(set.getDouble(S_MONEY));s.setDate(set.getDate(S_INTODATE));// 将封装好的Student对像装入集合list.add(s);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {JDBCUtil.closeAll(set, ps, conn);}return list;}public boolean insertStudent(Student student) throws RuntimeException {// TODO Auto-generated method stub//1.定义方法返回变量boolean con false;//3. 获取数据库连接Connection conn JDBCUtil.getConn();//4. 编写SQL语句String sql INSERT INTO STUDENT (S_NAME,S_AGE,S_INTODATE,S_MONEY) VALUES (?,?,?,?);// 5. 执行SQL语句PreparedStatement ps null;try {ps conn.prepareStatement(sql);// 6. 是否有占位符赋值?ps.setString(1, student.getName());ps.setInt(2, student.getAge());ps.setDate(3, new java.sql.Date(student.getDate().getTime()));ps.setDouble(4, student.getMoney());int count ps.executeUpdate(); // 执行增 删 改 SQL 返回int类型的受影响行数// 7. 改变方法的返回值con count0?true:false;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 2. 返回conreturn con;}// TODO 根据id查询public Student findStudentByID(Integer id) throws RuntimeException {//创建方法的返回值Student student null;Connection conn JDBCUtil.getConn();//编写SQL语句String sql SELECT * FROM STUDENT WHERE S_ID ?;//执行SQL语句PreparedStatement ps null;ResultSet set null;try {ps conn.prepareStatement(sql);//是否有占位符ps.setInt(1, id);set ps.executeQuery();if(set.next()){//创建实例对象封装查询数据student new Student();student.setId(set.getInt(S_ID));student.setAge(set.getInt(S_AGE));student.setDate(set.getDate(S_INTODATE));student.setMoney(set.getDouble(S_MONEY));student.setName(set.getString(S_NAME));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{JDBCUtil.closeAll(set, ps, conn);}return student;}// TODO 更新学生信息public boolean updateStudent(Student student) throws RuntimeException {//创建方法的返回值boolean con false;//获取数据库连接Connection conn JDBCUtil.getConn();//编写SQL语句String sql UPDATE STUDENT SET S_NAME?,S_AGE?,S_INTODATE?,S_MONEY? WHERE S_ID?;//执行SQL语句PreparedStatement ps null;try {ps conn.prepareStatement(sql);//是否有占位符ps.setString(1, student.getName());ps.setInt(2, student.getAge());ps.setDate(3, new java.sql.Date(student.getDate().getTime()));ps.setDouble(4, student.getMoney());ps.setInt(5, student.getId());int count ps.executeUpdate();con count0?true:false;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return con;}// TODO deletepublic boolean deleteStudent(Integer id) throws RuntimeException {//创建方法的返回变量boolean con false;//获取数据库链接Connection conn JDBCUtil.getConn();//编写SQL语句String sql DELETE FROM STUDENT WHERE S_ID ?;//执行SQL语句PreparedStatement ps null;try {ps conn.prepareStatement(sql);ps.setInt(1, id);int count ps.executeUpdate();con count 0?true:false;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{JDBCUtil.closeAll(null, ps, conn);}return con;}// TODO mainpublic static void main(String[] args) {StudentDAO dao new StudentDAOImpl();System.out.println(dao.findStudentListByPageCount());}}3.创建servlet文件StudentAddAction.java接收用户传入的值,添加到数据库并展示到list.jsp(增)package com.fyl.web;import java.io.IOException;import java.io.PrintWriter;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.fyl.dao.StudentDAO;import com.fyl.dao.impl.StudentDAOImpl;import com.fyl.entity.Student;public class StudentAddAction extends HttpServlet {private static final long serialVersionUID 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置请求来源的编码request.setCharacterEncoding(UTF-8);//1. 接收页面数据String studentName request.getParameter(studentName);String studentAge request.getParameter(studentAge);String intoDate request.getParameter(intoDate);String money request.getParameter(money);//2. 封装Student student new Student();student.setName(studentName);student.setAge(Integer.parseInt(studentAge));student.setMoney(Double.parseDouble(money));// String 转 时间DateFormat df new SimpleDateFormat(yyyy-MM-dd);try {Date d df.parse(intoDate);student.setDate(d);} catch (ParseException e) {e.printStackTrace();}// 3. 创建DAO层对象添加到数据库StudentDAO dao new StudentDAOImpl();boolean con dao.insertStudent(student);if(con){// 添加成功response.sendRedirect(StudentViewAction);}else{// 添加失败// 通过服务器的响应流主动向客户端发送信息response.setCharacterEncoding(UTF-8);response.setContentType(text/html; charsetUTF-8);String msg ;PrintWriter out response.getWriter();out.print(msg);out.flush();out.close();}}}4.创建servlet文件StudentDeleteAction.java接收用户传入的值,删除数据库中指定文件并展示到list.jsp(删)package com.fyl.web;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.fyl.dao.StudentDAO;import com.fyl.dao.impl.StudentDAOImpl;public class StudentDeleteAction extends HttpServlet {private static final long serialVersionUID 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//1. 确定编码request.setCharacterEncoding(UTF-8);//2. 获取页面数据String id request.getParameter(id);//3. 创建DAO方法执行删除StudentDAO dao new StudentDAOImpl();boolean con dao.deleteStudent(Integer.parseInt(id));if(con){//添加成功response.sendRedirect(StudentViewAction);}else{//添加失败response.setCharacterEncoding(UTF-8);response.setContentType(text/html; charsetUTF-8);String msg ;PrintWriter out response.getWriter();out.print(msg);out.flush();out.close();}}}创建servlet文件StudentFindByIDViewAction.java接收用户传入的值,查询数据库中指定文件并展示到list.jsp(查)package com.fyl.web;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.fyl.dao.StudentDAO;import com.fyl.dao.impl.StudentDAOImpl;import com.fyl.entity.Student;public class StudentFindByIDViewAction extends HttpServlet {private static final long serialVersionUID 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置编码request.setCharacterEncoding(UTF-8);//接收页面输入String id request.getParameter(id);//创建DAO层对象StudentDAO dao new StudentDAOImpl();Student student dao.findStudentByID(new Integer(id));request.setAttribute(stu, student);request.getRequestDispatcher(update.jsp).forward(request, response);}}6.创建servlet文件StudentUpdateAction.java接收用户传入的值,更新数据库中指定文件并展示到list.jsp(改)package com.fyl.web;import java.io.IOException;import java.io.PrintWriter;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.fyl.dao.StudentDAO;import com.fyl.dao.impl.StudentDAOImpl;import com.fyl.entity.Student;public class StudentUpdateAction extends HttpServlet {private static final long serialVersionUID 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置请求来源的编码格式request.setCharacterEncoding(UTF-8);//1. 设置接收页面数据String studentId request.getParameter(studentId);String studentName request.getParameter(studentName);String studentAge request.getParameter(studentAge);String intoDate request.getParameter(Date);String money request.getParameter(money);//2. 封装Student student new Student();String studentId1 studentId.trim();student.setId(Integer.parseInt(studentId1));student.setName(studentName);student.setAge(Integer.parseInt(studentAge));student.setMoney(Double.parseDouble(money));//String转时间DateFormat df new SimpleDateFormat(yyyy-MM-dd);try {Date d df.parse(intoDate);student.setDate(d);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}//3. 创建DAO层对象添加到数据库StudentDAO dao new StudentDAOImpl();boolean con dao.updateStudent(student);if(con)0.{//添加成功response.sendRedirect(StudentViewAction);}else{//添加失败//通过服务器的响应流主动向客户端发送信息response.setCharacterEncoding(UTF-8);response.setContentType(text/html; charsetUTF-8);String msg ;PrintWriter out response.getWriter();out.print(msg);out.flush();out.close();}}}
http://www.sadfv.cn/news/29097/

相关文章:

  • 郑州网站关键字优化优设网app官方下载
  • 公司要做个网站吗域名请记住222922
  • 铁岭建设网站现代建设中国公司网站
  • 专门做养老院的网站发行商城小程序
  • 深圳优化网站网站建设济南云畅网络
  • wordpress新闻视频站1688网站上自己做模版
  • linux上安装wordpress沈阳seo网站推广
  • 拿网站做商标怎么做资源类网站
  • 江苏网站备案需要多久西安免费自助建站模板
  • 德州市住房和城乡建设部网站专门做爬虫的网站
  • 网站建设淘宝属于什么类目网络维护主要工作内容
  • 彭州建设网站ASP做网站源代码
  • 上海中高端网站建设seo推广是什么
  • 网站建设纟金手指下拉壹陆app制作二维码
  • 移动网站 用户体验设计网站作品
  • 企业网站申请流程做网站架构需要什么工具
  • 嘉兴网站建设推荐html在线运行
  • 主流的网站建设的软件什么网站做婚礼请柬
  • 湖北网站排名优化互联网营销师报名入口官网
  • 青岛市两个体系建设网站网站做淘宝客赚钱吗
  • 云南省住房建设厅网站西安网站制作顶尖公司
  • 网站建设专业简介dede我的网站
  • 东莞网站建设新闻资讯自己建的网站也要注册域名吗
  • 网站开发需求问卷h5网站显示的图标怎么做
  • 查询个人房产信息网站做柜子喜欢上哪些网站看
  • vue做响应式网站建设知道购物网站
  • wap网站生成微信小程序广东响应式网站建设平台
  • 如何建设一个读书的网站内网门户网站建设方案
  • 一个网站域名多少钱有名的网站建设
  • dw怎样去除网站做的页面模板域名注册后怎么使用