Help with select code?

[nobr]I have a calendar program in jsp. The users can enter description for the dates. These dates are stored in the database. I need to provide colors to those dates,which have a value 'N' in the weekender field. Am able to write the query, but am not able to provide the color. how can i do it? please help..

<%@ page import="java.util.List"%>

<%@page import="java.sql.*, java.util.*, java.text.*" %>

<%@page import="java.sql.Connection, java.sql.DriverManager,java.sql.ResultSet,java.sql.SQLException" %>

<html>

<head>

<title>Print a month page.</title>

<meta name="version">

<script>

var lastId ="";

var curId ="";

function selectDate(f)

{

if(document.forms[0].lastDateId !=null)

{

lastId = document.forms[0].lastDateId.value;

}

if(lastId != document.forms[0].todayId.value)

{

if(document.getElementById(lastId).title =="")

{

document.getElementById(lastId).style.backgroundColor='white';

}

}

if(f.id != document.forms[0].todayId.value)

{

document.getElementById(f.id).style.backgroundColor='blue';

}

document.forms[0].lastDateId.value=f.id;

curId = f.id;

document.getElementById("descr").style.visibility='visible';

document.forms[0].descText.value = f.title;

}

function setTitle()

{

document.getElementById(curId).title=document.forms[0].descText.value;

if(document.forms[0].descText.value =="" && curId != document.forms[0].todayId.value)

{

document.getElementById(curId).style.backgroundColor='white';

}

else

{

if(document.forms[0].list.value =="" )

{

document.forms[0].list.value = curId+":"+document.getElementById(curId).title;

}

else

{

list = replaceList(document.forms[0].list.value,curId);

list = list+","+curId+":"+document.getElementById(curId).title;

document.forms[0].list.value = list;

}

}

document.getElementById("descr").style.visibility='hidden';

}

function replaceList(list,id)

{

arr = list.split(",");

newlist ="";

for(i in arr)

{

if(arr[i].match(id) ==null)

{

if(newlist =="")

{

newlist = arr[i];

}

else

{

newlist +=","+arr[i]+",";

}

}

}

return newlist

}

function showCalendar()

{

location.href="CalendarPage.jsp?year="+document.forms[0].year.value;

}

</script>

</head>

<body bgcolor="#c6d9e4">

<%

Connection con =null;

response.setContentType("text/html");

try

{

String driverName ="com.mysql.jdbc.Driver";

Class.forName(driverName);

String serverName ="192.168.10.5";

String mydatabase ="Trainees";

String url ="jdbc:mysql://" + serverName +"/" + mydatabase;

String username ="venkat";

String password ="venkat";

con = DriverManager.getConnection(url, username, password);

Statement stmt =null;

ResultSet rset =null;

PreparedStatement PREPstmt1;

stmt = con.createStatement();

String year = request.getParameter("year");

out.println("year value is : "+year);

String date="",descr="";

String list = request.getParameter("list");

if(list !=null)

{

String dateDescrList[] = list.split(",");

for(int i=0;i<dateDescrList.length;i++)

{

String listObj = dateDescrList[i];

if(!listObj.equals("") || !listObj.trim().equals(""))

{

date= year+"-"+listObj.substring(0,listObj.indexOf(":"));

descr = listObj.substring(listObj.indexOf(":")+1);

out.println("date is : "+date);

out.println("description is : "+descr);

}

String query ="insert into Holiday(HolidayDate, Description, Weekender) VALUES (?, ?, 'N')";

PREPstmt1=con.prepareStatement(query);

PREPstmt1.setString(1,date);

PREPstmt1.setString(2,descr);

PREPstmt1.executeUpdate();

}

}

}

catch(Exception e)

{

System.err.println("Exception: " + e.getMessage());

}

finally

{

try

{

if(con !=null)

con.close();

}

catch(SQLException e)

{

}

}

%>

<%

Calendar c = Calendar.getInstance( );

boolean yyok =false;

int yy = 0, mm = 0;

String yyString = String.valueOf(c.get(Calendar.YEAR));//setting calendar with current year

String STyear = request.getParameter("year");//to get selected year

if(STyear !=null)//If an year is selected, then set that year. Else Current Year

{

yyString=STyear;

}

if (yyString !=null && yyString.length() > 0)

{

try

{

yy = Integer.parseInt(yyString);

yyok =true;

}

catch (NumberFormatException e)

{

out.println("Year " + yyString +" invalid");

}

}

if (!yyok)yy = c.get(Calendar.YEAR);

mm = c.get(Calendar.MONTH);

String todayId ="";

%>

<form method=get action="CalendarPage.jsp">

Enter Year : <select name="year">

<%

for(int i=2000;i<=2015;i++)

