📜 Sanskrit Shloka of the Day using JSP & JDBC
This mini-project shows how to display a Sanskrit “Shloka/Quote of the Day” in a JSP page using JDBC + MySQL. The pick is deterministic per date—so everyone sees the same shloka on the same day.
1) Database Setup (MySQL)
CREATE DATABASE iks DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE iks;
CREATE TABLE shlokas (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
devanagari TEXT NOT NULL,
transliteration TEXT,
meaning TEXT,
source VARCHAR(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO shlokas (title, devanagari, transliteration, meaning, source) VALUES
('शान्ताकारं भुजगशयनं',
'शान्ताकारं भुजगशयनं पद्मनाभं सुरेशं। विश्वाधारं गगनसदृशं मेघवर्णं शुभाङ्गम्॥',
'śāntākāraṃ bhujagaśayanaṃ padmanābhaṃ sureśaṃ | viśvādhāraṃ gaganasadṛśaṃ meghavarṇaṃ śubhāṅgam ||',
'He who is peace itself, reclines on the serpent, lotus-naveled, Lord of gods… the sustainer of the universe.',
'Vishnu Stotra'),
('कराग्रे वसते लक्ष्मीः',
'कराग्रे वसते लक्ष्मीः करमध्ये सरस्वती। करमूले तु गोविन्दः प्रभाते करदर्शनम्॥',
'karāgre vasate lakṣmīḥ karamadhye sarasvatī | karamūle tu govindaḥ prabhāte karadarśanam ||',
'Lakshmi dwells at the fingertips, Saraswati in the middle of the hand, Govinda at the base; view your hands in the morning.',
'Subhāṣita'),
('অসतो মা সদ্গময় / असतो मा सद्गमय',
'असतो मा सद्गमय। तमसो मा ज्योतिर्गमय। मृत्योर्मा अमृतं गमय॥',
'asato mā sadgamaya | tamaso mā jyotirgamaya | mṛtyor mā amṛtaṃ gamaya ||',
'Lead me from unreal to real; from darkness to light; from mortality to immortality.',
'Bṛhadāraṇyaka Upaniṣad');
2) JDBC Helper & DAO (Java)
DB.java
import java.sql.Connection;
import java.sql.DriverManager;
public class DB {
private static final String URL = "jdbc:mysql://localhost:3306/iks?useSSL=false&useUnicode=true&characterEncoding=utf8";
private static final String USER = "root";
private static final String PASS = "your_password";
public static Connection get() throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(URL, USER, PASS);
}
}
Shloka.java
public class Shloka {
public int id;
public String title;
public String devanagari;
public String transliteration;
public String meaning;
public String source;
}
ShlokaDAO.java
import java.sql.*;
public class ShlokaDAO {
public int count() throws Exception {
String sql = "SELECT COUNT(*) FROM shlokas";
try (Connection con = DB.get();
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
rs.next();
return rs.getInt(1);
}
}
public Shloka getByOffset(int offset) throws Exception {
String sql = "SELECT id, title, devanagari, transliteration, meaning, source "
+ "FROM shlokas ORDER BY id LIMIT ?, 1";
try (Connection con = DB.get();
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setInt(1, offset);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
Shloka s = new Shloka();
s.id = rs.getInt("id");
s.title = rs.getString("title");
s.devanagari = rs.getString("devanagari");
s.transliteration = rs.getString("transliteration");
s.meaning = rs.getString("meaning");
s.source = rs.getString("source");
return s;
}
}
}
return null;
}
}
3) JSP Page (Shloka of the Day)
shloka-of-the-day.jsp
<%@ page import="java.time.*, java.time.format.DateTimeFormatter" %>
<%@ page import="ShlokaDAO, Shloka" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sanskrit Shloka of the Day | JSP + JDBC</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<%
ShlokaDAO dao = new ShlokaDAO();
int total = dao.count();
LocalDate today = LocalDate.now(ZoneId.of("Asia/Kolkata"));
int dayOfYear = today.getDayOfYear();
int idx = (total > 0) ? (dayOfYear % total) : 0;
Shloka s = (total > 0) ? dao.getByOffset(idx) : null;
%>
<h1>📜 Sanskrit Shloka of the Day</h1>
<p><%= today %></p>
<% if (s != null) { %>
<h2><%= s.title %></h2>
<div><%= s.devanagari.replaceAll("\n","<br/>") %></div>
<% if (s.transliteration != null && !s.transliteration.isEmpty()) { %>
<p><strong>Transliteration:</strong><br/><em><%= s.transliteration %></em></p>
<% } %>
<% if (s.meaning != null && !s.meaning.isEmpty()) { %>
<p><strong>Meaning:</strong><br/><%= s.meaning %></p>
<% } %>
<p><strong>Source:</strong> <%= (s.source==null? "—" : s.source) %></p>
<% } else { %>
<p>No shlokas found. Please insert data into the <code>shlokas</code> table.</p>
<% } %>
</body>
</html>
4) Run
- Add MySQL Connector/J to TOMCAT_HOME/lib.
- Compile the Java files into WEB-INF/classes.
- Open
/shloka-of-the-day.jsp
in your browser.