目录
js代码:
servlet代码:
Dao层实现类代码:
service层代码:
运行结果:
1.首页界面
2.分页后界面
3.增加后
4.模糊查询后
5.修改界面
6.删除界面
<%@ page import="Service.impl.BananaServiceimpl" %>
<%@ page import="Service.BananaService" %>
<%@ page import="java.util.List" %>
<%@ page import="entity.banana" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
香蕉管理系统
请输入香蕉名称:
香蕉编号
香蕉名称
香蕉价格
香蕉颜色
种植时间
操作
代码量太多这里只展示分页servlet
package Servlet;
import Service.BananaService;
import Service.impl.BananaServiceimpl;
import com.alibaba.fastjson.JSON;
import entity.banana;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/Do_page")
public class Do_page extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置resp编码格式为utf-8 两种方式都可以
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
BananaService banana=new BananaServiceimpl();
int index=1;
int count = (int) Math.ceil(banana.count()/3.0);
if(req.getSession().getAttribute("index")!=null){
int page= Integer.parseInt(req.getParameter("index"));
index= (int) req.getSession().getAttribute("index");
if(page==-1||page==1){
index=index+page;
}else{
index=Integer.parseInt(req.getParameter("index"));
}
if(index>count){
index=count;
}else if(index<1){
index=1;
}
}
//返回结果给AJAX 用out对象输出返回
PrintWriter out = resp.getWriter();
req.getSession().setAttribute("index",index);
List list=banana.limit(index,3);
//如果java结果要返回对象或者集合
//一定要手动先转成JSON格式 再out.print返回
String result = JSON.toJSONString(list);
out.print(result);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
package dao.impl;
import dao.Bananaimpl;
import dao.BasDao;
import entity.banana;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Bananadaoimpl extends BasDao implements Bananaimpl {
@Override
public List getall() {
Listlist=new ArrayList<>();
Connection conn=this.getConnection();
String sql="select * from banana";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
banana b=new banana();
b.setBid(rs.getInt("bid"));
b.setBname(rs.getString("bname"));
b.setBprice(rs.getDouble("bprice"));
b.setBcolor(rs.getString("bcolor"));
b.setBtime(rs.getString("btime"));
list.add(b);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public int delete(int bid) {
int i=0;
Connection conn=this.getConnection();
String sql="delete from banana where bid=?";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,bid);
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public int insert(banana b) {
int i=0;
Connection conn=this.getConnection();
String sql="insert into banana(bid,bname,bprice,bcolor,btime) values(?,?,?,?,?)";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,b.getBid());
ps.setString(2,b.getBname());
ps.setDouble(3,b.getBprice());
ps.setString(4,b.getBcolor());
ps.setString(5,b.getBtime());
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public int update(banana b) {
int i=0;
Connection conn=this.getConnection();
String sql="update banana set bname=?,bprice=?,bcolor=?,btime=? where bid=?";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,b.getBname());
ps.setDouble(2,b.getBprice());
ps.setString(3,b.getBcolor());
ps.setString(4,b.getBtime());
ps.setInt(5,b.getBid());
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public List limit(int index,int i) {
Listlist=new ArrayList<>();
Connection conn=this.getConnection();
String sql="select * from banana limit ?,?";
try {
PreparedStatement ps=conn.prepareStatement(sql);
int c=(index-1)*i;
ps.setInt(1,c);
ps.setInt(2,i);
ResultSet rs=ps.executeQuery();
while (rs.next()){
banana b=new banana();
b.setBid(rs.getInt("bid"));
b.setBname(rs.getString("bname"));
b.setBprice(rs.getDouble("bprice"));
b.setBcolor(rs.getString("bcolor"));
b.setBtime(rs.getString("btime"));
list.add(b);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public int count() {
int i=0;
Connection conn=this.getConnection();
String sql="select count(*) from banana";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
i=rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public List like(String bname) {
Listlist=new ArrayList<>();
Connection conn=this.getConnection();
String sql="select * from banana where bname like ?";
try {
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,'%'+bname+'%');
ResultSet rs=ps.executeQuery();
while (rs.next()){
banana b=new banana();
b.setBid(rs.getInt("bid"));
b.setBname(rs.getString("bname"));
b.setBprice(rs.getDouble("bprice"));
b.setBcolor(rs.getString("bcolor"));
b.setBtime(rs.getString("btime"));
list.add(b);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
package Service;
import entity.banana;
import java.util.List;
public interface BananaService {
/*
* 查询全部香蕉
* */
public List getall();
/*
* 删除香蕉
* */
public int delete(int bid);
/*
* 增加香蕉
* */
public int insert(banana b);
/*
* 修改香蕉
* */
public int update(banana b);
/*
* 分页查询全部
* */
public Listlimit(int index,int i);
/*
* 统计页数
* */
public int count();
/*
* 模糊查询
* */
public Listlike(String bname);
}
本项目全部采用Ajax异步方式完成