簡體   English   中英

如何從MySQL數據庫在jsp頁面中顯示月份名稱

[英]how to display month name in jsp page from mysql database

我想每月從數據庫明智地顯示值。 這是我的獲取代碼

<%
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>

但在這里,中間的一個月份丟失其余結果是前一個月下顯示,例如,如果5月丟失了,明年六月以后月結果下一個月來。 我想按月顯示明細,如果在每月可用的月明細之后缺少中間一個(或)兩個月的顯示,我想按月顯示。 給我建議解決這個問題

Java可以為您提供月份的名稱(給定日期)。 首先,從數據庫中選擇整個日期(不要只是從中提取部分),然后用Java處理它。

參見http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

一個非常小的基本示例:

<%@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>

帕特里克

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM