⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eshop.java

📁 是一个网上手机超市
💻 JAVA
字号:
package hall;

/**
 * Project:    NWPU online shop
 * JDK version used: jdk1.5.0
 * Version: 	1.01
 * class eshop 用来处理关于用户的各种操作
 **/

import java.sql.*;
import java.util.Vector;

public class Eshop {
	private Customers user = new Customers(); //新的用户对象

	private Vector userlist; //显示用户列表向量数组

	private int page = 1; //显示的页码

	private int pageSize = 8; //每页显示的商品数

	private int pageCount = 0; //页面总数

	private long recordCount = 0; //查询的记录总数

	private DBWrapper myConnection = null;

	private String sqlStr = "";

	/**

	 * 默认构造函数

	 */
	public Eshop() throws Exception {
		myConnection = DBWrapper.Instance();
	}

	/**
	 * int checkUserName(String inName)
	 * Description :检查用户名是哪种类型的
	 * @param String 输入的用户名
	 * @return int 不同的数字代表不同的类型
	 */

	public int checkUserName(String inName) throws Exception {
		int flag = 3;// 1 represents admin,2 represents customer,3 represents
		// that the usename isn't exsited
		sqlStr = "select * from administrators where username = '" + inName + "'";
		ResultSet rs = myConnection.runQuery(sqlStr);

		if (rs.next()) {

			flag = 1;
		} else {
			sqlStr = "select * from customers where name = '" + inName + "'";
			rs = myConnection.runQuery(sqlStr);
			if (rs.next()) {
				flag = 2;
			} else {
				flag = 3;
			}
		}
		rs.close();
		return flag;
	}

