数据库代码:

create database xiaojungan;
use xiaojungan;
create table IF NOT EXISTS `t_user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL ,
  `password` varchar(50) NULL ,
  `role` int(11) NOT NULL DEFAULT '0' COMMENT '用户角色',
  PRIMARY KEY (`id`)
) AUTO_INCREMENT=100;

CREATE TABLE `t_food` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `foodname` varchar(100) NOT NULL,
  `type_id` int(11) ,
  `descript` longtext ,
  `price` decimal(10,2) ,
  `onSale` int(11) default '0',
  `imgAddress` varchar(200) ,
  PRIMARY KEY (`id`)
) ;
CREATE TABLE `t_type` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `typename` varchar(20) ,
  PRIMARY KEY (`id`)
) ;
CREATE TABLE `t_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `food_id` int(11),
  `user_id` int(11),
  `table_id` int(11),
  `food_account` int(11) ,
  PRIMARY KEY (`id`)
);
CREATE TABLE `t_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tablename` varchar(20) ,
  `user_id` int(11),
  PRIMARY KEY (`id`)
)

 

c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/xiaojungan?serverTimezone=Asia/Shanghai</property>
        <property name="user">root</property>
        <property name="password">147258369</property>
        <!-- 初始化连接的数量 -->
        <property name="initialPoolSize">10</property>
        <!-- 最大空闲时间,单位是秒 -->
        <property name="maxIdleTime">1</property>
        <!-- 池中最大连接的数量 -->
        <property name="maxPoolSize">100</property>
        <!-- 池中最小连接的数量 -->
        <property name="minPoolSize">10</property>
    </default-config>
</c3p0-config>

 

Java

 

FoodDaoimpl.java

package dao.daoImpl;

import bean.Food;
import util.C3P0Util;
import dao.FoodDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao{
    @Override
    public void addFood(Food food) throws Exception{
        String sql = "INSERT INTO `xiaojungan`.`t_food`(`foodname`,`type_id`,`descript`,`price`,`saling`,`img`) VALUES (?,?,?,?,?,?)";

        try(
                Connection conn = C3P0Util.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
        ) {
            ps.setString(1,food.getFoodname());
            ps.setInt(2, food.getType_id());
            ps.setString(3,food.getDescript());
            ps.setBigDecimal(4, food.getPrice());
            ps.setInt(5,food.getSaling());
            ps.setString(6, food.getImg());

            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加失败!");
        }
    }


    @Override
    public List<Food> findSalingFoods() throws Exception{
        String sql = "select * from t_food where saling=?";
        List<Food> foodList = new ArrayList<>();
        try(
                Connection conn = C3P0Util.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, 1);
            try (
                    ResultSet rs = ps.executeQuery()
            ) {

                //将查询出的结果数据封装到List对象中
                while(rs.next()){
                    Food b = new Food();
                    b.setId(rs.getInt("id"));
                    b.setFoodname(rs.getString("foodname"));
                    b.setType_id(rs.getInt("type_id"));
                    b.setDescript(rs.getString("descript"));
                    b.setPrice(rs.getBigDecimal("price"));
                    b.setImg(rs.getString("img"));
                    b.setSaling(rs.getInt("saling"));
                    foodList.add(b);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return foodList;
    }

    @Override
    public  Food findFood_id(int id) throws Exception {
        String sql = "select * from t_food where id=?";
       Food b = null;

        try(
                Connection conn = C3P0Util.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, id);

            try (
                    ResultSet rs = ps.executeQuery()
            ) {
                //将查询出的结果数据封装到Food对象中
                if(rs.next()){
                    b = new Food();
                    b.setId(rs.getInt("id"));
                    b.setFoodname(rs.getString("foodname"));
                    b.setType_id(rs.getInt("type_id"));
                    b.setDescript(rs.getString("descript"));
                    b.setPrice(rs.getBigDecimal("price"));
                    b.setSaling(rs.getInt("saling"));
                    b.setImg(rs.getString("img"));

                    return b;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return b;
    }
}

 

Html代码

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>小郡肝</title>
    <style type="text/css">
        #main{
            width: 80%;height: auto;
            margin: 0 auto;
            overflow:auto;
            background: burlywood;
        }
        #header_div{
            width:80%;height: auto;
            overflow:auto;
            margin:5px auto 5px;
            background: coral;
            border-style: groove;
        }
        #header{
            width:80%;height: auto;
            margin:0px auto ;
            padding: 20px;
            overflow:auto;
        }
        #food_div{
            width:80%;height: auto;
            margin:0px auto 50px auto;
            overflow:auto;
            border-style: solid;
            background: aliceblue;
        }
        .childfood_div{
            width:80%;height: auto;
            margin:5px auto ;
            border-style: solid;
            padding: 20px;
            overflow:auto;
        }
    </style>
</head>
<body>
<div id="main">
    <div id="header_div">
        <div id="header">
            <span style="font-family: 微软雅黑;font-size: xx-large">小郡肝火锅</span>
            <br>

            <p style="float: right">
                <span id="loginT"><a href="login.html">登陆</a></span><span>&nbsp;&nbsp;</span>
                <span id="registT"><a href="register.html">注册</a> </span><span>&nbsp;&nbsp;</span>
                <span id="order" style="outline: none;"><a href="OrderServlet">餐车</a> </span>
            </p>
        </div>

    </div>
    <div id="food_div">

    </div>
</div>
</body>
<script type="text/javascript" src="MyAjax.js"></script>
<script type="text/javascript">
    window.onload = function () {
        var foodDiv = document.getElementById("food_div");

        //获取XMLHttpRequest对象
        var xhr = getXMLHttpRequest();
        var user_id = "" ;
        //回调函数
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                if(xhr.status == 200){
                    var newsJ = xhr.responseText;//获取服务器返回的数据
                    if(newsJ == ""){
                        return;
                    }
                    //转化Json消息
                    var result = JSON.parse(newsJ);

                    if(result[0] != null){
                        //消息第一个元素为当前用户
                        var loginT = document.getElementById("loginT");
                        loginT.innerHTML = result[0].username;
                        user_id = result[0].id;
                        var registT = document.getElementById("registT");
                        registT.innerHTML = "<a href='LoginOutServlet'>注销</a>";
                        var order = document.getElementById("order");
                        order.innerHTML = "<a href='OrderServlet?user_id="+ user_id +"'>餐车</a>";
                    }

                    var childDivs = "";
                    //循环放入到div中
                    for(var i=1; i<result.length; i++){
                        childDivs += "<br><div class='childfood_div'>"
                            + "<div style='float: left'>"
                            + "<img src='" + result[i].img + "' width='198' height='198'/>"
                            + "</div>"
                            + "<div style='float: left'>"
                            + "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                            +"<sqan style='font-size: x-large;font-family: 黑体'>" + result[i].foodname + "</sqan>"
                            + "<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                            + "在售价:" + result[i].price
                            + "<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                            + "<sqan>" +"描述:"+ result[i].descript +"</sqan>"
                            + "<sqan>" +"分类:"+ result[i].type_id+"</sqan>"


                            + "</div>"
                            + "<div style='float: left'>"
                            + "<br><br><br><br><br><br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;"
                            + "<a href ='AddOrderServlet?food_id=" + result[i].id + "&user_id="
                            + user_id + "&food_account=1"+ "'>加入餐车</a>"
                            + "</div>"
                            + "</div>";
                    }

                    //把多个食物childDivs放入列表foodDiv中
                    foodDiv.innerHTML = childDivs;
                }
            }
        }
        //创建连接
        xhr.open("get","/Servlet?id=food");
        //发送请求
        xhr.send(null);
    }
</script>
</html>