我正在尝试使用Struts2将数据从excel导入MySQL。在这里我导入了所有数据,但是我遇到了日期问题。这个excel有一个列作为月(2014年4月)。
我还在数据库中导入了Month列。但每当我使用Month字段检索时,它会显示2014年4月,2014年8月,2014年7月,2014年1月的数据。实际上,我的数据库总共(2014年4月至2015年4月)1年的数据。每个月有440条记录。
我创建了J.S.P页面,用于在数月之间检索数据。
MySQL数据库:
______________________________________________________________________
| ATM_Site_No | L H O | Revenue |Up_Time| Month |
|______________|______________|_____________|_______|________________|
|S1OZ000262004 | KERALA | 42,371 |96.85% | Apr-2014 |
|S1OZ000266006 | HYDERABAD | 28,572 |95.61% | Apr-2014 |
|S1OZ000327005 | BHOPAL | 43,812 |99.32% | Apr-2014 |
|______________|______________|_____________|_______|________________|
|S1OZ000262004 | KERALA | 41,659 |92.18% | May-2014 |
|S1OZ000266006 | HYDERABAD | 28,572 |95.61% | May-2014 |
|S1OZ000327005 | BHOPAL | 43,812 |99.32% | May-2014 |
|______________|______________|_____________|_______|________________|
|S1OZ000262004 | KERALA | 42,371 |96.85% | Jun-2014 |
|S1OZ000266006 | HYDERABAD | 41,659 |92.18% | Jun-2014 |
|S1OZ000327005 | BHOPAL | 43,812 |99.32% | Jun-2014 |
|______________|______________|_____________|_______|________________|
Retrieve.jsp:
<s:form action="uptimeview.action" method="post">
<s:select name="LHO" lable="LHO" list="{'KERALA','HYDERABAD', 'BHOPAL','ALL'}"/>
<sx:datetimepicker name="fromMonth" lable="From Month" displayFormate="dd-MMM-yyyy"/>
<sx:datetimepicker name="toMonth" lable="To Month" displayFormate="dd-MMM-yyyy"/>
<s:submit value="submit" align="center"></s:submit>
</s:form>
ViewUpTimeAction.java:
package com.cedge.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.cedge.bean.UpTimeBean;
import com.opensymphony.xwork2.ActionSupport;
public class ViewUpTimeAction extends ActionSupport{
private String LHO;
private Date fromMonth;
private Date toMonth;
public String getLHO() {
return LHO;
}
public void setLHO(String LHO) {
this.LHO = LHO;
}
public Date getFromMonth() {
return fromMonth;
}
public void setFromMonth(Date fromMonth) {
this.fromMonth = fromMonth;
}
public Date getToMonth() {
return toMonth;
}
public void setToMonth(Date toMonth) {
this.toMonth = toMonth;
}
private List<UpTimeBean> uptimeBeans=new ArrayList<UpTimeBean>();
public List<UpTimeBean> getUptimeBeans() {
return uptimeBeans;
}
public void setUptimeBeans(List<UpTimeBean> uptimeBeans) {
this.uptimeBeans = uptimeBeans;
}
public String execute() throws NullPointerException{
System.out.println(fromMonth);
System.out.println(toMonth);
System.out.println(LHO);
impleDateFormat simpleDateFormat=new SimpleDateFormat("dd-MMM-yyyy");
String fMonth=simpleDateFormat.format(fromMonth);
System.out.println(fMonth);
String tMonth=simpleDateFormat.format(toMonth);
System.out.println(tMonth);
String ret=ERROR;
try{
Context initCtx=new InitialContext();
Context envCtx=(Context) initCtx.lookup("java:comp/env");
DataSource ds=(DataSource) envCtx.lookup("jdbc/myTest");
Connection connection=ds.getConnection();
Statement statement=connection.createStatement();
ResultSet resultSet=null;
switch(LHO){
case "ALL": resultSet=statement.executeQuery
("SELECT * FROM revenue WHERE Month BETWEEN '"+fMonth+"'
AND '"+tMonth+"' ORDER BY Month");
break;
default: resultSet=statement.executeQuery(
"SELECT * FROM revenue WHERE LHO='"+LHO+"' and Month BETWEEN
'"+fMonth+"' AND '"+tMonth+"' ORDER BY Month");
}
while(resultSet.next()){
UpTimeBean fileBean=new UpTimeBean();
fileBean.setATM_Site_No(resultSet.getString(1));
fileBean.setATM_Location(resultSet.getString(2));
fileBean.setLHO(resultSet.getString(3));
fileBean.setCash(resultSet.getDouble(4));
fileBean.setNon_Cash(resultSet.getDouble(5));
fileBean.setRevenue(resultSet.getDouble(6));
fileBean.setUp_Time(resultSet.getString(7));
fileBean.setMonth(resultSet.getString(8));
System.out.println("fileBean"+fileBean);
uptimeBeans.add(fileBean);
System.out.println(uptimeBeans);
}
return SUCCESS;
}catch(Exception exception){
System.out.println(exception);
ret=ERROR;
}
return ret;
}
}
这里我试图在几个月之间检索数据。我试图找回
Apr-2014
和Jun-2014
之间的数据。但是,我得到了像Apr-2014
这样的数据
Aug-2014
,Jul-2014
和Jun-2014
。我需要数据
像这样Apr-2014
,May-2014
和Jun-2014
。
请帮助我在几个月之间从MySQL获取数据。