数据库代码:
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
com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/xiaojungan?serverTimezone=Asia/Shanghai root 147258369 10 1 100 10
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 ListfindSalingFoods() throws Exception{ String sql = "select * from t_food where saling=?"; List 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