	/**
	 * boolean addUser(Customers inUser)
	 * Description :将成功注册用户写入数据库
	 * @param Customers 
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean addUser(Customers inUser) {
		try {
			String sql = "INSERT INTO customers VALUES ('" + inUser.getName()
					+ "','" + inUser.getPassword() + "','" + inUser.getEmail()
					+ "','" + inUser.getSex() + "','" + inUser.getPhone()
					+ "','" + inUser.getMobilePhone() + "','"
					+ inUser.getState() + "','" + inUser.getProvince() + "','"
					+ inUser.getCity() + "','" + inUser.getStreet() + "',"
					+ inUser.getAge() + "," + inUser.getAccount() + ")";

			myConnection.runUpdate(sql);
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}

	/**
	 * boolean addMoney(String inName, String inAccount)
	 * Description :管理员给顾客冲值
	 * @param  String 用户名
	 * @param  String 增加的钱数
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean addMoney(String inName, String inAccount)throws Exception{
		double add;
		if(inAccount == null ||inAccount == ""){
			add = 0;
		}else{
			add = Double.parseDouble(inAccount);
		}
		sqlStr = "update customers set account = account + " + add
				+ " where name = '" + inName + "'";
		try {
			myConnection.runUpdate(sqlStr);

			return true;
		} catch (SQLException e) {
			System.out.println(e);
			return false;
		}
	}
	/**
	 * boolean get_alluser()
	 * Description :管理员查看所有用户信息
	 * @param  
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean get_alluser() throws Exception {
		sqlStr = "select count(*) from customers"; //取出记录数

		try {
			ResultSet rs1 = myConnection.runQuery(sqlStr);
			if (rs1.next())
				recordCount = rs1.getInt(1);
			System.out.println(recordCount);
			rs1.close();
		} catch (SQLException e) {
			System.out.print("count:" + e.getMessage());
			return false;
		}
		//设定有多少pageCount
		if (recordCount < 1)
			pageCount = 0;
		else
			pageCount = (int) (recordCount - 1) / pageSize + 1;
		//检查查看的页面数是否在范围内
		if (page < 1)
			page = 1;
		else if (page > pageCount)
			page = pageCount;

		sqlStr = "select  * from customers order by name";

		try {

			ResultSet rs = myConnection.runQuery(sqlStr);
			userlist = new Vector();
			if (page == 1) {

			} else {
				for (int i = 0; i < pageSize * (page - 1); i++) {
					rs.next();
				}
			}

			for (int i = 0; i < pageSize; i++) {

				if (rs.next()) {
					Customers user = new Customers();
					user.setName(rs.getString("name"));
					user.setPassword(rs.getString("password"));
					user.setEmail(rs.getString("email"));
					user.setSex(rs.getString("sex"));
					user.setPhone(rs.getString("phone"));
					user.setMobilePhone(rs.getString("mobilePhone"));
					user.setState(rs.getString("state"));
					user.setProvince(rs.getString("province"));
					user.setCity(rs.getString("city"));
					user.setStreet(rs.getString("street"));
					user.setAge(rs.getInt("age"));
					userlist.addElement(user);

				} else {
					break;
				}

			}
			rs.close();
			return true;
		} catch (SQLException e) {
			System.out.print(e.getMessage());
			return false;
		}

	}

	/**
	 * boolean checkPasswd(String inName, String inPasswd)
	 * Description :检查用户的名字和密码是否正确
	 * @param  String 用户名
	 * @param  String 密码
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean checkPasswd(String inName, String inPasswd) throws Exception {

		ResultSet r = null;
		String sqlQuery = "select password from customers where name='"
				+ inName + "'";

		try {
			r = myConnection.runQuery(sqlQuery);
			if (r.next()) {
				String tempPd = r.getString("password");

				if (tempPd.equals(inPasswd))
					return true;
				else
					return false;

			} else
				return false;
		} catch (SQLException e) {
			System.out.println(e);
			return false;
		}
	}

	/**
	 * boolean updatePasswd(String inName, String inPasswd)
	 * Description :修改用户密码
	 * @param  String 用户名
	 * @param  String 密码
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean updatePasswd(String inName, String inPasswd)
			throws Exception {

		sqlStr = "update customers set ";
		sqlStr = sqlStr + "password = '" + inPasswd + "' ";
		sqlStr = sqlStr + " where name = '" + inName + "'";
		try {
			myConnection.runUpdate(sqlStr);

			return true;
		} catch (SQLException e) {
			return false;
		}
	}

	/**
	 * boolean update(Customers inUser) 
	 * Description :修改用户密码
	 * @param  Customers
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean update(Customers inUser) throws Exception {

		sqlStr = "update costomers set ";
		sqlStr = sqlStr + "password = '" + inUser.getPassword() + "',";
		sqlStr = sqlStr + "email = '" + inUser.getEmail() + "',";
		sqlStr = sqlStr + "sex = '" + inUser.getSex() + "',";
		sqlStr = sqlStr + "phone = '" + inUser.getPhone() + "',";
		sqlStr = sqlStr + "mobilephone = '" + inUser.getMobilePhone() + "',";
		sqlStr = sqlStr + "state = '" + inUser.getState() + "',";
		sqlStr = sqlStr + "province = '" + inUser.getProvince() + "',";
		sqlStr = sqlStr + "city = '" + inUser.getCity() + "',";
		sqlStr = sqlStr + "street = '" + inUser.getStreet() + "',";
		sqlStr = sqlStr + "age = " + inUser.getAge() + " ";
		sqlStr = sqlStr + " where name = '" + inUser.getName() + "'";
		try {
			myConnection.runUpdate(sqlStr);
			return true;
		} catch (SQLException e) {
			return false;
		}

	}

	/**
	 * boolean delete(String inName)
	 * Description :根据用户名删除数据库中的用户信息
	 * @param  String
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean delete(String inName) throws Exception {
		sqlStr = "delete from customers where name = '" + inName + "'";
		try {
			myConnection.runUpdate(sqlStr);
			return true;
		} catch (SQLException e) {
			System.out.println(e);
			return false;
		}
	}

	/**
	 * boolean getUserinfo(String inName)
	 * Description :根据用户名将数据库中的用户信息放入Customers实例中
	 * 再放入Vector中供其他人或方法使用
	 * @param  String
	 * @return boolean 返回操作是否成功信息
	 */
	public boolean getUserinfo(String inName) throws Exception {
		try {
			sqlStr = "select  * from customers where name = '" + inName + "'";

			ResultSet rs = myConnection.runQuery(sqlStr);
			userlist = new Vector();
			while (rs.next()) {
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setEmail(rs.getString("email"));
				user.setSex(rs.getString("sex"));
				user.setPhone(rs.getString("phone"));
				user.setMobilePhone(rs.getString("mobilePhone"));
				user.setState(rs.getString("state"));
				user.setProvince(rs.getString("province"));
				user.setCity(rs.getString("city"));
				user.setStreet(rs.getString("street"));
				user.setAge(rs.getInt("age"));
				user.setAccount(rs.getDouble("account"));
				userlist.addElement(user);

			}
			rs.close();
			return true;
		} catch (Exception e) {
			System.out.print(e.getMessage());
			return false;
		}
	}

	/**
	 * int getPage()
	 * Description :得到要显示的页数
	 * @return int
	 */
	public int getPage() {
		return page;
	}

	/**
	 * void setPage(int newpage)
	 * Description :修改要显示的页数
	 * @param int
	 */
	public void setPage(int newpage) {
		page = newpage;
	}

	/**
	 * int getPageSize()
	 * Description :得到每页要显示的商品数
	 * @return int
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * void setPageSize(int newpsize)
	 * Description :修改每页要显示的商品数
	 * @param int
	 */
	public void setPageSize(int newpsize) {
		pageSize = newpsize;
	}

	/**
	 * int getPageCount()
	 * Description :得到页面总数
	 * @return int
	 */
	public int getPageCount() {
		return pageCount;
	}

	/**
	 * void setPageCount(int newpcount)
	 * Description :修改页面总数
	 * @param int
	 */
	public void setPageCount(int newpcount) {
		pageCount = newpcount;
	}

	/**
	 * int getRecordCount()
	 * Description :得到记录总数
	 * @return long
	 */
	public long getRecordCount() {
		return recordCount;
	}

	/**
	 * void setRecordCount(long newrcount)
	 * Description :修改记录总数
	 * @param long
	 */
	public void setRecordCount(long newrcount) {
		recordCount = newrcount;
	}

	/**
	 * Vector getUserlist()
	 * Description :得到存储用户的Vector 
	 * @return Vector
	 */
	public Vector getUserlist() {
		return userlist;
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -