UserManager.java
上传用户:yuyunping
上传日期:2013-03-21
资源大小:1844k
文件大小:14k
源码类别:

Java书籍

开发平台:

Java

  1. package net.acai.forum;
  2. /**
  3.  * Title:        清清网络
  4.  * Description:
  5.  * Copyright:    Copyright (c) 2002
  6.  * Company:      www.SuperSpace.com
  7.  * @author:       SuperSpace
  8.  * @version 1.0
  9.  */
  10. import net.acai.forum.*;
  11. import net.acai.database.*;
  12. import java.sql.*;
  13. import java.util.Vector;
  14. import javax.servlet.http.*;
  15. import net.acai.util.*;
  16. public class UserManager{
  17. public static void createUser(String username,String password)
  18. throws UserAlreadyExistException,Exception{
  19. DBConnect dbc=new DBConnect("select * from bbs.myuser where UserName=?");
  20. dbc.setBytes(1,(new String(username.getBytes("ISO-8859-1"),"GBK")).getBytes());
  21. java.sql.ResultSet rs=dbc.executeQuery();
  22. if(rs.next())
  23. throw new UserAlreadyExistException();
  24. else{
  25. dbc.clearParameters();
  26. dbc.prepareStatement("insert into bbs.myuser (UserName,UserPassword) values(?,?)");
  27. dbc.setString(1,username);
  28. dbc.setString(2,password);
  29. dbc.executeUpdate();
  30. }
  31. dbc.close();
  32. }
  33. public static User findUser(String userName) throws UserNotFoundException,Exception{
  34. try{
  35. DBConnect dbc=new DBConnect("select * from bbs.myuser where UserName like ?");
  36. dbc.setBytes(1,(new String(userName.trim().getBytes("ISO-8859-1"),"GBK")).getBytes());
  37. ResultSet rs=dbc.executeQuery();
  38. if(rs.next()){
  39. //此处对USER的各种属性进行定义了
  40. User tempUser=new User();
  41. tempUser.setUserID(rs.getInt(1));
  42. tempUser.setUserName(rs.getString(2));
  43. tempUser.setUserEmail(rs.getString(3));
  44. tempUser.setArticle(rs.getInt(4));
  45. tempUser.setUserPassword(rs.getString(5));
  46. tempUser.setSign(rs.getString(6));
  47. tempUser.setSex(rs.getString(7));
  48. tempUser.setHomePage(rs.getString(8));
  49. tempUser.setAddDate(rs.getString(9));
  50. tempUser.setLogins(rs.getInt(10));
  51. tempUser.setFace(rs.getString(11));
  52. tempUser.setWidth(rs.getInt(12));
  53. tempUser.setHeight(rs.getInt(13));
  54. tempUser.setOicq(rs.getString(14));
  55. tempUser.setLastLogin(rs.getString(15));
  56. tempUser.setBbsType(rs.getInt(16));
  57. tempUser.setUserClass(rs.getInt(18));
  58. tempUser.setUserGroup(rs.getString(19));
  59. tempUser.setUserWealth(rs.getInt(20));
  60. tempUser.setUserEP(rs.getInt(21));
  61. tempUser.setUserCP(rs.getInt(22));
  62. tempUser.setTitle(rs.getString(23));
  63. tempUser.setReann(rs.getString(25));
  64. dbc.close();
  65. return tempUser;
  66. }
  67. else{
  68. dbc.close();
  69. throw new UserNotFoundException("<li>对不起,没有发现此用户"+userName+"</li>");
  70. }
  71. }
  72. catch(Exception e){
  73. e.printStackTrace();
  74. throw new UserNotFoundException(e.getMessage());
  75. }
  76. }
  77. public static Vector findUsers(String userName) throws UserNotFoundException,Exception{
  78. try{
  79. DBConnect dbc=new DBConnect("select * from bbs.myuser where UserName like ?");
  80. dbc.setBytes(1,(new String(("%"+userName.trim()+"%").getBytes("ISO-8859-1"),"GBK")).getBytes());
  81. ResultSet rs=dbc.executeQuery();
  82. if(!rs.next()){
  83. dbc.close();
  84. throw new Exception();
  85. }
  86. Vector userVector=new Vector();
  87. do{
  88. //此处对USER的各种属性进行定义了
  89. User tempUser=new User();
  90. tempUser.setUserID(rs.getInt(1));
  91. tempUser.setUserName(rs.getString(2));
  92. tempUser.setUserEmail(rs.getString(3));
  93. tempUser.setArticle(rs.getInt(4));
  94. tempUser.setUserPassword(rs.getString(5));
  95. tempUser.setSign(rs.getString(6));
  96. tempUser.setSex(rs.getString(7));
  97. tempUser.setHomePage(rs.getString(8));
  98. tempUser.setAddDate(rs.getString(9));
  99. tempUser.setLogins(rs.getInt(10));
  100. tempUser.setFace(rs.getString(11));
  101. tempUser.setWidth(rs.getInt(12));
  102. tempUser.setHeight(rs.getInt(13));
  103. tempUser.setOicq(rs.getString(14));
  104. tempUser.setLastLogin(rs.getString(15));
  105. tempUser.setBbsType(rs.getInt(16));
  106. tempUser.setUserClass(rs.getInt(18));
  107. tempUser.setUserGroup(rs.getString(19));
  108. tempUser.setUserWealth(rs.getInt(20));
  109. tempUser.setUserEP(rs.getInt(21));
  110. tempUser.setUserCP(rs.getInt(22));
  111. tempUser.setTitle(rs.getString(23));
  112. tempUser.setReann(rs.getString(25));
  113. userVector.add(tempUser);
  114. }
  115. while(rs.next());
  116. dbc.close();
  117. return userVector;
  118. }
  119. catch(Exception e){
  120. e.printStackTrace();
  121. throw new UserNotFoundException(e.getMessage());
  122. }
  123. }
  124. public static void updateUser(HttpServletRequest request) throws Exception{
  125. String userName=ParamUtil.getString(request,"userName","");
  126. String userPassword=ParamUtil.getString(request,"userPassword","");
  127. String userEmail=ParamUtil.getString(request,"userEmail","");
  128. String face=ParamUtil.getString(request,"face","");
  129. int width=ParamUtil.getInt(request,"width",0);
  130. int height=ParamUtil.getInt(request,"height",0);
  131. String oicq=ParamUtil.getString(request,"oicq");
  132. String sign=ParamUtil.getString(request,"sign","");
  133. String myFace=ParamUtil.getString(request,"myface","");
  134. boolean foundErr=false;
  135. String errMSG="";
  136. int sex=0;
  137. int showRe=0;
  138. if("".equals(userName)||userName.length()>20)
  139. {
  140. errMSG=errMSG+"<br>"+"<li>请输入您的用户名(长度不能大于20)。";
  141. foundErr=true;
  142. }
  143. if (userName.indexOf('=')>-1||userName.indexOf('%')>-1||userName.indexOf('?')>-1||userName.indexOf('&')>-1 || userName.indexOf(';')>-1 ||userName.indexOf(',')>0 || userName.indexOf(''')>-1 || userName.indexOf('+') >-1){
  144. errMSG=errMSG+"<br>"+"<li>用户名中含有非法字符。";
  145. foundErr=true;
  146. }
  147. try{
  148. sex=ParamUtil.getInt(request,"sex");
  149. }
  150. catch(NumberFormatException e)
  151. {
  152. errMSG=errMSG+"<br>"+"<li>请选择您的性别。";
  153. foundErr=true;
  154. }
  155. if (userEmail.indexOf('@')<0||userEmail.indexOf('.')<0){
  156. errMSG=errMSG+"<br>"+"<li>您的Email有错误。";
  157.     foundErr=true;
  158.     }
  159.     if (!"".equals(myFace)){
  160.     if (width==0 || height==0){
  161. errMSG=errMSG+"<br>"+"<li>请输入图片的宽度和高度。";
  162. foundErr=true;
  163. }
  164. else if (width<20|| width>80){
  165. errMSG=errMSG+"<br>"+"<li>您输入的图片宽度不符合标准。";
  166. foundErr=true;
  167. }
  168. else if (height<20 || height>80){
  169. errMSG=errMSG+"<br>"+"<li>您输入的图片高度不符合标准。";
  170. foundErr=true;
  171. }
  172. else face=myFace;
  173. }
  174. else
  175. if ("".equals(face)){
  176. errMSG=errMSG+"<br>"+"<li>请选择您的个性头像。";
  177. foundErr=true;
  178. }
  179. else if (face.endsWith(".gif")){
  180. width=32;
  181. height=32;
  182. }
  183. else{
  184. errMSG=errMSG+"<br>"+"<li>您选择了错误的头像。";
  185. foundErr=true;
  186. }
  187. if(oicq==null)
  188. oicq="";
  189. else
  190. try{
  191. Integer.parseInt(oicq);
  192. }
  193. catch(NumberFormatException e)
  194. {
  195. errMSG=errMSG+"<br>"+"<li>Oicq号码只能是4-10位数字,您可以选择不输入。";
  196. foundErr=true;
  197. }
  198. if(!foundErr)
  199. {
  200. try{
  201. DBConnect dbc=new DBConnect("select * from bbs.myuser where username=? ");
  202. dbc.setBytes(1,(new String(userName.getBytes("ISO-8859-1"),"GBK")).getBytes());
  203. ResultSet rs=dbc.executeQuery();
  204. if(rs.next()){
  205. MD5 md5=new MD5();
  206. dbc.clearParameters();
  207. dbc.prepareStatement("update bbs.myuser set userPassword=?,userEmail=?,sign=?,oicq=?,sex=?,face=?,width=?,height=? where userName=?");
  208. dbc.setBytes(1,(new String(md5.getMD5ofStr(userPassword).getBytes("ISO-8859-1"),"GBK")).getBytes());
  209. dbc.setBytes(2,(new String(userEmail.getBytes("ISO-8859-1"),"GBK")).getBytes());
  210. dbc.setBytes(3,(new String(sign.getBytes("ISO-8859-1"),"GBK")).getBytes());
  211. dbc.setBytes(4,(new String(oicq.getBytes("ISO-8859-1"),"GBK")).getBytes());
  212. dbc.setInt(5,sex);
  213. dbc.setBytes(6,(new String(face.getBytes("ISO-8859-1"),"GBK")).getBytes());
  214. dbc.setInt(7,width);
  215. dbc.setInt(8,height);
  216. dbc.setBytes(9,(new String(userName.getBytes("ISO-8859-1"),"GBK")).getBytes());
  217. dbc.executeUpdate();
  218. }
  219. else
  220. throw new UserNotFoundException();
  221. //ForumPropertiesManager.resetManager();
  222. dbc.close();
  223. }
  224. catch(UserNotFoundException e){
  225. errMSG=errMSG+"<br>"+"<li>对不起,您输入的用户名已经被注册,请重新输入。";
  226. throw new Exception(errMSG);
  227. }
  228. catch(Exception e){
  229. e.printStackTrace();
  230. throw e;
  231. }
  232. }
  233. if(foundErr)
  234. throw new Exception(errMSG);
  235. }
  236. public static void delUser(HttpServletRequest request) throws Exception{
  237. String [] userID=request.getParameterValues("userID");
  238. DBConnect dbc=new DBConnect();
  239. String sql;
  240. for(int i=0;i<userID.length;i++){
  241. sql="delete from bbs.myuser where userID="+userID[i];
  242. dbc.executeUpdate(sql);
  243. }
  244. dbc.close();
  245. }
  246. public static void addAdmin(HttpServletRequest request,int wealthReg,int epReg,int cpReg) throws Exception{
  247. String userName=ParamUtil.getString(request,"userName","");
  248. String psw=ParamUtil.getString(request,"psw","");
  249. String userPassword="";
  250. String pswc=ParamUtil.getString(request,"pswc","");
  251. String userEmail=ParamUtil.getString(request,"userEmail","");
  252. String face=ParamUtil.getString(request,"face","");
  253. int width=ParamUtil.getInt(request,"width",0);
  254. int height=ParamUtil.getInt(request,"height",0);
  255. String oicq=ParamUtil.getString(request,"oicq");
  256. String sign=ParamUtil.getString(request,"sign","");
  257. String myFace=ParamUtil.getString(request,"myface","");
  258. boolean foundErr=false;
  259. String errMSG="";
  260. int sex=0;
  261. int showRe=0;
  262. if("".equals(userName)||userName.length()>20)
  263. {
  264. errMSG=errMSG+"<br>"+"<li>请输入您的用户名(长度不能大于20)。";
  265. foundErr=true;
  266. }
  267. if (userName.indexOf('=')>-1||userName.indexOf('%')>-1||userName.indexOf('?')>-1||userName.indexOf('&')>-1 || userName.indexOf(';')>-1 ||userName.indexOf(',')>0 || userName.indexOf(''')>-1 || userName.indexOf('+') >-1){
  268. errMSG=errMSG+"<br>"+"<li>用户名中含有非法字符。";
  269. foundErr=true;
  270. }
  271. try{
  272. sex=ParamUtil.getInt(request,"sex");
  273. }
  274. catch(NumberFormatException e)
  275. {
  276. errMSG=errMSG+"<br>"+"<li>请选择您的性别。";
  277. foundErr=true;
  278. }
  279. try{
  280. showRe=ParamUtil.getInt(request,"showRe");
  281. }
  282. catch(NumberFormatException e){
  283. errMSG=errMSG+"<br>"+"<li>请选择您的帖子有回复时是否要提示您。";
  284. foundErr=true;
  285. }
  286. if("".equals(psw) || psw.length()>10){
  287. errMSG=errMSG+"<br>"+"<li>请输入您的密码(长度不能大于10)。";
  288. foundErr=true;
  289. }
  290. if (!pswc.equals(psw)){
  291. errMSG=errMSG+"<br>"+"<li>您输入的密码和确认密码不一致。";
  292. foundErr=true;
  293. }
  294. else
  295. userPassword=psw;
  296. if (userEmail.indexOf('@')<0||userEmail.indexOf('.')<0){
  297. errMSG=errMSG+"<br>"+"<li>您的Email有错误。";
  298.     foundErr=true;
  299.     }
  300.     if (!"".equals(myFace)){
  301.     if (width==0 || height==0){
  302. errMSG=errMSG+"<br>"+"<li>请输入图片的宽度和高度。";
  303. foundErr=true;
  304. }
  305. else if (width<20|| width>80){
  306. errMSG=errMSG+"<br>"+"<li>您输入的图片宽度不符合标准。";
  307. foundErr=true;
  308. }
  309. else if (height<20 || height>80){
  310. errMSG=errMSG+"<br>"+"<li>您输入的图片高度不符合标准。";
  311. foundErr=true;
  312. }
  313. else face=myFace;
  314. }
  315. else
  316. if ("".equals(face)){
  317. errMSG=errMSG+"<br>"+"<li>请选择您的个性头像。";
  318. foundErr=true;
  319. }
  320. else if (face.endsWith(".gif")){
  321. width=32;
  322. height=32;
  323. }
  324. else{
  325. errMSG=errMSG+"<br>"+"<li>您选择了错误的头像。";
  326. foundErr=true;
  327. }
  328. if(oicq==null)
  329. oicq="";
  330. else
  331. try{
  332. Integer.parseInt(oicq);
  333. }
  334. catch(NumberFormatException e)
  335. {
  336. errMSG=errMSG+"<br>"+"<li>Oicq号码只能是4-10位数字,您可以选择不输入。";
  337. foundErr=true;
  338. }
  339. if(!foundErr)
  340. {
  341. try{
  342. DBConnect dbc=new DBConnect("select * from bbs.myuser where username=?");
  343. dbc.setBytes(1,(new String(userName.getBytes("ISO-8859-1"),"GBK")).getBytes());
  344. ResultSet rs=dbc.executeQuery();
  345. if(rs.next()){
  346. dbc.close();
  347. throw new UserAlreadyExistException();
  348. }
  349. MD5 md5=new MD5();
  350. dbc.clearParameters();
  351. dbc.prepareStatement("insert into bbs.myuser ( userName,userPassword,userEmail,userClass,sign,oicq,article,lockuser,sex,showRe,addDate,face,width,height,logins,lastlogin,userWealth,userEP,userCP) values( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
  352. dbc.setBytes(1,(new String(userName.getBytes("ISO-8859-1"),"GBK")).getBytes());
  353. dbc.setBytes(2,(new String(md5.getMD5ofStr(userPassword).getBytes("ISO-8859-1"),"GBK")).getBytes());
  354. dbc.setBytes(3,(new String(userEmail.getBytes("ISO-8859-1"),"GBK")).getBytes());
  355. dbc.setInt(4,20);
  356. dbc.setBytes(5,(new String(sign.getBytes("ISO-8859-1"),"GBK")).getBytes());
  357. dbc.setBytes(6,(new String(oicq.getBytes("ISO-8859-1"),"GBK")).getBytes());
  358. dbc.setInt(7,0);
  359. dbc.setInt(8,0);
  360. dbc.setInt(9,sex);
  361. dbc.setInt(10,showRe);
  362. dbc.setString(11,Format.getDateTime());
  363. dbc.setBytes(12,(new String(face.getBytes("ISO-8859-1"),"GBK")).getBytes());
  364. dbc.setInt(13,width);
  365. dbc.setInt(14,height);
  366. dbc.setInt(15,1);
  367. dbc.setString(16,Format.getDateTime());
  368. dbc.setInt(17,wealthReg);
  369. dbc.setInt(18,epReg);
  370. dbc.setInt(19,cpReg);
  371. dbc.executeUpdate();
  372. dbc.clearParameters();
  373. dbc.prepareStatement("update bbs.config set usernum=usernum+1,lastUser=?");
  374. dbc.setBytes(1,userName.getBytes());
  375. dbc.executeUpdate();
  376. String forumName=ForumPropertiesManager.getString("ForumName");
  377. String sender=forumName;
  378. String title=forumName+"欢迎您的到来";
  379. String body=forumName+"全体管理人员欢迎您的到来n如有任何疑问请及时联系系统管理员。n如有任何使用上的问题请查看论坛帮助。n感谢您注册本系统,让我们一起来建设这个网上家园!";
  380. String sql="insert into bbs.message(incept,sender,title,content,sendtime,flag,issend) values(?,?,?,?,getdate(),0,1)";
  381. dbc.prepareStatement(sql);
  382. dbc.setBytes(1,(new String(userName.getBytes("ISO-8859-1"),"GBK")).getBytes());
  383. dbc.setBytes(2,(new String(sender.getBytes("ISO-8859-1"),"GBK")).getBytes());
  384. dbc.setBytes(3,(new String(title.getBytes("ISO-8859-1"),"GBK")).getBytes());
  385. dbc.setBytes(4,(new String(body.getBytes("ISO-8859-1"),"GBK")).getBytes());
  386. dbc.executeUpdate();
  387. dbc.close();
  388. ForumPropertiesManager.resetManager();
  389. }
  390. catch(UserAlreadyExistException e){
  391. errMSG=errMSG+"<br>"+"<li>对不起,您输入的用户名已经被注册,请重新输入。";
  392. throw new Exception(errMSG);
  393. }
  394. catch(Exception e){
  395. e.printStackTrace();
  396. throw e;
  397. }
  398. }
  399. if(foundErr)
  400. throw new Exception(errMSG);
  401. }
  402. }