Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

find the error in the java code: <%@ page import=\"java.sql.*\" %> <%@ page impo

ID: 3754930 • Letter: F

Question

find the error in the java code:

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

<%@ page import="java.io.*" %>

<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>

<html>

<head>

<title> Debug Week 6 </title>

</head>

<bodt>

<h1>The Employee Database Table </h1>

<%

Connection connection = DriverManager.getConnection(

"jdbc:odbc:data", "student1", "pass");

Statement statement = connection.createStatement() ;

ResultSet resultset = statement.executeQuery("SELECT empno, ename, job, sal, comm FROM emp") ;

%>

<%

GregorianCalendar currentDate = new GregorianCalendar();

int currentYear = currentDate.get(Calendar.YEAR);

%>

<table border="1">

<tr>

<th>Employee Number</th>

<th>Name</th>

<th>Job</th>

<th>Salary</th>

<th>Commision</th>

</tr>

<%= while(resultset.next()){ %>

<tr>

<td> <%= resultset.getString(1) %></td>

<td> <%= resultset.getString(2) %></td>

<td> <%= resultset.getString(3) %></td>

<td> <%= resultset.getString(4) %></td>

<td> <%= resultset.getString(5) %></td>

</tr>

<%= } %>

  

<p>Published <%= currentYear %> </p>

  

</table>

</body>

</html>

Explanation / Answer

Error:

Reason:

Jsp tag "<%=" is value tag and this is used in order to fetch a particular value either from the java part or from any user defined expressions. Hence, using this with while loop creates problems as while is a statement not a value.

Note:

Sometimes, error also occurs due to the use of ResultSet in JSTL.

In order to use resultSet with JSTL, we need to convert "java.sql.ResultSet" to the object of "javax.servlet.jsp.jstl.sql.Result".

For that we need to follow certain steps as mentioned below:

<c : forEach var = "row" items = "${result.rows}" >

<tr>

<td> <c:out value="${row.empno}"/> </td>

<td> <c:out value="${row.ename}"/> </td>

<td> <c:out value="${row.job}"/> </td>

<td> <c:out value="${row.sal}"/> </td>

<td> <c:out value="${row.comm}"/> </td>

</tr>

</c:forEach>

Note:

Use the second method only when error persists even after the solution provided at the top.