{

if(i==Integer.parseInt(yyString))

{

%>

<OPTION SELECTED= <%=i%> > <%=i%> </option>

<%

}

else

{

%>

<OPTION VALUE= <%=i%> > <%=i%> </option>

%>

<%

}

}

%>

</select>

<input type="button" value="Display" onClick="showCalendar()">

<br><br>

<div id="descr" style="visibility:hidden">Enter Description : <input type="text" name="descText" value=""> </div>

<br>

<%

String driverName ="com.mysql.jdbc.Driver";

Class.forName(driverName);

String serverName ="192.168.10.5";

String mydatabase ="Trainees";

String url ="jdbc:mysql://" + serverName +"/" + mydatabase;

String username ="venkat";

String password ="venkat";

con = DriverManager.getConnection(url, username, password);

Statement stmt =null;

ResultSet rset =null;

PreparedStatement PREPstmt1;

stmt = con.createStatement();

rset=stmt.executeQuery("select count(*) from Holiday where YEAR(HolidayDate)='"+yyString+"'");

int cnt=0;

boolean ALflag=false;

while(rset.next())

{

cnt= rset.getInt(1);

out.println("count is : "+cnt);

if(cnt>1)

ALflag=true;

}

%>

<%

rset=stmt.executeQuery("select count(*) from Holiday where Weekender='N'");

int cnte=0;

boolean SELflag=false;

while(rset.next())

{

cnte = rset.getInt(1);

out.println("count is : "+cnte);

if(cnte>1)

SELflag=true;

}

%>

<%!

String[] months ={

"January","February","March",

"April","May","June",

"July","August","September",

"October","November","December"

};

int dom[] ={

31, 28, 31, 30,

31, 30, 31, 31,

30, 31, 30, 31

};

%>

<%

int leadGap = 0;

%>

<table border="0" width="100%" align="Left">

<%

GregorianCalendar calendar =null;

for(int j=0;j<12;j++)

{

calendar =new GregorianCalendar(yy, j, 1);

if(j==0 || j%3==0)

{

%>

<tr>

<%

}

%>

<td halign="top" >

<table border="1" width="33%" align="Left"><tr align="right">

<th colspan=7>

<%= months[j] %>

<%= yy %>

</th>

</tr>

<tr>

<td>Sun<td>Mon<td>Tue<td>Wed<td>Thu<td>Fri<td>Sat

</tr>

<%

leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;

int daysInMonth = dom[j];

if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && j == 1)

++daysInMonth;

out.println("<tr>");

out.println(" ");

for (int i = 0; i < leadGap; i++)

{

out.print("<td> </td>");

}

for (int iday = 1; iday <= daysInMonth; iday++)

{

out.println("<td>");

int dayOfWeek = (leadGap + iday) % 7;

GregorianCalendar today =new GregorianCalendar();

if(today.get(Calendar.DATE) == iday && today.get(Calendar.MONTH) == j)

{

todayId = iday+"-"+(j+1);

%>

<div id="<%=((j+1)+"-"+iday)%>" onClick="selectDate(this)" style="background-color:#cccc00" title="" ><a href="#" style="text-decoration:none;color:808080"><%=iday %> </a></div>

<%

}

else

{

if(dayOfWeek <= 1)

{

String str = yyString+"-"+(j+1)+"-"+ iday;

if((dayOfWeek == 0) && (ALflag==false))

{

String query ="insert into Holiday VALUES ('" + str +"','Saturday','Y')";

stmt=con.createStatement();

stmt.executeUpdate(query);

}

elseif ((dayOfWeek == 1) && (ALflag==false))

{

String query ="insert into Holiday VALUES ('" + str +"','Sunday','Y')";

stmt=con.createStatement();

stmt.executeUpdate(query);

}

%>

<div id="<%=((j+1)+"-"+iday)%>" onClick="selectDate(this)" style="background-color:#cccccc" title=""> <a href="#" style="text-decoration:none;color:808080"><%=iday %> </a></div>

<%

}

else

{

%>

<div id="<%=((j+1)+"-"+iday)%>" onClick="selectDate(this)" style="background-color:white" title="" ><a href="#" style="text-decoration:none;color:808080"><%=iday %> </a></div>

<%

}

}

out.println("</td>");

if(calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == iday)

{

out.println("</td></tr></table>");

}

elseif ((leadGap + iday) % 7 == 0)

{

out.println("</tr>");

out.println("<tr>");

}

}

%>

</td>

<%

if((j+1)%3==0)

{

%>

</tr>

<%

}

}

%>

<input type="hidden" name="lastDateId" value="<%=todayId%>">

<input type="hidden" name="todayId" value="<%=todayId%>">

<input type="hidden" name="list" value="">

<input type="submit" value="Submit" onClick="setTitle()">

</form>

</body>

</table>

</html>

This is the extract of the above code. This is how i have provided the query. In which part of my above code should i provide the coloring aspect. If the (SELflag==false), then i want to provide the color. where should i give? please help

<%

rset=stmt.executeQuery("select count(*) from Holiday where Weekender='N'");

int cnte=0;

boolean SELflag=false;

while(rset.next())

{

cnte = rset.getInt(1);

out.println("count is : "+cnte);

if(cnte>1)

SELflag=true;

}

%>

[/nobr]

[20953 byte] By [venkateshpa] at [2007-10-2 16:34:41]
# 1

This is absolutely the wrong way to do it. I wouldn't have all that scriptlet code in the page.

Write an object that is loaded with the data from the database and put into page scope. Have that method carry the logic for colors and weekends and whatnot. Then use JSTL to write a clean JSP that simply iterates through that object and uses its methods to set colors.

The page you've written is hard to read, has too much logic in it, is hard to test, and will be a maintainence nightmare for years to come, IMO. Better to break it up a bit.

%

duffymoa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
ok.. But once i finish up the whole code, i can then easily break this code and put in the way you have mentioned. But for the time being, i really need to complete it. can u help me with the problem i have earlier mentioned. please help
venkateshpa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
I'd still advise that you try to do the right thing now. You won't do it later.Maybe a custom tag library will help.%
duffymoa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Looked at your code again - it's worse than I thought. You don't take advantage of connection pools, you've got all that JDBC code right in the page, you don't use JSTL. I wouldn't go forward with that.%
duffymoa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Ok friend, i will surely do. But please help me now.
venkateshpa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

I think you have an idea how, and anything you can write in Java you seem willing to put inside your page.

You have to determine what constitutes a weekend day (Saturday or Sunday). You'll have to check each Date, see if it's a Saturday or Sunday, and add an "N" to its table row and set the color to what you wish. If it's not a weekend Date, leave that entry blank.

If this is accurate, what's the problem? It'll be a nightmarish mess in scriptlet code, but that doesn't seem to concern you.

%

duffymoa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
i only want the dates which have 'N' in the weekender field to have a seperate color. I am able to get the count of those dates, but where should i give the coloring part in my code?please help
venkateshpa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

hi,

even i suggest what duffymo had said its too difficult to understand ur code.

anyway this how i do if i get this sutuation

take a class and define a propert y called description

class some{

String description;

public logic (){

/* write ur method ( logic) to fill this description, like 'y' or 'n' according the data in ur database, as ur wrote the code in the jsp */

}

}

now in ur jsp page use jstl tags

like,

first get the value for description

<c:set var="desc" value="${description}">

<c:choose>

<c:when test="${desc eq 'y'}">

apply the color

</c:when>

<c:otherwiser>

apply other color

</c:otherwiser>

</c:choose>

so, now the only thing u need to do is, just keep the value for description in request object

like in ur servlet class

{

some s=new some();

s. logic();

request.put("description",s.discription);

}

so the value of description is in request scope so u get that value in ur jsp with <c:set>

apply color when it is 'y'

nookersatyaa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
please help?where should i put the color code?
venkateshpa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

u maybe using some table tag to display ur data,

<c:set var="desc" value="${description}">

<table>

<tr>

<c:choose>

<c:when test="${desc eq 'y'}">

<td bgcolor="#ur color">

</td>

</c:when>

<c:otherwiser>

<td bgcolor="#ur other color">

</td>

</c:otherwiser>

</c:choose>

<tr>

<tabe>

maybe like this change according to ur needs

nookersatyaa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

Here is the calendar code, which i have used. Where should i use the code to check . i am able to get the count of user inputted days. i want to use the code,

if(SELflag==false)

{

give color.

}

where can i give this if code, to get the color?

<%

rset=stmt.executeQuery("select count(*) from Holiday where Weekender='N'");

int cnte=0;

boolean SELflag=false;

while(rset.next())

{

cnte = rset.getInt(1);

out.println("count is : "+cnte);

if(cnte>1)

SELflag=true;

}

%>

<%!

String[] months = {

"January", "February", "March",

"April","May", "June",

"July", "August","September",

"October", "November", "December"

};

int dom[] = {

31, 28, 31, 30,

31, 30, 31, 31,

30, 31, 30, 31

};

%>

<%

int leadGap = 0;

%>

<table border="0" width="100%" align="Left">

<%

GregorianCalendar calendar =null;

for(int j=0;j<12;j++)

