如何在servlet中显示mysql_select结果

时间:2015-03-27 09:04:49

标签: java mysql servlets

我尝试过一些东西,但是我无法获得输出,只是为了显示输出屏幕需要花费太多时间.... 例如:在我的情况下,输出应该包含一个表,其输出是根据我的程序生成的,但是它只需要花费太多时间来执行操作,之后只显示表头....

我用谷歌搜索了这个,但我无法找到关于此的合适想法...

感谢一百万可以提供帮助的人......

这是我的代码:

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
ResultSet result;
String dat,d,s,i,r;
int numcols,index,ans = 0;
ResultSetMetaData resultMd;
String rs = null;
d= request.getParameter("name");
s= request.getParameter("gen");
i= request.getParameter("indi");
r= request.getParameter("rea");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
if(d!=null && i==null && r==null)
    ans=1;
else if(d!=null && i==null && r!=null)
    ans=2;
else if(d!=null && i!=null && r!=null)
    ans=3;
else if(d==null && i!=null && r!=null)
    ans=4;
else if(d==null && i==null && r!=null)
    ans=5;
else if(d!=null && i!=null && r==null)
    ans=6;
else if(d==null && i!=null && r==null)
    ans=7;

switch(ans)
{
case 1:
    rs="SELECT d.gndr_cod,d.age,d.wt,d.reporter_country,dr.drug_name,i.indi_pt,o.outc_cod,r.pt from demo d,drug dr,indi i,outc o,reac r where dr.drug_name='"+d+"' and d.gndr_cod='"+s+"' limit 25";
    break;
case 2:
    rs="SELECT d.gndr_cod,d.age,d.wt,d.reporter_country,dr.drug_name,i.indi_pt,o.outc_cod,r.pt from demo d,drug dr,indi i,outc o,reac r where dr.drug_name='"+d+"' and r.pt='"+r+"' and d.gndr_cod='"+s+"' limit 25";
    break;
case 3:
    rs="SELECT d.gndr_cod,d.age,d.wt,d.reporter_country,dr.drug_name,i.indi_pt,o.outc_cod,r.pt from demo d,drug dr,indi i,outc o,reac r where dr.drug_name='"+d+"' and r.pt='"+r+"' and i.indi_pt='"+i+"' and d.gndr_cod='"+s+"' limit 25";
    break;
case 4:
    rs="SELECT d.gndr_cod,d.age,d.wt,d.reporter_country,dr.drug_name,i.indi_pt,o.outc_cod,r.pt from demo d,drug dr,indi i,outc o,reac r where r.pt='"+r+"' and i.indi_pt='"+i+"' and d.gndr_cod='"+s+"' limit 25";
    break;
case 5:
    rs="SELECT d.gndr_cod,d.age,d.wt,d.reporter_country,dr.drug_name,i.indi_pt,o.outc_cod,r.pt from demo d,drug dr,indi i,outc o,reac r where r.pt='"+r+"' and d.gndr_cod='"+s+"' limit 25";
    break;
case 6:
    rs="SELECT d.gndr_cod,d.age,d.wt,d.reporter_country,dr.drug_name,i.indi_pt,o.outc_cod,r.pt from demo d,drug dr,indi i,outc o,reac r where dr.drug_name='"+d+"' and i.indi_pt='"+i+"' and d.gndr_cod='"+s+"' limit 25";
    break;
case 7:
    rs="SELECT d.gndr_cod,d.age,d.wt,d.reporter_country,dr.drug_name,i.indi_pt,o.outc_cod,r.pt from demo d,drug dr,indi i,outc o,reac r where i.indi_pt='"+i+"' and d.gndr_cod='"+s+"' limit 25";
    break;
}
out.print("<html>");
out.print("<head><title>Results</title></head>");
out.print("<body>");
try
{
    result=mystmt.executeQuery(rs);
    resultMd=result.getMetaData();
    numcols=resultMd.getColumnCount();
    out.print("<table border='1' width='100%'>");
    out.print("<tr><th>Drug Name</th><th>Sex</th><th>Age</th><th>Weight</th><th>Country</th>");
    out.print("<th>Indication</th><th>Reaction</th><th>Outcome</th>");
    out.print("</tr>");
    while(result.next())
    {
        out.print("<tr>");
        for(index=0; index<=numcols;index++)
        {
            dat=result.getString(index);
            out.print("<td>" +dat+"</td>");
        }
        out.print("</tr>");
    }
    out.print("</table>");
}
catch(Exception e)
{
    e.printStackTrace();
}
out.print("</body></html>");}

1 个答案:

答案 0 :(得分:0)

所有的拳头,测试您的查询,如果有任何性能问题,请尝试解决它。可能存在大量数据以及无法处理运行时的数据。请参阅RuntimeExceptions或Errors的一些日志。

如果您的SQL查询运行速度很快并且返回的行数不多,那么请编辑您的问题并尽可能简化。谢谢。 ;)

编辑:

尽量不要连接SQL查询,而是使用绑定,因为如果执行全新的查询,数据库就无法使用统计信息,并且可能会遇到SQLInjection问题。

EDIT2:

请关闭finally块中的资源,如果有,请增加连接线程限制。只尝试一个SQL,试着让它找到问题的根源。