Display data in tabular format in jsp - sql

I am trying the following jsp code.
switch(ch)
{
case 1 :
ResultSet rsid=stmt.executeQuery("select ID from BcTwo");
while(rsid.next())
{
%>
<tr><td>
<%out.println(rsid.getString(1)); %>
</td></tr>
<%
}
rsid.close();
break;
case 2 :
ResultSet rs=stmt.executeQuery("select SERIES from BcTwo");
while(rs.next())
{
%>
<tr><td>
<%out.println(rs.getString("SERIES")); %>
</td></tr>
<%
}
rs.close();
break;
}
Using this code I am able to print the data.But I want the data to be printed in following format :
ID Series
1 BE
2 EQ
3 BE
4 BE
5 EQ
6 EQ
And using the above code data is getting printed as :
ID
1
2
3
4
5
6
Series
BE
EQ
BE
BE
EQ
EQ
What changes I should make to get the required output?

I got the answer.
switch(ch)
{
case 1 :
String ID=(String)session.getAttribute("ID");
session.setAttribute("ID", ID);
if(null == session.getAttribute("ID"))
{
out.println(rsid.getString("ID"));
}
break;
case 2 :
String SERIES=(String)session.getAttribute("SERIES");
session.setAttribute("SERIES", SERIES);
if(null == session.getAttribute("SERIES"))
{
out.println(rsid.getString("SERIES"));
}
break;
}
I am not sure whether this is the best solution but it just worked for me.

switch(ch)
{
%>
<tr>
<%
case 1 :
ResultSet rsid=stmt.executeQuery("select ID from BcTwo");
while(rsid.next())
{
%>
<td>
<%out.print(rsid.getString(1)); %>
</td>
<%
}
rsid.close();
break;
case 2 :
ResultSet rs=stmt.executeQuery("select SERIES from BcTwo");
while(rs.next())
{
%>
<td>
<%out.print(rs.getString("SERIES")); %>
</td>
<%
}
rs.close();
break;
}
%>
</tr>

first of all ,having java code inside scriptlets is a bad practice. You should avoid it. DO the necessary java code in a servlet and use EL or JSTL to display the returned data in the JSP
as for your question, each result will have a TR where the ID and SERIES will be printed inside that TR in 2 TDs
<%
ResultSet rsid=stmt.executeQuery("select ID,SERIES from BcTwo");
while(rsid.next())
{
%>
<tr>
<td>
<%out.print(rsid.getString("ID")); %>
</td>
<td>
<%out.print(rsid.getString("SERIES")); %>
</td>
</tr>
<%
}
rsid.close();
%>
hope this helps..

Related

Half of Classic ASP page works great, 2nd half gives SQL connection error

