Hareesh
Hareesh

Reputation: 125

how to display month name in jsp page from mysql database

i want to display values monthly wise from database. here are my fetchning code

<%
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) ");
%> 

and i'm displaying in jsp page like

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

but here middle one month is missing remaining results is displaying under the previous month,for example if may month is missing, next June and after months results coming under April month. i want to display the details monthly wise, if middle one(or) two months are missing display after available month details monthly wise. give me suggestion to solve this

Upvotes: 0

Views: 1736

Answers (2)

Patrick
Patrick

Reputation: 4572

a very small and basic example:

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

Patrick

Upvotes: 0

NickJ
NickJ

Reputation: 9559

Java can give you the name of the month, given a Date. First, select the whole Date from the database (don't just extract parts from it) and process it in Java.

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

Upvotes: 1

Related Questions