{

calendar = new GregorianCalendar(yy, j, 1);

if(j==0 || j%3==0)

{

%>

<tr>

<%

}

%>

<td halign="top" >

<table border="1" width="33%" align="Left"><tr align="right">

<th colspan=7>

<%= months[j] %>

<%= yy %>

</th>

</tr>

<tr>

<td>Sun<td>Mon<td>Tue<td>Wed<td>Thu<td>Fri<td>Sat

</tr>

<%

leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;

int daysInMonth = dom[j];

if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && j == 1)

++daysInMonth;

out.println("<tr>");

out.println(" ");

for (int i = 0; i < leadGap; i++)

{

out.print("<td> </td>");

}

for (int iday = 1; iday <= daysInMonth; iday++)

{

out.println("<td>");

int dayOfWeek = (leadGap + iday) % 7;

GregorianCalendar today = new GregorianCalendar();

if(today.get(Calendar.DATE) == iday && today.get(Calendar.MONTH) == j)

{

todayId = iday+"-"+(j+1);

%>

<div id="<%=((j+1)+"-"+iday)%>" onClick="selectDate(this)" style="background-color:#cccc00" title="" ><a href="#" style="text-decoration:none;color:808080"><%=iday %> </a></div>

<%

}

else

{

if(dayOfWeek <= 1)

{

String str = yyString+"-"+(j+1)+ "-"+ iday;

if((dayOfWeek == 0) && (ALflag==false))

{

String query = "insert into Holiday VALUES ('" + str + "','Saturday','Y')";

stmt=con.createStatement();

stmt.executeUpdate(query);

}

else if ((dayOfWeek == 1) && (ALflag==false))

{

String query = "insert into Holiday VALUES ('" + str + "','Sunday','Y')";

stmt=con.createStatement();

stmt.executeUpdate(query);

}

%>

<div id="<%=((j+1)+"-"+iday)%>" onClick="selectDate(this)" style="background-color:#cccccc" title=""> <a href="#" style="text-decoration:none;color:808080"><%=iday %> </a></div>

<%

}

else

{

%>

<div id="<%=((j+1)+"-"+iday)%>" onClick="selectDate(this)" style="background-color:white" title="" ><a href="#" style="text-decoration:none;color:808080"><%=iday %> </a></div>

<%

}

}

out.println("</td>");

if(calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == iday)

{

out.println("</td></tr></table>");

}

else if ((leadGap + iday) % 7 == 0)

{

out.println("</tr>");

out.println("<tr>");

}

}

%>

</td>

<%

if((j+1)%3==0)

{

%>

</tr>

<%

}

}

%>

<input type="hidden" name="lastDateId" value="<%=todayId%>">

<input type="hidden" name="todayId" value="<%=todayId%>">

<input type="hidden" name="list" value="">

<input type="submit" value="Submit" onClick="setTitle()">

</form>

</body>

</table>

</html>

venkateshpa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

> Here is the calendar code, which i have used. Where should i use the code to > check . i am able to get the count of user inputted days. i want to use the code,

> if(SELflag==false)

> {

> give color.

> }

> where can i give this if code, to get the color?

You've allready been told multiple times that your code is unmaintainable, unreadable and buggy (you don't close result sets and so on).

I would not go on with this code. Split it up into a part that fills your model (controller, usually a Servlet) and concentrate wihtin the JSP on representing the model instead of mixing up every thing possible.

Anyway. You've three positions in your code where you set the background color. I think these are the places where you have to set the color too.

And please stop spamming this forum with your code (

http://forum.java.sun.com/thread.jspa?threadID=723788&messageID=4172847#4172847

http://forum.java.sun.com/thread.jspa?threadID=723204&messageID=4169746#4169746

http://forum.java.sun.com/thread.jspa?threadID=723171&messageID=4169651#4169651

http://forum.java.sun.com/thread.jspa?threadID=722825&messageID=4167889#4167889

...

)

andi

andiha at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
Friend, i am newbie in java and jsp. so mistakes do occur, when u do the coding. I was asking for help, if u can u help. once i get the program running, i can split the codes.
venkateshpa at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

> so mistakes do occur, when u do the coding.

Every one makes mistakes. That's not the problem.

> i am newbie in java and jsp

One more reason to learn it the "correct" way.

> I was asking for help, if u can u help.

Different people allready told you what to do - Rewrite the page so that it is understandable and maintainable. As a first step think about what's the element this page represents (the model of the page). Then move all the Database realated stuff into a Controller (Servlet). Think about using JSTL tags instead of using JSP Scriplets. Having done so you will see that the page is much easier to read and that it's nearby automatically clear where is the right place to set the color.

btw. I've also told you where I think that it is the right place to set the color.

> once i get the program running, i can split the codes.

Nobody believes that.

And please write out words like "you, thanks, ...".

andi

andiha at 2007-7-13 17:39:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...