2013-12-11 67 views
0

我想從數據庫中明智地顯示每月的值。這裏是我fetchning代碼如何在mysql數據庫的jsp頁面中顯示月份名稱

<% 
int i=1; 
String country=request.getParameter("country"); 
Connection con=Singleton.getMySqlConnection(); 
Statement st=con.createStatement(); 
ResultSet rs=st.executeQuery("select MONTHNAME(start_date),Extract(year from start_date),Extract(day from start_date),event_name,state,country from conf_events where country='"+country+"' and type='approved' order by Extract(month from start_date),Extract(day from start_date) "); 
%> 

,我很喜歡在顯示jsp頁面

<%while(rs.next()){%> 

     <div class="alertbox"> 
     <table border="0" cellPadding="0" cellspacing="0" width="100%" > 
    <tr valign="top" >  
    <th align="left"class="alertmonth" > 
    <%=rs.getString(1)%>-<%=rs.getString(2)%> 
    </th>   
    </tr> 



     <tr > 
     <table border="0" cellpadding="8" cellspacing="0" width="100%" style="margin-top:5px;" id="cnflist"> 
     <tr> 
     <td width="20%"><%=rs.getString(3)%>&nbsp;th</td> 
     <td width="60%"><%=rs.getString(4)%></td> 
     <td width="20%"><%=rs.getString(5)%>,&nbsp;&nbsp;<%=rs.getString(6)%></td>  
     </tr> 

但這裏中間的一個月丟失剩餘的結果,如果可能是前一個月下顯示,例如月失蹤,接下來6月份以及月後結果下4月月份。我希望每月明智地顯示詳細信息,如果中間一個(或)兩個月缺失,則按月明智地顯示可用月份詳情。 給我建議解決這個問題

+2

讓我跟你說實話:這是一個可怕的寫JSP! – aquaraga

+1

嘗試使用'jstl'和'el'.It會更好 – Deepak

+0

我見過的最美麗的一段代碼(諷刺) – Makky

回答

0

一個非常小的和基本的例子:

<%@page import="java.text.SimpleDateFormat"%> 
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
    <%@page import="java.util.Date" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Show Date</title> 
</head> 
<body> 
<% 
    Date date = new Date(); 
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); 
    String today = sdf.format(date); 
%> 

Today is <%=today %> 
</body> 
</html> 

帕特里克