不知所措的servlet返回问题

wudaoshengu 2008-07-10 04:19:06
我做的是一个链接到servlet上去,后代参数。由servlet返回一个excel文件。

但奇怪的是,从数据库中查询出的记录没有第一条(从那个输出可以看到,是从第一条开始的)但在 mysqlaboutfunc 中打印出的查询语句在mysql中执行是没有问题的。

这个令我十分郁闷和头痛。为什么没有第一条????????????????????

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;

public class GenerateExcel extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String skipnum=request.getParameter("skipnum");
String starttime=request.getParameter("starttime");
String endtime=request.getParameter("endtime");
String cardtype=request.getParameter("cardtype");
String cardstatus=request.getParameter("cardstatus");
String cardbalance=request.getParameter("cardbalance");

String emptyserial="";
String emptyaccount="";

if (starttime==null || endtime==null ||skipnum==null ||cardtype==null ||
cardstatus==null || cardbalance==null || starttime.equals(endtime))
{
response.sendRedirect("kadeguanli-kadaochu.jsp");
return;
}

int skipnumber=Integer.parseInt(skipnum);
int cardtypeof=Integer.parseInt(cardtype);
int cardstatusof=Integer.parseInt(cardstatus);
int cardbalanceof=Integer.parseInt(cardbalance);

response.setContentType("application/vnd.ms-excel");

HSSFWorkbook wbook = new HSSFWorkbook();

HSSFSheet sheet = wbook.createSheet("new sheet");

HSSFRow row= sheet.createRow((short)0);
HSSFCell cell= row.createCell((short)0);
cell.setCellValue("卡的信息");
sheet.addMergedRegion(new Region(0,(short)0,1,(short)6));
HSSFCellStyle style = wbook.createCellStyle();
style.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell.setCellStyle(style);

row= sheet.createRow((short)2);
row.createCell((short)0).setCellValue("序 号");
row.createCell((short)1).setCellValue("序列号");
row.createCell((short)2).setCellValue("卡 号");
row.createCell((short)3).setCellValue("激活日期");
row.createCell((short)4).setCellValue("主持人密码");
row.createCell((short)5).setCellValue("会员密码");
row.createCell((short)6).setCellValue("制造时间");

MysqlForExcel myexcel=new MysqlForExcel();

int rett=myexcel.startsearchcardinfo(skipnumber,starttime,endtime,
emptyserial,emptyaccount,cardtypeof,cardstatusof,cardbalanceof);

if(rett!=0)
return;

if(myexcel.nextcardinfo()!=0){
response.sendRedirect("kadeguanli-kadaochu.jsp");
return;
}

int i=1;
while(myexcel.nextcardinfo()==0){
String selserial=myexcel.getcardinfostring("serial_no");
System.out.println(selserial); //此处输出就从第二天记录输出
String selaccount=myexcel.getcardinfostring("card_id");
String selactivedate=myexcel.getcardinfodate("active_date");
String selhostpasswd=myexcel.getcardinfostring("host_pwd");
String selattendpasswd=myexcel.getcardinfostring("attend_pwd");
String selmkdate=myexcel.getcardinfodate("mk_date");

row=sheet.createRow(i+2);
row.createCell((short)0).setCellValue(i);
row.createCell((short)1).setCellValue(selserial);
row.createCell((short)2).setCellValue(selaccount);
row.createCell((short)3).setCellValue(selactivedate);
row.createCell((short)4).setCellValue(selhostpasswd);
row.createCell((short)5).setCellValue(selattendpasswd);
row.createCell((short)6).setCellValue(selmkdate);
i++;
}

OutputStream out = response.getOutputStream();
wbook.write(out);
out.close();
}
}



class MysqlForExcel{
Connection mysqlconn;
Statement mysqlstmt;
ResultSet mysqlres;

private int domysqlconn(){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
mysqlconn=DriverManager.getConnection(uriparam1,"root","123456");
mysqlstmt=mysqlconn.createStatement();

}catch (Exception e){
System.out.println("Error:"+e.toString());
return -1;
}
return 0;
}

private int domysqlclose(){
try{
mysqlstmt.close();

mysqlconn.close();
}catch (Exception e){
System.out.println("Error:"+e.toString());

return -1;
}
return 0;
}


public int startsearchcardinfo(int skipnum,String starttime,String endtime, String serialno,
String account, int cardtype,int cardstatus,int cardbalance)
{
int ret=domysqlconn();

if (ret!=0)
{
return -100;
}

try{
String subquerysql="from view_cardinfo where mk_date>='"+starttime+"' and mk_date<adddate('"+endtime+"',1)";

if (!serialno.equals(""))
{
subquerysql+=" and serial_no='"+serialno+"'";
}

if (!account.equals(""))
{
subquerysql+=" and card_id='"+account+"'";
}

if (cardtype!=-1)
{
subquerysql+=" and card_type="+cardtype;
}

if (cardstatus!=-1)
{
subquerysql += " and card_state="+cardstatus;
}

if (cardbalance!=-1)
{
subquerysql += " and balance="+cardbalance*100;
}


int limitcount=skipnum+20;
querysql="select * "+subquerysql+" limit "+limitcount;
System.out.println(querysql+"\nKKKKKK\n");

mysqlres=mysqlstmt.executeQuery(querysql);

if (skipnum==0)
{
mysqlres.beforeFirst();
}
else
{
mysqlres.absolute(skipnum);
}

}catch (Exception e){
System.out.println("Error:"+e.toString());
ret=-10;
}
return 0;
}

public int nextcardinfo()
{
int ret=0;
try{
if (mysqlres.next())
{
ret=0;
}
else
{
ret=-1;
}
}catch (Exception e){
System.out.println("Error:"+e.toString());
ret=-100;
}
return ret;
}

public int getcardinfoint(String colname)
{
try{
return mysqlres.getInt(colname);

}catch (Exception e){
System.out.println("Error:"+e.toString());
}
return -1;
}

public String getcardinfodate(String colname)
{
try{
Date datevalue=mysqlres.getDate(colname);
return datevalue.toString();
}catch (Exception e){
System.out.println("Error:getcardinfodate "+colname+" "+e.toString());
}
return "0000-00-00";
}

public String getcardinfostring(String colname)
{
try{
return mysqlres.getString(colname);

}catch (Exception e){
System.out.println("Error:"+e.toString());
}
return null;
}

public int endsearchcardinfo()
{
try{
mysqlres.close();
}catch (Exception e){
System.out.println("Error:"+e.toString());
return -1;
}
domysqlclose();
return 0;
}
}
...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wudaoshengu 2008-07-12
  • 打赏
  • 举报
回复
while改成do while就好了
lzhw669 2008-07-10
  • 打赏
  • 举报
回复
if(myexcel.nextcardinfo()!=0){

while(myexcel.nextcardinfo()==0){
这两个地方执行完之后,是不是就把游标指到第二行了,不知是不是这个原因

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