




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、目 录 第一部分第一部分 功能功能代代码码.- 1 - 1.DBO.JAVA.- 1 - 2.LOG.JAVA.- 4 - 3.MD5.JAVA.- 5 - 4.COMMON.JAVA.- 6 - 5.CHECKCODE.JAVA.- 8 - 6.LOGINSERVLET.JAVA.- 9 - 7.TRAINBEAN.JAVA.- 13 - 第二部分第二部分 外部显示代码外部显示代码.- 24 - 1.INDEX.JSP.- 24 - 2.LOGIN.JSP.- 27 - 3.ERROR.JSP.- 31 - 4.INDEX.JSP.- 32 - 5.INDEX.JSP.- 35 - 第一部分
2、第一部分 内部功能代码内部功能代码 1. DBO.java package com.util; import java.sql.*; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import javax.activation.DataSource; import java.sql.*; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; im
3、port java.sql.Statement; import javax.naming.Context; import javax.naming.InitialContext; public class DBO private Connection conn; private Statement stmt; private DataSource ds; public DBO() /* 打开数据库 */ public void open() try Class.forName(org.gjt.mm.mysql.Driver); conn = DriverManager.getConnectio
4、n(jdbc:mysql:/localhost:3306/cpyd?user=root stmt=conn.createStatement(); System.out.println(打开数据库连接); catch (Exception ex) System.err.println(打开数据库时出错: + ex.getMessage(); /* 关闭数据库,将连接返还给连接池 */ public void close() try /connMgr.freeConnection(java, conn); conn.close(); System.out.println (释放连接); catch
5、 (SQLException ex) System.err.println(返还连接池出错: + ex.getMessage(); /* 执行查询 */ public ResultSet executeQuery(String sql) throws SQLException ResultSet rs = null; rs = stmt.executeQuery(sql); System.out.println (执行查询); return rs; /* 执行增删改 */ public int executeUpdate(String sql) throws SQLException int
6、ret = 0; ret = stmt.executeUpdate(sql); System.out.println (执行增删改); return ret; /* 将 SQL 语句加入到批处理 */ public void addBatch(String sql) throws SQLException stmt.addBatch(sql); /* 执行批处理 */ public int executeBatch() throws SQLException boolean isAuto=conn.getAutoCommit(); conn.setAutoCommit(false); int
7、updateCounts = stmt.executeBatch(); /mit(); /conn.setAutoCommit(isAuto); /conn.setAutoCommit(true); return updateCounts; public boolean getAutoCommit() throws SQLException return conn.getAutoCommit(); public void setAutoCommit(boolean auto) throws SQLException conn.setAutoCommit(auto); public void c
8、ommit() throws SQLException mit(); /this.close(); public void rollBack() throws SQLException conn.rollback(); /this.close(); 2. Log.java package com.util; /* * 日志 */ import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IO
9、Exception; import java.io.PrintWriter; public class Log public void addLog(String str) try File file=new File(c:/log.txt); FileWriter fw=new FileWriter(file,true); BufferedWriter bw=new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); pw.write(str); bw.newLine();/断行 bw.flush();/将数据更新至文件 pw.
10、close(); fw.close();/关闭文件流 catch (FileNotFoundException e) System.out.println(警告:日志文件没找到!); e.printStackTrace(); catch (IOException e) System.out.println(警告:日志文件 IO 错误!); e.printStackTrace(); 3. MD5.java package com.util; /* *MD5 密码加密类 * */ import java.security.*; import java.security.spec.*; public
11、 final class MD5 public final static String MD5(String s) char hexDigits = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f; try byte strTemp = s.getBytes(); MessageDigest mdTemp = MessageDigest.getInstance(MD5); mdTemp.update(strTemp); byte md = mdTemp.digest(); int j = md.length; char str = new char
12、j * 2; int k = 0; for (int i = 0; i 4 strk+ = hexDigitsbyte0 return new String(str); catch (Exception e) return null; 4. Common.java package com.util; /* * 常用方法类 */ import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public final class Common /字符串 HTML 转换 public stat
13、ic String turn(String str) while (str.indexOf(n) != -1) str = str.substring(0, str.indexOf(n) + + str.substring(str.indexOf(n) + 1); while (str.indexOf( ) != -1) str = str.substring(0, str.indexOf( ) + + str.substring(str.indexOf( ) + 1); return str; /返回日期 当前日期传入 0 一年之前传入-1 三年后传入 3 public static Str
14、ing getDate(int num) SimpleDateFormat df = new SimpleDateFormat(yyyy-MM-dd); Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, num); return df.format(c.getTime(); /数据库输出字符串的过滤 public static String SQLStr(String str) if(str != null if(str = null | str.equals() | str.equals( ) str = ; return s
15、tr; /过滤空格 public static String PageStr(String str) if(str=null | str.trim().equals() | str.equals( )|str.equals(null) str = ; str = str.trim(); return str; /格式化日期 public static String formatRZ(String oldDate) SimpleDateFormat df = new SimpleDateFormat(yyyy-MM-dd); Date Date = new Date(oldDate); retu
16、rn df.format(Date); /汉化 public static String toChineseAndTrim(String str) if(str=null) str=; try byte b=str.getBytes(iso-8859-1); str=new String(b); str=str.trim(); catch(Exception e) e.printStackTrace(); return str; 5. CheckCode.java package com.util; /* * 验证码生成类 */ import java.util.Random; public
17、class CheckCode public String getCheckCode() Random random = new Random(); String sRand=; for (int i=0;i4;i+) String rand=String.valueOf(random.nextInt(10); sRand+=rand; return sRand; 6. LoginServlet.java package com.action; /* * 前台会员登陆 退出 */ import java.io.IOException; import javax.servlet.ServletE
18、xception; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.bean.MemberBean; import com.util.Constant; import com.util.Filter; public class LoginServlet extends HttpSer
19、vlet /* * Constructor of the object. */ public LoginServlet() super(); /* * Destruction of the servlet. */ public void destroy() super.destroy(); / Just puts destroy string in log / Put your code here public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException
20、, IOException doPost(request,response); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(Constant.CONTENTTYPE); request.setCharacterEncoding(Constant.CHARACTERENCODING); HttpSession session = request.getSession(
21、); MemberBean memberBean = new MemberBean(); String method = Filter.escapeHTMLTags(request.getParameter(method).trim(); if(method.equals(HEADUSERLOGIN)/网页头部会员登陆 String reg_user = Filter.escapeHTMLTags(request.getParameter(reg_user).trim(); String reg_pwd = Filter.escapeHTMLTags(request.getParameter(
22、reg_pwd).trim(); String reg_type = Filter.escapeHTMLTags(request.getParameter(reg_type).trim(); String lastip = request.getRemoteAddr(); int flag = memberBean.memberLogin(reg_user, reg_pwd, reg_type); if(flag = Constant.SUCCESS)/登陆成功 String info = memberBean.getLastTimeIP(reg_user); int flag2 = memb
23、erBean.upmemberLogin(reg_user, lastip); if(flag2 = Constant.SUCCESS) session.setAttribute(member, reg_user); session.setAttribute(type, reg_type); session.setAttribute(info, info); request.getRequestDispatcher(login1.jsp).forward(request, response); else session.setAttribute(member, reg_user); sessi
24、on.setAttribute(type, reg_type); session.setAttribute(info, info); session.setAttribute(message, 登陆成功,登陆信息更新失败! ); request.getRequestDispatcher(login1.jsp).forward(request, response); else if(flag = Constant.NAME_ERROR)/用户名错误 request.setAttribute(reg_user, reg_user); request.setAttribute(message, 该用
25、户名不存在或者已被管理员冻结! ); request.getRequestDispatcher(login1.jsp).forward(request, response); else if(flag = Constant.PASSWORD_ERROR)/密码错误 request.setAttribute(reg_user, reg_user); request.setAttribute(message, 密码错误!); request.getRequestDispatcher(login1.jsp).forward(request, response); else if(method.equ
26、als(PAGEUSERLOGIN)/登陆页面会员登陆 String username = Filter.escapeHTMLTags(request.getParameter(username).trim(); String password = Filter.escapeHTMLTags(request.getParameter(password).trim(); String reg_type = Filter.escapeHTMLTags(request.getParameter(reg_type).trim(); String lastip = request.getRemoteAd
27、dr(); int flag = memberBean.memberLogin(username, password, reg_type); if(flag = Constant.SUCCESS)/登陆成功 String info = memberBean.getLastTimeIP(username); int flag2 = memberBean.upmemberLogin(username, lastip); if(flag2 = Constant.SUCCESS) session.setAttribute(member, username); session.setAttribute(
28、type, reg_type); session.setAttribute(info, info); request.getRequestDispatcher(member/index.jsp).forward(request, response); else session.setAttribute(member, username); session.setAttribute(type, reg_type); session.setAttribute(info, info); session.setAttribute(message, 登陆成功,登陆信息更新失败! ); request.g
29、etRequestDispatcher(member/index.jsp).forward(request, response); else if(flag = Constant.NAME_ERROR)/用户名错误 request.setAttribute(reg_user, username); request.setAttribute(message, 该用户名不存在!); request.getRequestDispatcher(login.jsp).forward(request, response); else if(flag = Constant.PASSWORD_ERROR)/密
30、码错误 request.setAttribute(reg_user, username); request.setAttribute(message, 密码错误!); request.getRequestDispatcher(login.jsp).forward(request, response); else if(method.equals(logout)/从 iframe 页面退出登陆 session.removeAttribute(member); session.removeAttribute(type); request.getRequestDispatcher(login1.js
31、p).forward(request, response); else if(method.equals(pagelogout)|method.equals(memberexit)/从网页 退出登陆 session.removeAttribute(member); session.removeAttribute(type); request.getRequestDispatcher(index.jsp).forward(request, response); else request.getRequestDispatcher(error.jsp).forward(request, respon
32、se); public void init() throws ServletException / Put your code here 7. TrainBean.java package com.bean; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import com.util.Constant; import com.util.DBO; public c
33、lass TrainBean private List list; private ResultSet rs; private String date=new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(Calendar.getInstance().getTime(); /车次 /增加 车次管理 times 例如: 沈阳-北京 2510 次 时间:xx 点 xx 分发车 xx 点 xx 分到达 软座 硬座 卧铺 票价 /增加 车次管理 train 例如:id, train, num,stime, etime,soft,softprice,hard,
34、 hardprice, sleep, sleepprice, addtime public int addTrain(String train,String num,String stime,String etime,String sitetype,String price) String sql = insert into train (train,num,stime,etime,sitetype,price,addtime) + values(+train+,+num+,+stime+,+etime+,+sitetype+,+price+,+dat e+); DBO dbo = new D
35、BO(); dbo.open(); try int i = dbo.executeUpdate(sql); if(i = 1) return Constant.SUCCESS; else return Constant.SYSTEM_ERROR; catch(Exception e) e.printStackTrace(); return Constant.SYSTEM_ERROR; finally dbo.close(); / 修改 public int upTrain(int id,String train,String num,String stime,String etime,Stri
36、ng sitetype,String price) String sql = update train set train=+train+,num=+num+,stime=+stime+,etime=+etime+,sitetype=+sitety pe+, + price=+price+ where id=+id+; DBO dbo = new DBO(); dbo.open(); try int i = dbo.executeUpdate(sql); if(i = 1) return Constant.SUCCESS; else return Constant.SYSTEM_ERROR;
37、catch(Exception e) e.printStackTrace(); return Constant.SYSTEM_ERROR; finally dbo.close(); / 删除 public int delTrain(int id) String sql = delete from train where id=+id+; DBO dbo = new DBO(); dbo.open(); try int i = dbo.executeUpdate(sql); if(i = 1) return Constant.SUCCESS; else return Constant.SYSTE
38、M_ERROR; catch(Exception e) e.printStackTrace(); return Constant.SYSTEM_ERROR; finally dbo.close(); / 所有 public List getAllTrain() String sql = select * from train order by id desc ; DBO dbo=new DBO(); list = new ArrayList(); dbo.open(); try rs = dbo.executeQuery(sql); while(rs.next() List list2=new
39、 ArrayList(); list2.add(rs.getString(1); list2.add(rs.getString(2); list2.add(rs.getString(3); list2.add(rs.getString(4); list2.add(rs.getString(5); list2.add(rs.getString(6); list2.add(rs.getString(7);list2.add(rs.getString(8); list.add(list2); return list; catch(Exception e) e.printStackTrace(); r
40、eturn null; finally dbo.close(); public List getTrain(String train) String sql = select * from train where train like %+train+% order by id desc ; DBO dbo=new DBO(); list = new ArrayList(); dbo.open(); try rs = dbo.executeQuery(sql); while(rs.next() List list2=new ArrayList(); list2.add(rs.getString
41、(1); list2.add(rs.getString(2); list2.add(rs.getString(3); list2.add(rs.getString(4); list2.add(rs.getString(5); list2.add(rs.getString(6); list2.add(rs.getString(7); list2.add(rs.getString(8); list.add(list2); return list; catch(Exception e) e.printStackTrace(); return null; finally dbo.close(); pu
42、blic List getOneTrain(String id) String sql = select * from train where id=+id+ ; DBO dbo=new DBO(); list = new ArrayList(); dbo.open(); try rs = dbo.executeQuery(sql); while(rs.next() list.add(rs.getString(1); list.add(rs.getString(2); list.add(rs.getString(3); list.add(rs.getString(4); list.add(rs
43、.getString(5); list.add(rs.getString(6); list.add(rs.getString(7); list.add(rs.getString(8); return list; catch(Exception e) e.printStackTrace(); return null; finally dbo.close(); /订票管理 /增加 车次管理 times 例如: 沈阳-北京 2510 次 时间:xx 点 xx 分发车 xx 点 xx 分到达 软座 硬座 卧铺 票价 flag:是送票 pay:付款方式 /增加 车次管理 train 例如: id,tra
44、in,num,stime,etime,soft,softprice,hard,hardprice,sleep,sleepprice,addtime public int addPrep(String train,String num,String stime,String etime,String sitetype,String price,int numb,String pay,String member) String sql = insert into prep (train,num,stime,etime,sitetype,price,numb,pay,member,addtime)
45、+ values(+train+,+num+,+stime+,+etime+,+sitetype+,+price+,+nu mb+,+pay+,+member+,+date+); DBO dbo = new DBO(); dbo.open(); try int i = dbo.executeUpdate(sql); if(i = 1) return Constant.SUCCESS; else return Constant.SYSTEM_ERROR; catch(Exception e) e.printStackTrace(); return Constant.SYSTEM_ERROR; f
46、inally dbo.close(); / 修改 public int upPrep(int id,int numb,String pay) String sql = update prep set numb=+numb+,pay=+pay+ where id=+id+; DBO dbo = new DBO(); dbo.open(); try int i = dbo.executeUpdate(sql); if(i = 1) return Constant.SUCCESS; else return Constant.SYSTEM_ERROR; catch(Exception e) e.pri
47、ntStackTrace(); return Constant.SYSTEM_ERROR; finally dbo.close(); /送票 public int sendPrep(int id) String sql =update prep set flag=已送票; DBO dbo = new DBO(); dbo.open(); try int i = dbo.executeUpdate(sql); if(i = 1) return Constant.SUCCESS; else return Constant.SYSTEM_ERROR; catch(Exception e) e.pri
48、ntStackTrace(); return Constant.SYSTEM_ERROR; finally dbo.close(); / 删除 public int delPrep(int id) String sql = delete from prep where id=+id+; DBO dbo = new DBO(); dbo.open(); try int i = dbo.executeUpdate(sql); if(i = 1) return Constant.SUCCESS; else return Constant.SYSTEM_ERROR; catch(Exception e
49、) e.printStackTrace(); return Constant.SYSTEM_ERROR; finally dbo.close(); / 所有订票信息 public List getAllPrep() String sql = select * from prep order by id desc ; DBO dbo=new DBO(); list = new ArrayList(); dbo.open(); try rs = dbo.executeQuery(sql); while(rs.next() List list2=new ArrayList(); list2.add(
50、rs.getString(1); list2.add(rs.getString(2); list2.add(rs.getString(3); list2.add(rs.getString(4); list2.add(rs.getString(5); list2.add(rs.getString(6); list2.add(rs.getString(7); list2.add(rs.getString(8); list2.add(rs.getString(9); list2.add(rs.getString(10); list2.add(rs.getString(11); list.add(li
51、st2); return list; catch(Exception e) e.printStackTrace(); return null; finally dbo.close(); public List getOnePrep(String id) String sql = select * from prep where id=+id+ ; DBO dbo=new DBO(); list = new ArrayList(); dbo.open(); try rs = dbo.executeQuery(sql); while(rs.next() list.add(rs.getString(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 历年高考各省语文作文范文5篇
- 旅游度假区项目风险评估与对策
- 2025年上海奉贤区机关事业单位招聘考试笔试试题(含答案)
- 老年期课件教学课件
- 老年护理课件教学
- 老师课件导入方法
- 老师安全课堂课件
- 装修工程验收及保修个人服务合同
- 高端人才招聘标准劳动合同范本
- 车辆指标租赁与城市交通拥堵治理协议
- 最新-伤口愈合新进展和美容缝合课件
- 营销学相关理论-4P、4C、6P、整合营销
- 2022-2023年(备考资料)副主任医师(副高)-肾内科学(副高)历年真题精选一含答案试卷4
- 半导体设备零部件公司质量检验
- 零信任网络安全理念的重塑
- 黑布林The Clever Woman 聪明的妇人公开课课件
- 酒店客房部绩效考核管理制度
- 勇者斗恶龙怪兽篇joker2专家版中文配合表(附图)
- 房屋建筑构造(地基与基础)课件
- 西药房工作管理制度
- 《高分子取向结构》PPT课件.ppt
评论
0/150
提交评论