2014-04-08 91 views
0

我有下面的JSP將數據插入到Excel中。單個語句拋出異常

<%-- 
    Document : GetRec 
    Created on : Apr 7, 2014, 7:44:25 PM 
    Author  : u0138039 
--%> 

<%@page import="java.sql.ResultSetMetaData"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@include file="DBCon.jsp" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <%   try { 

       String x = request.getParameter("sid"); 
       String query = "Select * from [Feb$] where [Shipment ID]=?"; 
       ps = con.prepareStatement(query); 
       ps.setString(1, x); 
       rs = ps.executeQuery(); 
       ResultSetMetaData meta = rs.getMetaData(); 
       final int columnCount = meta.getColumnCount(); 

       while (rs.next()) {%> 
     <table border="1px"> 
      <tr> 
       <% 
        for (int i = 1; i < 21; i++) {%> 
       <td> 
        <b> 
         <%=meta.getColumnName(i)%> 
        </b> 
       </td> 

       <% } 

       %> 
      </tr> 
      <tr> 

       <td> 
        <%=rs.getString(1)%> 
       </td> 
       <td> 
        <%=rs.getString(2)%> 
       </td> 
       <td> 
        <%=rs.getString(3)%> 
       </td> 
       <td> 
        <%=rs.getString(4)%> 
       </td> 
       <td> 
        <%=rs.getString(5)%> 
       </td> 

        <% 
         if (rs.getString(6) == null) { 
        %> 
        <td> <select name="Type" id="Type"> 
         <option value="" disabled selected>Select your option</option> 
         <option value="Looseleaf - Update">Looseleaf - Update</option> 
         <option value="Pamphlet">Pamphlet</option> 
        </select> 
        </td> 
        <% 
         } else { 
          %> 
           <td> 
        <%=rs.getString(6)%> 
       </td> 
        <% 
         } 
        %> 

       <% 
         } 
        } catch (Exception e) { 
         out.print(e); 
        }%> 
      </tr> 
     </table> </body> 
</html> 

我在這裏具有rs.getString(6)價值,如果我使用它,而無需使用if condition,價值是越來越打印,但是當我用if condition做到這一點,它是扔我下面的例外。

java.sql.SQLException: No data found 

請讓我知道如何解決這個問題。

謝謝

回答

1

您從結果集中多次檢索相同的數據。您需要檢索一次數據並將其分配給一個變量,然後多次使用該變量。

更新答案

<% 
    String str = rs.getString(6); 
    if (str == null) { 
%> 
       <td> <select name="Type" id="Type"> 
        <option value="" disabled selected>Select your option</option> 
        <option value="Looseleaf - Update">Looseleaf - Update</option> 
        <option value="Pamphlet">Pamphlet</option> 
       </select> 
       </td> 
<% 
    } else { 
%> 
       <td> 
<%=str%> 
       </td> 
<% 
    } 
%> 
+0

可以請你讓我知道該怎麼做呢。謝謝 – user2423959

+0

請檢查更新 –