ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SMBMS项目搭建

2021-08-05 21:02:40  阅读:195  来源: 互联网

标签:项目 rs try connection SMBMS static params pstm 搭建


SMBMS项目搭建

开始进行SMBMS(超市订单管理系统)项目的分析。

1. SMBMS结构

2. 数据库模型

  • smbms_user:用户表
  • smbms_role:职责表
  • smbms_bill:订单表
  • smbms_provider:供应商表
  • smbms_address:地址表

3. 项目搭建准备

  1. 创建Maven项目

    使用Webapp模板创建,或创建普通项目后添加Webapp框架。

  2. 配置Tomcat,测试运行

  3. 导入Maven依赖

    Servlet、jsp、jstl、standard、mysql...

  4. 创建项目包结构

    com.qiyuan.servlet、entity、dao、filter、service、util...

  5. 编写实体类

    ORM映射:表 -> 实体类 int -> Interger?

  6. 编写基础公共类

    6.1 数据库配置文件db.properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username=root
    password=0723
    

    6.2 编写操作数据库基类

    package com.qiyuan.dao;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    /**
     * @ClassName BaseDao
     * @Description TODO
     * @Author Qiyuan
     * @Date 2021/8/5 18:32
     * @Version 1.0
     **/
    public class BaseDao {
        private static String driver;
        private static String url;
        private static String username;
        private static String password;
        // 静态代码块,在类加载的时候就执行
        static {
            Properties properties = new Properties();
            // 通过类加载器获取对应的资源
            InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.propertoes");
            try {
                properties.load(is);
            } catch (IOException e) {
                e.printStackTrace();
            }
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        }
    
        // 获取数据库连接
        public static Connection connection() {
            Connection connection = null;
            try {
                Class.forName(driver);
                connection = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return connection;
        }
        /**
         * 查询数据库的公共操作
         * @param connection,sql,params,rs,ptsm
         */
        public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet rs, PreparedStatement pstm) {
            try {
                pstm = connection.prepareStatement(sql);
                for (int i = 0; i < params.length; i++) {
                    pstm.setObject(i + 1, params[i]);
                }
                rs = pstm.executeQuery();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return rs;
        }
        /**
         * 更新数据库的公共操作
         * @param connection,sql,params,ptsm
         */
        public static int execute(Connection connection, String sql, Object[] params, PreparedStatement pstm) {
            int updateRows = 0;
            try {
                pstm = connection.prepareStatement(sql);
                for (int i = 0; i < params.length; i++) {
                    pstm.setObject(i + 1, params[i]);
                }
                updateRows = pstm.executeUpdate();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return updateRows;
        }
        /**
         * 释放资源
         * @param connection,pstm,rs
         */
        public static boolean closeResource(Connection connection, PreparedStatement pstm, ResultSet rs) {
            boolean flag = true;
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;//GC回收
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
            if (pstm != null) {
                try {
                    pstm.close();
                    pstm = null;//GC回收
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                    connection = null;//GC回收
                } catch (SQLException e) {
                    e.printStackTrace();
                    flag = false;
                }
            }
            return flag;
        }
    }
    

    6.3 编写字符编码过滤器

  7. 导入静态资源

    css、js、img...

✨准备工作完成!

标签:项目,rs,try,connection,SMBMS,static,params,pstm,搭建
来源: https://www.cnblogs.com/qiyuanc/p/smbms.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有