Please help!
I am moving a Classic ASP & ASP.Net website from one server to another, and one of the reports that works fine on the old server is only partially working on the new server.
Can someone please tell me:
If there is anything that might be preventing a successful connection to SQL server.
If there is anything else involved in the this than:
Connection String
ASP Page
DLL
My server environment:
Windows Server 2008 R2
SQL EXPRESS 2008
IIS7
Connection Details:
The Classic ASP pages of this website connect using a SQL connection string located in the registry. The other Classic ASP pages on the website are working just fine....and the first half of this report is also working just fine.
I have tested the connection string individually, and it connects properly.
Report Structure Details:
The first half of the report (that is working correctly) loads data directly from a table in the database to display each item in a list format. It also uses the EMPLOYEES table to populate the employee name for each item in the list.
The second half of the report (that is NOT working) loads data from the same table as the first half. However, it groups them by manager (Manager Details are stored in the EMPLOYEES table).
So, both half of the reports are accessing the same tables for information....the only difference is how they are displayed.
Error Details
Microsoft OLE DB Provider for SQL Server->DLL.Shape_DB.ShapeCMdWithRS(SHAPE {select * from employees where username in (select mgr_username from employees where empID in (select empID from time_off_requests where mgr_code=0)) order by last_name, first_name} APPEND ([select time_off_requests_view.*, employees.mgr_username as username from time_off_requests_view inner join employees on employees.empID = time_off_requests_view.empID where mgr_code=0} relate username to username) as timeoff, ...)#WEBSITENAME[version 4.0.480] error '80004005'
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
Code Details
Here is the CLASSIC ASP code for the part of the website that DOES work:
set time_off_requests = DLL.TimeOffAdminPending()
%>
<CENTER>
<%
if time_off_requests.EOF then %>
<font class="headerNoBold">No Pending Time Off Requests<BR>
<% else %>
<font class="headerNoBold">Pending Time Off Requests</font>
<table cellspacing="0" cellpadding="3" width="500" align=center border=0 bordercolorlight=#000000 bordercolordark=#000000>
<TR bgcolor=#ffffcc>
<TD width=33% align=center><B>Date</B></TD>
<TD width=33% align=center><nobr><B>Requested By</B></nobr></TD>
<TD width=33% align=center><nobr><B>Type</B></nobr></TD>
</TR>
<%
dim theDate
do while not time_off_requests.EOF
if (time_off_requests.AbsolutePosition mod 2) = 0 then %>
<TR bgcolor=#ffffcc>
<% else %>
<TR bgcolor=#ffffff>
<% end if %>
<TD class="smallText" align=center>
<A HREF="time_off_request_details.asp?timeoffreq_id=<% = time_off_requests("timeoffreq_id") %>">
<% = time_off_requests("dt_created") %>
</A>
</TD>
<TD class="smallText" align=center><% = time_off_requests("last_name") %>, <% = time_off_requests("first_name") %></TD>
<TD class="smallText" align=center><% = time_off_requests("time_off_type") %></TD>
</TR>
<%
time_off_requests.MoveNext
loop %>
</TABLE>
<% end if %>
Details from the DLL
Function TimeOffAdminPending() As ADODB.Recordset
TimeOffAdminPending = RunCmdWithRS("select * from time_off_request_view where mgr_code =0 and empID in (select empID from employees where mgr_username = '" & username & "')")
End Function
Here is the CLASSIC ASP code for the part that DOES NOT work:
<% set managers = DLL.PendingTimeOffMgr()
if not managers.EOF then %>
<font class="headerNoBold">Pending Manager Time Off Approvals</font>
<table cellspacing="0" cellpadding="3" width="500" align=center border=0 bordercolorlight=#000000 bordercolordark=#000000>
<TR bgcolor=#ffffcc>
<TD width=33% align=center><B>Date</B></TD>
<TD width=33% align=center><nobr><B>Requested By</B></nobr></TD>
<TD width=33% align=center><nobr><B>Type</B></nobr></TD>
</TR>
<% do while not managers.EOF
set time_requests = managers.Fields("timeoff").Value %>
<TR><TD colspan=3>
<% = managers("last_name") %>, <% = managers("first_name") %>
</TD><TR>
<%
do while not timeoff_requests.EOF
if (timeoff_requests.AbsolutePosition mod 2) = 0 then %>
<TR bgcolor=#ffffcc>
<% else %>
<TR bgcolor=#ffffff>
<% end if %>
<TD class="smallText" align=center>
<% = timeoff_requests("dt_created") %>
</TD>
<TD class="smallText" align=center><% = timeoff_requests("last_name") %>, <% = timeoff_requests("first_name") %></TD>
<TD class="smallText" align=center><% = timeoff_requests("timeoff_type") %></TD>
</TR>
<%
timeoff_requests.MoveNext
loop
managers.MoveNext
loop %>
</TABLE>
<% end if %>
Details from the DLL
PendingTimeOffMgr() As ADODB.Recordset
PendingTimeOffMgr = ShapeCmdWithRS("SHAPE {select * from employees where username in (select mgr_username from employees where empID in (select empID from time_off_requests where mgr_code = 0)) order by last_name, first_name} APPEND ({select time_off_requests_view.*, employees.mgr_username as username from time_off_requests_view inner join employees on employees.empID=time_off_requests_view.empID where mgr_code = 0} relate username to username) as timeoff")
End function

How to get currently selected row from a resultset?

Please help me to get the currently selected row from this list. I tried to get it by sessions but it didnt work.
<% int icount=0;
while(rs.next())
{
String repairId = rs.getString("item.id");
String esn = rs.getString("esn");
String type = rs.getString("type");
session.setAttribute("repId", repairId);
session.setAttribute("repEsn", esn);
session.setAttribute("repType", type);
%>
<tr>
<td><div align="center"><%=++icount%></div></td>
<td><%=rs.getString("esn")%></td>
<td><%=rs.getString("type")%></td>
<td><%=rs.getString("technician")%></td>
<td><%=rs.getDate("date")%></td>
<td><div align="center">
<a href="LoadRepairEqipmentsServlet?repId=<%=repairId%>"
onclick="return edit()">Done Repair</a>
</div></td>
</tr>
<% } %>

