1.创建数据库
2.创建表 添加数据
3.添加jar和c3p0的配置文件
4.搭建 三层
com.vp.bean或者 com.vp.pojo
实体类和辅助类,接收数据和发送数据所使用的载体
com.vp.service
业务逻辑层接口,增加功能
com.vp.service.impl 这里其实是调用dao层的方法来实现
业务逻辑层接口实现类,实现功能//相当于表现层和数据访问层的缓冲区
com.vp.dao
数据访问层接口,增加需求的功能
com.vp.dao.impl
数据访问层实现类,实现需求,直接访问数据库,并对数据库进行增删改查,并利用返回值进行反馈。
com.vp.util
工具类
C3P0工具类以及其他工具类,主要用于减少代码冗余。
com.vp.test //调用业务逻辑层的实现类方法。
C3P0
private static QueryRunner queryRunner;
public static QueryRunner getQueryRunner(){
//获取一个数据源
DataSource dsource=new ComboPooledDataSource();
//通过数据源初始化一个QueryRunner对象
queryRunner=new QueryRunner(dsource);
return queryRunner;
}
DAO层代码 private QueryRunner queryRunner =C3P0Util.getQueryRunner();//表示调用该方法,可以取代classforname和DriverManager.getConnection使用
测试类,表现层。
5.根据表创建实体类
6.在表现层中编写菜单
例如:欢迎光临图书借阅系统
1.查询所有图书信息
2.根据条件查询所有图书信息
3.根据图书编号查询该图书的详细信息
6.查询所有图书类型信息
4.新增图书
5.根据图书编号删除图书信息
6.根据图书类型编号删除图书类型
7.根据图书编号修改图书信息
7.根据需求开始写代码
(curd)
@WebServlet(name ="ibookServlet.htm", urlPatterns = "/ibookServlet.htm")
public class IbookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//中文乱码处理
request.setCharacterEncoding("utf-8");
String book_code = request.getParameter("book_code");//request.getParameter获得对应name的value
String book_name = request.getParameter("book_name");
String book_type = request.getParameter("book_type");
String book_author = request.getParameter("book_author");
String book_publish_press = request.getParameter("book_publish_press");
String publish_date = request.getParameter("publish_date");
System.out.println(book_type);
int type = 0;
if (book_type != null && !"".equals(book_type)) {
type = Integer.parseInt(book_type);//强转
}
Book_InfoTable book_infoTable = new Book_InfoTable(book_code, book_name, type, book_author, book_publish_press, publish_date, 0);
IBookServiceImpl iBookServiceimpl=new IBookServiceImpl();
int add = iBookServiceimpl.add(book_infoTable);
if (add>=1){
response.sendRedirect("success.html");//最终返回到某个jsp网址。
}else {
response.sendRedirect("false.html");
}
}