
从数据库拿数据,并把值绑到界面上去
实体类:book.javapackage com.zking.etity;
import java.io.Serializable;
public class Book implements Serializable{
private static final long serialVersionUID = 1L;
private int bid;
private String bname;
private int bprice;
private String btype;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public int getBprice() {
return bprice;
}
public void setBprice(int bprice) {
this.bprice = bprice;
}
public String getBtype() {
return btype;
}
public void setBtype(String btype) {
this.btype = btype;
}
public Book() {
// TODO Auto-generated constructor stub
}
public Book(int bid, String bname, int bprice, String btype) {
super();
this.bid = bid;
this.bname = bname;
this.bprice = bprice;
this.btype = btype;
}
public Book(String bname, int bprice, String btype) {
this.bname = bname;
this.bprice = bprice;
this.btype = btype;
}
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", bprice=" + bprice + ", btype=" + btype + "]";
}
}
Module.java
package com.zking.etity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**实体类
* @author zjjt
*
*/
public class Module implements Serializable {
private static final long serialVersionUID = 6951631954282202090L;
private int id;
private int pid;
private String text;
private String url;
private String iconCls;
private int sort;
//子节点集合
private List children =new ArrayList();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getIconCls() {
return iconCls;
}
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public Module() {
// TODO Auto-generated constructor stub
}
public Module(int id, int pid, String text, String url, String iconCls, int sort, List children) {
super();
this.id = id;
this.pid = pid;
this.text = text;
this.url = url;
this.iconCls = iconCls;
this.sort = sort;
this.children = children;
}
public Module(int pid, String text, String url, String iconCls, int sort, List children) {
this.pid = pid;
this.text = text;
this.url = url;
this.iconCls = iconCls;
this.sort = sort;
this.children = children;
}
@Override
public String toString() {
return "Module [id=" + id + ", pid=" + pid + ", text=" + text + ", url=" + url + ", iconCls=" + iconCls
+ ", sort=" + sort + ", children=" + children + "]";
}
}
dao方法:BookDao.java(用了三层架构)
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.etity.Book;
import com.zking.util.DBHelper;
/**数据库访问层 书籍增删改查
* @author zjjt
*
*/
public class BookDao implements IBookDao {
@Override
public List getAllByPage(int pageIndex, int pageSize, String str) {
List lb=new ArrayList();
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
int a=(pageIndex-1)*pageSize+1;
int b=pageIndex*pageSize;
try {
//获得连接
con=DBHelper.getCon();
//定义sql语句
String sql="select * from(select a.*,rownum as rid from tb_book a where bname like '%"+str+"%') b where b.rid between ? and ?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, a);
ps.setInt(2, b);
//获得结果集
rs=ps.executeQuery();
//遍历结果集
while(rs.next()) {
//实例化书籍对象
Book book=new Book(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4));
//加到集合中
lb.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con,ps, rs);
}
return lb;
}
@Override
public int getRows(String str) {
int n=0;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
//获得连接
con=DBHelper.getCon();
//定义sql语句
String sql="select count(*) from "+str;
//获得执行对象
ps=con.prepareStatement(sql);
//获得结果集
rs=ps.executeQuery();
if(rs.next()) {
n=rs.getInt(1);//赋值
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con,ps, rs);
}
return n;
}
public static void main(String[] args) {
System.out.println(new BookDao().getRows("tb_book"));
}
}
ModuleDao.java
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.etity.Module;
import com.zking.util.DBHelper;
/**数据库访问层
* @author zjjt
*
*/
public class ModuleDao implements IModuleDao {
private Connection con=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
@Override
public List getAll(int pid) {
List lm=new ArrayList();
try {
//连接
con=DBHelper.getCon();
//定义sql语句
String sql="select *from tb_module where pid=? order by sort";
//获得执行对象
ps=con.prepareStatement(sql);
//占位符赋值
ps.setInt(1, pid);
//拿到结果集
rs=ps.executeQuery();
//遍历结果集
while(rs.next()) {
//实例化一个
Module m=new Module();
m.setId(rs.getInt(1));
m.setPid(rs.getInt(2));
m.setText(rs.getString(3));
m.setIconCls(rs.getString(4));
m.setUrl(rs.getString(5));
m.setSort(rs.getInt(6));
//加进去
lm.add(m);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return lm;
}
}
servlet:
BookListServlet.java
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 com.alibaba.fastjson.JSON;
import com.zking.biz.BookBiz;
import com.zking.biz.IBookBiz;
import com.zking.etity.Book;
@WebServlet("/bookList.do")
public class BookListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码方式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
//获取out对象
PrintWriter out = response.getWriter();
int pageIndex=1;
int pageSize=10;
//接收前台传递过来的参数 page rows bname
String pid=request.getParameter("page");//当前页码
if(pid!=null) {
pageIndex=Integer.parseInt(pid);
}
String size=request.getParameter("rows");//每页多少条
if(size!=null) {
pageSize=Integer.valueOf(size);
}
String bname=request.getParameter("bname");//关键字
if(bname==null) {
bname="";//相当于查询全部
}
String type=request.getParameter("btype");//关键字
if(type==null) {
type="bname";//相当于查询全部
}
//servlet调用biz
IBookBiz ibb=new BookBiz();
//获取总行数
int zhs = ibb.getRows("tb_book where "+type+" like'%"+bname+"%'");
List lb = ibb.getAllByPage(pageIndex, pageSize, bname);
//前台的json数据需要两个参数:total:总行数 rows:书籍集合
Map mym=new HashMap();
//放两对值
mym.put("total", zhs);
mym.put("rows", lb);
//把map集合--->json格式的对象字符串
String str = JSON.toJSONString(mym);
//输送到页面
out.write(str);
out.flush();
out.close();
}
}
BookServlet.java
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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 com.alibaba.fastjson.JSON;
import com.zking.biz.IModuleBiz;
import com.zking.biz.ModuleBiz;
import com.zking.etity.Module;
@WebServlet("/indexServlet.do")
public class indexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码方式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
//拿到out
PrintWriter out = response.getWriter();
//调用biz层
IModuleBiz imb=new ModuleBiz();
List lm = imb.getAll(-1);
//把集合转WWIjson的为字符串
String str = JSON.toJSONString(lm);
//响应
out.write(str);
out.flush();
out.close();
}
}
在common文件里建book,book里建了 head.jsp(公告界面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path=request.getContextPath();
//System.out.print(path);// /easyui01
request.setAttribute("con", path);
%>
index.jsp(主界面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="common/head.jsp"%>
Insert title here
书籍管理后台
©玉渊工作室,未经允许不可随意使用
效果图:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)