Rails. how to display the data by column name

I use Rails 4.
I use structure
#users = ActiveRecord::Base.connection.execute("SELECT * FROM users")
#users.each do |row| %>
puts user[0] # id--> 1,2,3,4,5,6
end
#users.fields do |field| %>
puts field.name # --> id, login, password.....
end
how to display the data by column name?
for example
#users.first.field['id']
ActiveRecord::Base.connection.execute( will run the sql but not return anything (or return nil, more accurately). To load the records into memory do
#users = User.all
You don't describe what you want very well. If you literally want "1,2,3,4,5,6" you could do
#users.map(&:id).join(",")
if you wanted, for example, to do a table showing all the user data you could do (in your view)
<% cols = User.column_names %>
<table>
<thead>
<tr>
<% cols.each do |col| %>
<th><%= col %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<% cols.each do |col| %>
<td><%= user.send(col) %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
Like i say i don't know if this is what you actually want to do. Your requirements are a very messy and very vague.
EDIT
If you want to load data out of the database without using an AR model then you can do
#users = ActiveRecord::Base.connection.select_all("select * from users")
this will give you an array of hashes like this
[{"id" => "1", "name" => "foo", "bar" => "baz"}, {"id" => "2", "name" => "chunky", "bar" => "bacon"}, .... ]
which you could then access like
user = #users.first #you've got a hash now
aname = user["name"]

Trying to enable/disable a hyperlink using code given. For some reason it doesnt work as expected

Following is my code snippet. Even when the rs.getString(10) is NULL in the table, I am getting the Download link enabled instead of it to display "Cannot Download".
<table border="1" width="100%" cellpadding="0" cellspacing="0">
<tr>
<th>FileName</th>
<th>Ip</th>
<th>Date</th>
<th>Download</th>
</tr>
<%
Connection con = DbConnector.getConnection();
PreparedStatement pstm = null;
//PreparedStatement pstm1 = null;
String sql = "SELECT * FROM transaction t LEFT JOIN mykeys m ON t.FileID = m.FileID UNION ALL SELECT * FROM transaction t RIGHT JOIN mykeys m ON t.FileID = m.FileID WHERE t.FileID IS NULL and t.status='Success'";
pstm = con.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
while (rs.next())
{
%>
<tr>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<%
if(rs.getString(10) != null)
{
%>
<td>Download</td>
<%
}
else
{
%>
<td>Cannot Download</td>
<%
}%>
</tr>
<%
}
%>
</table>
I have resolved the issue. The if condition is making a reference to null which is different than making a reference to the string "NULL". My code is now working as expected.

Giving right align to numeric data in datatables

I am using jquery-datatables. I want that if the value is numeric its alignment should be right and if it is string then the alignment should be left. Is it possible with datatables or is there any method in it?
<% #tasks.each do |task| -%>
<tr>
<% col_order.each do |key| %>
<td>
<% value = task[key.column_name.split(" as ")[1]] || task[key.column_name.split(".")[1]] -%>
<% if key["drilldown_reportid"].present? %>
<%= link_to value, project_report_path(#current_project,key["drilldown_reportid"], :column=>"#{key.column_name}", :value=>"#{value}") %>
<% else %>
<%= value -%> </td>
<% end %>
<% end %>
</tr>
<% end -%>
I solved this problems by changing the client side code (not changing any server-side code).
in file css/jquery.dataTables.css,
I added a class
.alignRight { text-align: right; }
And in my javascript file for processing the data I changed "aoColumnDefs"...
///...
"aoColumnDefs" : [
//...col 1 -6
// col_07
{
"aTargets" : [ 7 ],
"fnRender" : function(oObj) {
return Math.round(oObj.aData["endingDepth"] * 100) / 100;
},
"sTitle" : "Ending Depth [m]",
"sWidth" : "5em",
"sClass" : "alignRight"
},
//... more columns
Another solution: add the following line and no need the update the CSS
///...
"aoColumnDefs" : [
//...col 1 -6
// col_07
{
"aTargets" : [ 7 ],
"fnRender" : function(oObj) {
return Math.round(oObj.aData["endingDepth"] * 100) / 100;
},
"sTitle" : "Ending Depth [m]",
"sWidth" : "5em",
"sClass" : "right"
},
//... more columns