一個簡單的入門Java web項目

 

引言:商城訂單管理系統屬於小型的Java web系統,由java web+mysql+servlet實現,採用mvc的設計模式,jdbc編程,具備增刪改查以及模糊查詢,用戶登陸註冊的功能,版本爲eclipse版,如果需要別的版本如idea版,系統架構採用ssm或者springboot的類似Java 或Java web 小型軟件系統,或者定製的,需要本系統源碼的,幫忙遠程部署安裝講解可以加我qq1728608455。

關注我的微信公衆號,送IT視頻資料,並送Java web源碼一份,希望能幫到你;


1、數據庫表圖:

 


2、登陸界面


 3、系統主界面


4、更改界面


5、系統架構圖


 6、重要代碼(接口)

public interface GoodsMapper {
    List<Goods> selectAll();
    int delete(int gId);
    boolean insert(Goods record);
    boolean update(Goods record,int gId);
    List<Goods> findByGno(int gId);
    Goods findByGId(int gId);
}


7、實現類

package com.service;

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

import com.mapper.GoodsMapper;
import com.model.Goods;
import com.utils.DBUtils;


//#f91570
public class GoodsService implements GoodsMapper {
    Connection con=null;

    @Override
    public List<Goods> selectAll() {
        List<Goods> list=new ArrayList<Goods>();//定義list集合
        try {
                con=DBUtils.getConnection();//獲取連接
                String sql="select * from Goods";//
                PreparedStatement pst=con.prepareStatement(sql);//執行sql
                ResultSet rs=pst.executeQuery();//獲取查詢結果集
                while(rs.next()){//ֹ
                    Goods record=new Goods();//實例化對象
                    record.setgId(rs.getInt("gId"));
                    record.setGoodsName(rs.getString("goodsName"));
                    record.setGoodsCount(rs.getInt("goodsCount"));
                    record.setGoodsPrice(rs.getDouble("goodsPrice"));
                    record.setCreateTime(rs.getDate("createTime"));
                    list.add(record);//丟到list集合裏
                }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            DBUtils.closeConnection(con);//關閉連接
        }
        return list;//返回list集合
        
        
    }

    @Override
    public int delete(int gId) {
         try {
                con=DBUtils.getConnection();
                String sql="delete from goods where gId=?";
                PreparedStatement pst=con.prepareStatement(sql);
                pst.setInt(1,gId);
                pst.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            } finally{
                DBUtils.closeConnection(con);
            }  
         return 1;
        
    }

    @Override
    public boolean insert(Goods record) {
          String sql="insert into goods(gId,goodsName,goodsPrice,goodsCount,createTime) values(?,?,?,?,?)";
            try {
                con=DBUtils.getConnection();
                PreparedStatement pst=con.prepareStatement(sql);
                pst=con.prepareStatement(sql);
                pst.setInt(1, record.getgId());
                pst.setString(2, record.getGoodsName());
                pst.setDouble(3, record.getGoodsPrice());
                pst.setInt(4,record.getGoodsCount());
                pst.setDate(5, record.getCreateTime());
                pst.execute();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally{
                DBUtils.closeConnection(con);
            }
    }

    @Override
    public boolean update(Goods record, int gId) {
        try{
               con=DBUtils.getConnection();
             String sql="update Goods set goodsName=?,goodsPrice=?,goodsCount=?,createTime=? where gId=?";
            PreparedStatement pst=con.prepareStatement(sql);
            pst.setString(1,record.getGoodsName());
            pst.setDouble(2,record.getGoodsPrice());
            pst.setInt(3,record.getGoodsCount());
            pst.setDate(4, record.getCreateTime());
            pst.setInt(5,gId);
            pst.execute();
            return true;
         }catch(Exception e){
             e.printStackTrace();
             return false;
         }finally{
             DBUtils.closeConnection(con);
         }
    }

    @Override
    public List<Goods> findByGno(int gId) {
        Connection con=null; 
        List<Goods> list =new ArrayList<Goods>();
        try{
            con=DBUtils.getConnection();
            String sql="select * from Goods where gId like ?";
            PreparedStatement pst=con.prepareStatement(sql);
            pst.setString(1, "%"+gId+"%");
            ResultSet rs=pst.executeQuery();
            while(rs.next()) {
                Goods record =new Goods();
                record.setgId(rs.getInt("gId"));
                record.setGoodsName(rs.getString("goodsName"));
                record.setGoodsCount(rs.getInt("goodsCount"));
                record.setGoodsPrice(rs.getDouble("goodsPrice"));
                record.setCreateTime(rs.getDate("createTime"));
                list.add(record);
            }

            return list;
            }catch(Exception e){
            e.printStackTrace();
                return null;
        }finally{
            DBUtils.closeConnection(con);
        }
    }

    @Override
    public Goods findByGId(int gId) {
         try{
                con=DBUtils.getConnection();
                   String sql="select * from Goods where gId=?";
                   PreparedStatement pst=con.prepareStatement(sql);
                   pst.setInt(1,gId);
                   ResultSet rs=pst.executeQuery();
                   if(rs.next()){
                       Goods record=new Goods();
                       record.setgId(rs.getInt("gId"));
                       record.setGoodsName(rs.getString("goodsName"));
                    record.setGoodsCount(rs.getInt("goodsCount"));
                    record.setGoodsPrice(rs.getDouble("goodsPrice"));
                    record.setCreateTime(rs.getDate("createTime"));
                        return record;
                   }else{
                       return null;
                   }
               }catch(Exception e){
                   e.printStackTrace();
                   return null;
               }finally{
                   DBUtils.closeConnection(con);
               }        
    }
    public static void main(String[] args) {
        GoodsService s=new GoodsService();
        /*List<Student> list=s.selectAll();
        for(Student st:list) {
            System.out.println(st.getsAddress());
        }*/
        Goods stu=new Goods();
        //stu.setsNo(sNo);
        /*stu.setsName("");
        stu.setsAge(21);
        stu.setsAddress("");
        stu.setSchool("海院");
        boolean b=s.update(stu, 1211);*/
        //System.out.println(b);
        List<Goods> list=s.selectAll();
        for(Goods st:list) {
            System.out.println(st.getGoodsName());
        }
    }

}
 


8、總結

代碼比較多,我只選擇了重要的代碼,希望幫到大家。有問題的可以加我qq1728608455,歡迎諮詢。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章