课设:点餐小程序的设计与实现(三)

153 阅读5分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第3天,点击查看活动详情

点餐小程序的设计与实现(三)

Eclipse简介与Tomcat简介

       Eclipse是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。它专注于为高度集成的工具开发提供一个全功能的、具有商业品质的工业平台。它主要由Eclipse项目、Eclipse工具项目和Eclipse技术项目三个项目组成,具体包括四个部分组成——Eclipse Platform、JDT、CDT和PDE。JDT支持Java开发、CDT支持C开发、PDE用来支持插件开发,Eclipse Platform则是一个开放的可扩展IDE,提供了一个通用的开发平台。它提供建造块和构造并运行集成软件开发工具的基础。Eclipse Platform允许工具建造者独立开发与他人工具无缝集成的工具从而无须分辨一个工具功能在哪里结束,而另一个工具功能在哪里开始。

       Tomcat是一种小型web程序服务器,由于它是免费开源的,并且支持访问量较少,所以常常被用来私人使用,J2EE程序设计中成为最好的选择。

这个课设的后台使用了java语言,核心代码如下:

使用到的jar包:

image.png

login.java

package xxx;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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.google.gson.Gson;

/**
 * Servlet implementation class login
 */
@WebServlet("/login")
public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;

    
    public login() {
       
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/json;charset=UTF-8");
		Connection cn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String phone = request.getParameter("phone");
		Gson gson = new Gson();
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC","root","010206");
			ps = cn.prepareStatement("select * from login where username=? and password=?and phone=?");
			ps.setString(1, username);
			ps.setString(2, password);
			ps.setString(3, phone);
			rs = ps.executeQuery();
			if(rs.next()) {
				System.out.println("登陆成功!");
				String json = gson.toJson("true");
				response.getWriter().write(json);
			}else {
				System.out.println("账号或密码不正确!");
				String json = gson.toJson("false");
				response.getWriter().write(json);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs!=null)
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(ps!=null)
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(cn!=null)
				cn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

update.java

package xxx;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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.google.gson.Gson;


@WebServlet("/update")
public class update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public update() {
        super();
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/json;charset=UTF-8");
		Connection cn = null;
		PreparedStatement ps = null;
		Gson gson = new Gson();
		String username = request.getParameter("username");
		String phone = request.getParameter("phone");
		try {
		if(cha(username,phone)) {
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		if(password1.equals(password2)) {

		if((!username.equals("")) && (!password1.equals("")) && (!phone.equals(""))) {
			Class.forName("com.mysql.cj.jdbc.Driver");
			cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC","root","010206");
			ps = cn.prepareStatement("update login set password=? where username=? ");
		}else {
			System.out.println("修改信息不能为空!");
			response.getWriter().write(gson.toJson(1));
		}
		
			ps.setString(1, password1);
			ps.setString(2, username);
			ps.executeUpdate();
			 if(cha(username,phone)) { System.out.println("修改成功!"); String json = gson.toJson("true");
			  response.getWriter().write(json); }else { System.out.println("修改失败!"); String
			  json = gson.toJson("false"); response.getWriter().write(json); }
			 
					 
		}else {
				System.out.println("两次密码不一致,请重新输入!");
				response.getWriter().write(gson.toJson(0));
			}
				}else {
					System.out.println("用户名或电话号不正确!");
				response.getWriter().write(gson.toJson(-1));
				}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(ps!=null)
				ps.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if(cn!=null)
				cn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	public static Connection method() throws Exception {
		Class.forName("com.mysql.cj.jdbc.Driver");
		Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC","root","010206");
		return cn;
	}
	
	public static boolean cha(String username,String phone) throws Exception {
		boolean flag;
		Connection cn = method();
		PreparedStatement ps = cn.prepareStatement("select * from login where username = ? and phone = ?");
		ps.setString(1, username);
		ps.setString(2, phone);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			flag = true;
		}else {
			flag = false;
		}
		rs.close();
		ps.close();
		return flag;
	}
	
	public static boolean chauser(String username,String password,String phone) throws Exception {
		boolean flag;
		Connection cn = method();
		PreparedStatement ps = cn.prepareStatement("select * from login where username = ? and password= ? and phone = ?");
		ps.setString(1, username);
		ps.setString(2, password);
		ps.setString(3, phone);
		
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			flag = true;
		}else {
			flag = false;
		}
		rs.close();
		ps.close();
		return flag;
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

zhuce.java

package xxx;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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.google.gson.Gson;

/**
 * Servlet implementation class zhuce
 */
@WebServlet("/zhuce")
public class zhuce extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public zhuce() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/json;charset=UTF-8");
		Connection cn = null;
		PreparedStatement ps = null;
		Gson gson = new Gson();
		String username = request.getParameter("username");
		try {
		if(!cha(username)) {
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		System.out.println(username);
		if(password1.equals(password2)) {
		String phone = request.getParameter("phone");
		if((!username.equals("")) && (!password1.equals("")) && (!phone.equals(""))) {
			Class.forName("com.mysql.cj.jdbc.Driver");
			cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC","root","010206");
			ps = cn.prepareStatement("insert into login values (?,?,?)");
		}else {
			System.out.println("注册信息不能为空!");
			response.getWriter().write(gson.toJson(1));
		}
			ps.setString(1, username);
			ps.setString(2, password1);
			ps.setString(3, phone);
			int num = ps.executeUpdate();
			if(num > 0) {
				System.out.println("注册成功!");
				String json = gson.toJson("true");
				response.getWriter().write(json);
			}else {
				System.out.println("注册失败!");
				String json = gson.toJson("false");
				response.getWriter().write(json);
			}
		}else {
				System.out.println("两次密码不一致,请重新输入!");
				response.getWriter().write(gson.toJson(0));
			}
				}else {
					System.out.println("用户名重复,请重新输入!");
				response.getWriter().write(gson.toJson(-1));
				}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(ps!=null)
				ps.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if(cn!=null)
				cn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	public static Connection method() throws Exception {
		Class.forName("com.mysql.cj.jdbc.Driver");
		Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC","root","010206");
		return cn;
	}
	
	public static boolean cha(String username) throws Exception {
		boolean flag;
		Connection cn = method();
		PreparedStatement ps = cn.prepareStatement("select * from login where username = ?");
		ps.setString(1, username);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			flag = true;
		}else {
			flag = false;
		}
		rs.close();
		ps.close();
		return flag;
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

课程设计总结与体会

       随着Internet技术的发展,人们的日常生活已经离不开网络。未来社会人们的生活和工作将越来越依赖于数字技术的发展,越来越数字化、网络化、电子化、虚拟化、Internet的发展历程以及目前的应用状况和发展趋势,可以充分地相信网络技术将极大的改变我们的生活和工作方式,甚至社会的价值观也会发生某种变化。

§1 课程设计总结

       本文按照软件工程开发设计方法,实现了一个点餐微信小程序,本系统采用JDBC技术,数据库MySQL,开发工具Eclipse以及微信开发者工具,实现了小程序系统用户登录、图片加载、地图位置获取、音乐播放等具体功能的操作。所有功能全部都了通过测试。

§2 心得体会

       在这次课程设计中,我学到了。。。。。

尾言

       这是课设的后端设计以及部分代码,至此,整个简单的小程序课设——点餐小程序的设计就完成了,如果觉得还不错的话,欢迎点赞收藏哦。