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

📄 c042cc8b7245001c1e82be6cd01e78c9

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

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Vector;
//import javax.servlet.http.HttpServletRequest;
import java.util.*;

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

	//private javax.servlet.http.HttpServletRequest request; 
	private Vector userlist; //显示用户列表向量数组

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

	private int pageSize = 8; //每页显示的图书数

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

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

	private String message = ""; //出错信息提示

	private String username = ""; //注册后返回的用户名

	private DBWrapper myConnection = null;

	private String sqlStr = "";

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

	
	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;
	}

	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() + ")";
			//System.out.println(sql);
			myConnection.runUpdate(sql);
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}

	//查询书店所有的用户
	public boolean get_alluser() throws Exception {
		sqlStr = "select count(*) from customers"; //取出记录数
		int rscount = pageSize;
		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;
		rscount = (int) recordCount % pageSize; // 最后一页记录数
		//sql为倒序取值
		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()) {
						//User user = new User();
						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);
						System.out.println();
					} else {
						break;
					}
				
			}
			System.out.println(userlist.size());
			rs.close();
			return true;
		} catch (SQLException e) {
			System.out.print(e.getMessage());
			return false;
		}

	}

	public boolean checkPasswd(String inName, String inPasswd) throws Exception {
		boolean flag = false;
		ResultSet r = null;
		String sqlQuery = "select password from customers where name='"
				+ inName + "'";

		try {
			r = myConnection.runQuery(sqlQuery);
			r.next();
			String tempPd = r.getString("password");
			if (tempPd.equals(inPasswd))
				flag = true;
		} catch (SQLException e) {
			flag = false;
		}
		return flag;
	}

	public boolean updatePasswd(String inName, String inPasswd)
			throws Exception {

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

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

	//修改用户
	public boolean update(Customers inUser) throws Exception {

		sqlStr = "update customers 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) {
			System.out.print(e.getMessage());
			return false;
		}

	}

	//删除用户
	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;
		}
	}

	//查询指定id的用户,用于支持页面的查看详细资料请求
	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;
		}
	}

	public int getPage() { //显示的页码
		return page;
	}

	public void setPage(int newpage) {
		page = newpage;
	}

	public int getPageSize() { //每页显示的图书数
		return pageSize;
	}

	public void setPageSize(int newpsize) {
		pageSize = newpsize;
	}

	public int getPageCount() { //页面总数
		return pageCount;
	}

	public void setPageCount(int newpcount) {
		pageCount = newpcount;
	}

	public long getRecordCount() {
		return recordCount;
	}

	public void setRecordCount(long newrcount) {
		recordCount = newrcount;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String msg) {
		message = msg;
	}

	public void setUserName(String uName) {
		username = uName;
	}

	public String getUserName() {
		return username;
	}

	public Vector getUserlist() {
		return userlist;
	}

	/* public static void main(String args[]){ 
	 try{
	 Eshop e= new Eshop();
	 if(e.checkPasswd("qwert", "123456")){
	 System.out.println("0000");
	 }
	 else System.out.println("111");
	 if(e.updatePasswd("qwert", "0000000")){
	 System.out.println("chenggonh");
	 }
	 else System.out.println("fail");
	 }catch(Exception e){ 
	 System.out.println("12468978");
	 e.printStackTrace();
	 }
	 
	 }
	 */

}

⌨️ 快捷键说明

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