项目文件目录
package com.ozpk.dao;
import java.util.List;
import com.ozpk.pojo.Flower;
/**
* @author user
* 查询所有
*/
public interface FlowerDao {
List selAll();
// 返回值增删改都是int
/**
* 新增
* @param flower
* @return
*/
int insFlower(Flower flower);
}
package com.ozpk.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ozpk.dao.FlowerDao;
import com.ozpk.pojo.Flower;
/**
* @author user 数据访问层要有异常处理
*/
public class FlowerDaoImpl implements FlowerDao {
@Override
public int insFlower(Flower flower) {
int index = 0;
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "niit");
ps = conn.prepareStatement("insert into flower value(default,?,?,?)");
ps.setObject(1, flower.getName());
ps.setObject(2, flower.getPrice());
ps.setObject(3, flower.getProduction());
index=ps.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return index;
}
@Override
public List selAll() {
// JDK从1.7开始后面泛型可以省略
List list = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "niit");
ps = conn.prepareStatement("select * from flower");
rs = ps.executeQuery();// 游标指向数据,rs是一行数据,要加while循环
// rs.next();控制游标指向
while (rs.next()) {
list.add(new Flower(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getString(4)));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
}
package com.ozpk.pojo;
public class Flower {
private int id;
private String name;
private double price ;
private String production;
public Flower(int id, String name, double price, String production) {
super();
this.id = id;
this.name = name;
this.price = price;
this.production = production;
}
public Flower() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getProduction() {
return production;
}
public void setProduction(String production) {
this.production = production;
}
}
package com.ozpk.service;
import java.util.List;
import com.ozpk.pojo.Flower;
/**
* @author user
* 显示所有花卉信息
*/
public interface FlowerService {
List show();
int add(Flower flower);
}
package com.ozpk.service;
import java.util.List;
import com.ozpk.dao.FlowerDao;
import com.ozpk.dao.impl.FlowerDaoImpl;
import com.ozpk.pojo.Flower;
public class FlowerServiceImpl implements FlowerService{
private FlowerDao flowerDao=new FlowerDaoImpl();
@Override
public List show() {
return flowerDao.selAll();
}
@Override
public int add(Flower flower) {
return flowerDao.insFlower(flower);
}
}
package com.ozpk.servlet;
import java.io.IOException;
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.ozpk.pojo.Flower;
import com.ozpk.service.FlowerService;
import com.ozpk.service.FlowerServiceImpl;
/**
*
* @ClassName: AddServlet.java
* @Description:
*
* @author: CY
* @date: 2019年1月7日 下午10:49:05
* tomcat 7以上
* web 3.0以上
*/
@WebServlet("/insert")
public class AddServlet extends HttpServlet{
private FlowerService flowerService=new FlowerServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
String price=req.getParameter("price");
String production=req.getParameter("production");
Flower flower=new Flower();
flower.setName(name);
flower.setPrice(Double.parseDouble(price));
flower.setProduction(production);
int index=flowerService.add(flower);
if(index>0){
// 重定向防止表单重复提交
resp.sendRedirect("show");
// req.getRequestDispatcher("show").forward(req, resp);
}else{
resp.sendRedirect("add.jsp");
// req.getRequestDispatcher("add.jsp").forward(req, resp);
}
}
}
package com.ozpk.servlet;
import java.io.IOException;
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.ozpk.pojo.Flower;
import com.ozpk.service.FlowerService;
import com.ozpk.service.FlowerServiceImpl;
//从web3.0开始的
//@WebServlet("/show")
public class ShowServlet extends HttpServlet{
private FlowerService flowerService=new FlowerServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List list=flowerService.show();
req.setAttribute("list", list);
req.getRequestDispatcher("index.jsp").forward(req, resp);;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Insert title here
花卉编号
花卉名称
花卉价格
原产地
${flower.id}
${flower.name}
${flower.price}
${flower.production}
添加花卉信息