0% found this document useful (0 votes)
6 views2 pages

JSTL Vs Scriptlets Database

Uploaded by

ေမဇင္
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

JSTL Vs Scriptlets Database

Uploaded by

ေမဇင္
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

JSTL vs Scriptlets in Database Operations

When working with databases in JSP, developers can either use Scriptlets (Java code
directly) or JSTL (JSP Standard Tag Library). Below is a comparison of both approaches.

1. Scriptlet Style (Traditional JDBC in JSP)


<%@ page import="java.sql.*" %>
<html>
<body>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT id, name FROM students");

while(rs.next()){
out.println("<p>ID: " + rs.getInt("id") + " - Name: " + rs.getString("name") + "</p>");
}
} catch(Exception e){
out.println("Error: " + e.getMessage());
} finally {
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
%>
</body>
</html>

2. JSTL Style (Using <sql:query>)


<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>

<sql:setDataSource var="db" driver="com.mysql.cj.jdbc.Driver"


url="jdbc:mysql://localhost:3306/testdb"
user="root" password=""/>
<sql:query dataSource="${db}" var="result">
SELECT id, name FROM students;
</sql:query>

<table border="1">
<tr><th>ID</th><th>Name</th></tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td>${row.id}</td>
<td>${row.name}</td>
</tr>
</c:forEach>
</table>

</body>
</html>

3. Side-by-Side Comparison
Feature Scriptlet (JDBC in JSP) JSTL (sql tag)

Code Style Java code in JSP using JDBC XML-like tags

Complexity Long try-catch, manual Simple and clean


connection handling

Readability Hard for beginners Easy to understand

Best Use Case Good for learning raw JDBC Good for demos/teaching

Enterprise Practice ❌ Not recommended ✅ Cleaner, but still DAO


preferred

4. Teaching Tip for Students


- Start with Scriptlet JDBC to show how messy it looks.
- Then show JSTL sql:query to demonstrate cleaner code.
- Finally, explain that in real enterprise projects, database logic should go into Servlets/DAO
layer, not directly in JSP.

You might also like