Below is the code which shows the way to implement Hashtable in Java.
Also it shows the way to check the NULL values in Hashtable & exclude them while processing.
<%
String qry_Relations = "SELECT * FROM RELATIONS WHERE ID<> 0 ORDER BY relationname";
ResultSet rs_Relations = mdb.runQuery(qry_Relations);
Hashtable<String, String> ht = new Hashtable<String, String>();
while (rs_Relations.next()) {
ht.put(rs_Relations.getString("id"), rs_Relations.getString("relationship"));
}
String keyvalue = "";
String relationname = "";
String relationcode = "";
%>
<table>
<tr>
<td class="normal">
<select name="relation" id="relation" onChange="validate();" >
<option value="0">SELECT </option>
<%
//ITERATING THOUGH THE HASH TABLE
for (int j = 1; j <= 10; j++) {
//Checking for NULL values in hashtable. Get the values which are not NULL
if(ht.get(String.valueOf(j)) != null)
{
keyvalue = ht.get(String.valueOf(j)).toString();
if (keyvalue.equals(relationname)) {
%>
<option value="<%=keyvalue%>" selected><%=keyvalue%> </option>
<%
} else {
%>
<option value="<%=keyvalue%>" ><%=keyvalue%> </option>
<%
}
}//if
}
}
%>
</select>
</td>
</tr>
</table>
Also it shows the way to check the NULL values in Hashtable & exclude them while processing.
<%
String qry_Relations = "SELECT * FROM RELATIONS WHERE ID<> 0 ORDER BY relationname";
ResultSet rs_Relations = mdb.runQuery(qry_Relations);
Hashtable<String, String> ht = new Hashtable<String, String>();
while (rs_Relations.next()) {
ht.put(rs_Relations.getString("id"), rs_Relations.getString("relationship"));
}
String keyvalue = "";
String relationname = "";
String relationcode = "";
%>
<table>
<tr>
<td class="normal">
<select name="relation" id="relation" onChange="validate();" >
<option value="0">SELECT </option>
<%
//ITERATING THOUGH THE HASH TABLE
for (int j = 1; j <= 10; j++) {
//Checking for NULL values in hashtable. Get the values which are not NULL
if(ht.get(String.valueOf(j)) != null)
{
keyvalue = ht.get(String.valueOf(j)).toString();
if (keyvalue.equals(relationname)) {
%>
<option value="<%=keyvalue%>" selected><%=keyvalue%> </option>
<%
} else {
%>
<option value="<%=keyvalue%>" ><%=keyvalue%> </option>
<%
}
}//if
}
}
%>
</select>
</td>
</tr>
</table>
No comments:
Post a Comment