2014-04-03 43 views
0

我已經下面的jsp。stringbuilder和查詢發佈數據

<%@page import="java.util.Locale"%> 
<%@page import="java.text.SimpleDateFormat"%> 
<%@page import="java.sql.*"%> 
<% 
    Connection con = null; 
    Statement stmt = null; 
    try { 
     String a = request.getParameter("comments"); 
     String b = request.getParameter("updatedl"); 
     String c = request.getParameter("sid"); 
     String d = request.getParameter("desc"); 
     String e = request.getParameter("pubcode"); 
     String f = request.getParameter("Type"); 
     String g = request.getParameter("status"); 


     String h = null; 
     if (null != request.getParameter("DateRec")) { 
      h = request.getParameter("DateRec"); 
     } 
     String i = null; 
     if (null != request.getParameter("startD")) { 
      i = request.getParameter("startD"); 
     } 
     String j = null; 
     if (null != request.getParameter("AssignedD")) { 
      j = request.getParameter("AssignedD"); 
     } 
     String k = null; 
     if (null != request.getParameter("sentToAE")) { 
      k = request.getParameter("sentToAE"); 
     } 
     String l = null; 
     if (null != request.getParameter("RespFrmAE")) { 
      l = request.getParameter("RespFrmAE"); 
     } 
     String m = null; 
     if (null != request.getParameter("VWRCmp")) { 
      l = request.getParameter("VWRCmp"); 
     } 
     String n = request.getParameter("PS"); 
     String o = request.getParameter("TEst"); 
     String p = request.getParameter("Units"); 
     String q = null; 
     if (null != request.getParameter("VWR")) { 
      l = request.getParameter("VWR"); 
     } 
     String r = request.getParameter("IE"); 
     String s = null; 
     if (null != request.getParameter("RevDate")) { 
      s = request.getParameter("RevDate"); 
     } 
     String t = request.getParameter("ReviewS"); 

     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=C:/jus/Excel1.xls; ReadOnly=true;"); 

     StringBuilder sc=new StringBuilder(); 


     stmt = con.createStatement(); 
     String query = "insert into [Feb$] ([comments],[No of lines updated in PSIS],[Shipment ID]," 
       + "[SAP TXT Description],[Phoenix Pub Code], [Product Type], [Status of the job],[Date Request received]," 
       + "[Start Date], [Date assigned],[Date file sent to AE],[Date response from AE]," 
       + "[Date completed --VWR], [Assigned to PS], [Time Estimate],[Units], [Date file needs to be sent to Vendor (VWR)]," 
       + "[Internal Errors],[Review Date],[Review Sent (Yes/No)]) values ('" + a + "','" + b + "','" + c + "','" + d + "','" + e + "','" + f + "','" + g + "','" + h + "','" + i + "','" + j + "','" + k + "','" + l + "','" + m + "','" + n + "','" + o + "','" + p + "','" + q + "','" + r + "','" + s + "','" + t + "')"; 

     PreparedStatement ps = con.prepareStatement(query); 
     int new1 = ps.executeUpdate(); 

     String UR = "success.jsp"; 
     response.sendRedirect(UR); 

    } catch (Exception e) { 
     out.println(e); 
    } finally { 
     try { 
      stmt.close(); 
      con.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

%> 

在這裏,我想發佈數據到Excel表,我要讓使用StringBuilder的查詢,從而使空值(如上面我的程序初始化),是空的,這些應該在跳過查詢,如果他們有價值,那麼他們應該被考慮。請讓我知道我該如何做到這一點。

謝謝。

回答

0

使用字符串如下建設者:

StringBuilder sb=new StringBuilder(); 

String query = "insert into [Feb$] ([comments],[No of lines updated in PSIS],[Shipment ID]," 
       + "[SAP TXT Description],[Phoenix Pub Code], [Product Type], [Status of the job],[Date Request received]," 
       + "[Start Date], [Date assigned],[Date file sent to AE],[Date response from AE]," 
       + "[Date completed --VWR], [Assigned to PS], [Time Estimate],[Units], [Date file needs to be sent to Vendor (VWR)]," 
       + "[Internal Errors],[Review Date],[Review Sent (Yes/No)]) values ('" + a + "','" + b + "','" + c + "','" + d + "','" + e + "','" + f + "','" + g; 

sb.append(query); 
if(h==null && i==null &&b j==null && k==null) 
sb.append(+ "')"); 
if(h!=null) 
sb.append("','" + h); 
if(i!=null) 
sb.append("','" + i); 
if(j!=null) 
sb.append("','" + j); 
if(k!=null) 
sb.append("','" + k); 
. 
. 
. 

最後你的某人轉換成字符串爲:

String dynamicQuery = sb.toString();