?? 00950124e946001c1160e657ddb92502
字號:
package hall;
/**
* Project: NWPU online shop
* JDK version used: jdk1.5.0
* Version: 1.01
* class eshop 用來處理關(guān)于用戶的各種操作
**/
import java.sql.*;
import java.util.Vector;
public class Eshop {
private Customers user = new Customers(); //新的用戶對象
private Vector userlist; //顯示用戶列表向量數(shù)組
private int page = 1; //顯示的頁碼
private int pageSize = 8; //每頁顯示的商品數(shù)
private int pageCount = 0; //頁面總數(shù)
private long recordCount = 0; //查詢的記錄總數(shù)
private DBWrapper myConnection = null;
private String sqlStr = "";
/**
* 默認(rèn)構(gòu)造函數(shù)
*/
public Eshop() throws Exception {
myConnection = DBWrapper.Instance();
}
/**
* int checkUserName(String inName)
* Description :檢查用戶名是哪種類型的
* @param String 輸入的用戶名
* @return int 不同的數(shù)字代表不同的類型
*/
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 :將成功注冊用戶寫入數(shù)據(jù)庫
* @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 增加的錢數(shù)
* @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) {
return false;
}
}
/**
* boolean get_alluser()
* Description :管理員查看所有用戶信息
* @param
* @return boolean 返回操作是否成功信息
*/
public boolean get_alluser() throws Exception {
sqlStr = "select count(*) from customers"; //取出記錄數(shù)
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;
}
//設(shè)定有多少pageCount
if (recordCount < 1)
pageCount = 0;
else
pageCount = (int) (recordCount - 1) / pageSize + 1;
//檢查查看的頁面數(shù)是否在范圍內(nèi)
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 :根據(jù)用戶名刪除數(shù)據(jù)庫中的用戶信息
* @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 :根據(jù)用戶名將數(shù)據(jù)庫中的用戶信息放入Customers實(shí)例中
* 再放入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 :得到要顯示的頁數(shù)
* @return int
*/
public int getPage() {
return page;
}
/**
* void setPage(int newpage)
* Description :修改要顯示的頁數(shù)
* @param int
*/
public void setPage(int newpage) {
page = newpage;
}
/**
* int getPageSize()
* Description :得到每頁要顯示的商品數(shù)
* @return int
*/
public int getPageSize() {
return pageSize;
}
/**
* void setPageSize(int newpsize)
* Description :修改每頁要顯示的商品數(shù)
* @param int
*/
public void setPageSize(int newpsize) {
pageSize = newpsize;
}
/**
* int getPageCount()
* Description :得到頁面總數(shù)
* @return int
*/
public int getPageCount() {
return pageCount;
}
/**
* void setPageCount(int newpcount)
* Description :修改頁面總數(shù)
* @param int
*/
public void setPageCount(int newpcount) {
pageCount = newpcount;
}
/**
* int getRecordCount()
* Description :得到記錄總數(shù)
* @return long
*/
public long getRecordCount() {
return recordCount;
}
/**
* void setRecordCount(long newrcount)
* Description :修改記錄總數(shù)
* @param long
*/
public void setRecordCount(long newrcount) {
recordCount = newrcount;
}
/**
* Vector getUserlist()
* Description :得到存儲用戶的Vector
* @return Vector
*/
public Vector getUserlist() {
return userlist;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -