代码
1、recharge.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,
minimum-scale=1.0, maximum-scale=2.0; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="RechargeServlet" method="post">
<table border="26" align="center">
<tr>
<td align="right"> 串号/手机号:</td>
<td> <input type="text" name="serialNum" size="25"></td>
</tr>
<tr>
<td align="right" ize="25"> 兑换码: </td>
<td><input type="text" name="rndcode"></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="充值">
</td>
</tr>
</table>
</from>
</body>
</html>
2、RechargeServlet.java
package servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.Code;
import bean.Device;
import dao.CodeInfoDao;
import dao.DeviceInfoDao;
/**
* Servlet implementation class RechargeServlet
*/
@WebServlet("/RechargeServlet")
public class RechargeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RechargeServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String serialNum = request.getParameter("serialNum");
String rndCode = request.getParameter("rndcode");
String message;
boolean ret = false;
boolean ret2 = false;
if (serialNum == null || rndCode == null || "none".equals(serialNum)) {
return;
}
DeviceInfoDao deviceInfo = new DeviceInfoDao();
CodeInfoDao codeInfo = new CodeInfoDao();
String regex = "^[0-9]+$";
Pattern pattern = Pattern.compile(regex);
Matcher match = pattern.matcher(serialNum);
List<Device> devices = null;
List<Code> codes = null;
if (match.matches()) {
devices = deviceInfo.findDeviceByPhoneNumber(serialNum);
} else {
devices = deviceInfo.findDeviceBySerialNum(serialNum);
}
if (devices.isEmpty()) {
response.sendRedirect(request.getContextPath() + "/result.jsp?message=fail");
return;
} else {
codes = codeInfo.findCodeByRndCode(rndCode);
if (codes.isEmpty()) {
response.sendRedirect(request.getContextPath() + "/result.jsp?message=fail");
return;
} else {
Date now = new Date();
java.sql.Date time = devices.get(0).getTime();
long bigDate = now.getTime() >= time.getTime() ? now.getTime() : time.getTime();
//long realDate = bigDate + codes.get(0).getPrice() * 30 * 24 * 60 *60 * 1000;
//java.sql.Date newDate = new java.sql.Date(realDate);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(bigDate);
c.add(Calendar.MONTH,codes.get(0).getPrice());
ret = deviceInfo.updateTimeBySerialNum(new java.sql.Date(c.getTime().getTime()), devices.get(0).getSerialNum());
ret2 = codeInfo.updateUsedByCode(true, codes.get(0).getCode());
if (ret && ret2) {
message = "success";
} else {
message = "fail";
}
System.out.println("now:" + now + " time:" + time + " newDate:" + c.getTime());
response.sendRedirect(request.getContextPath() + "/result.jsp?message=" + message);
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3、Code.java
package bean;
import java.sql.Date;
public class Code {
private Integer id;
private String serialNum;
private String code;
private Integer price;
private boolean used;
private Date time;
public Code() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSerialNum() {
return this.serialNum;
}
public void setSerialNum(String serialNum) {
this.serialNum = serialNum;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public Integer getPrice() {
return this.price;
}
public void setPrice(Integer price) {
this.price = price;
}
public boolean getUsed() {
return this.used;
}
public void setUsed(boolean used) {
this.used = used;
}
public Date getTime() {
return this.time;
}
public void setTime(Date time) {
this.time = time;
}
}
4、
drop table rndcode;
create table rndcode
(
id int auto_increment primary key,
serialnum varchar(30),
code varchar(30),
price int,
used boolean,
time DATE
);
重新执行ImportDB
5、DeviceInfoDao.java添加:
public List<Device> findDeviceByPhoneNumber(String phoneNumber) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs=null;
List<Device> deviceList= new ArrayList<Device>();
try {
conn = DBUtil.getConnection();
String sql = "select * from device where phonenumber=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, phoneNumber);
rs=pstmt.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String uid = rs.getString("serialnum");
int price = rs.getInt("price");
Date time = rs.getDate("time");
String phoneNum = rs.getString("phonenumber");
Device device = new Device();
device.setId(id);
device.setSerialNum(uid);
device.setPrice(price);
device.setTime(time);
device.setPhoneNum(phoneNum);
deviceList.add(device);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.release(conn, pstmt, rs);
}
return deviceList;
}
6、CodeInfoDao.java
package dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import bean.Code;
import util.DBUtil;
public class CodeInfoDao {
public List<Code> findCodeByRndCode(String tmpCode) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs=null;
List<Code> codeList= new ArrayList<Code>();
try {
conn = DBUtil.getConnection();
String sql = "select * from rndcode where code=? and used=0";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, tmpCode);
rs=pstmt.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String serialNum = rs.getString("serialnum");
String rndCode = rs.getString("code");
int price = rs.getInt("price");
boolean used = rs.getBoolean("used");
Date time = rs.getDate("time");
Code code = new Code();
code.setId(id);
code.setSerialNum(serialNum);
code.setCode(rndCode);
code.setPrice(price);
code.setUsed(used);
code.setTime(time);
codeList.add(code);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.release(conn, pstmt, rs);
}
return codeList;
}
public List<Code> findOneCodeByPrice(int tmpPrice) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs=null;
List<Code> codeList= new ArrayList<Code>();
try {
conn = DBUtil.getConnection();
String sql = "select * from rndcode where price=? and used=0 limit 1";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, tmpPrice);
rs=pstmt.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String serialNum = rs.getString("serialnum");
String rndCode = rs.getString("code");
int price = rs.getInt("price");
boolean used = rs.getBoolean("used");
Date time = rs.getDate("time");
Code code = new Code();
code.setId(id);
code.setSerialNum(serialNum);
code.setCode(rndCode);
code.setPrice(price);
code.setUsed(used);
code.setTime(time);
codeList.add(code);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.release(conn, pstmt, rs);
}
return codeList;
}
public boolean updateUsedByCode(boolean used, String code) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs=null;
boolean ret = false;
try {
conn = DBUtil.getConnection();
String sql = "update rndcode set used=? where code=?";
pstmt=conn.prepareStatement(sql);
pstmt.setBoolean(1, used);
pstmt.setString(2, code);
int row=pstmt.executeUpdate();
if (row == 1) {
System.out.println("update rnd used success!");
ret = true;
} else {
System.out.println("update rnd used fail!");
ret = false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.release(conn, pstmt, rs);
}
return ret;
}
}
7、GetRndCodeServlet
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.Code;
import dao.CodeInfoDao;
/**
* Servlet implementation class GetRndCodeServlet
*/
@WebServlet("/GetRndCodeServlet")
public class GetRndCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetRndCodeServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String month = request.getParameter("month");
CodeInfoDao codeInfo = new CodeInfoDao();
List<Code> codes = codeInfo.findOneCodeByPrice(Integer.parseInt(month));
if (!codes.isEmpty()) {
response.getWriter().append(codes.get(0).getCode());
} else {
response.getWriter().append("none");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
8、getTimeServlet要修改,数据库存的是设备到期时间,不需+30