Querying IKS Data with JDBC API – Statement, PreparedStatement & CallableStatement Examples



Querying IKS Data with JDBC API

JDBC (Java Database Connectivity) provides several interfaces to execute SQL queries and update data from Java programs. In the context of Indian Knowledge Systems (IKS), these APIs can be used to retrieve, update, and manage heritage data such as Vedic rituals, traditional texts, and Yoga poses.

Key JDBC Interfaces for Querying

  1. Statement: Used for executing static SQL queries without parameters. Best for quick retrievals.
  2. PreparedStatement: Used for precompiled SQL queries with parameters. Helps prevent SQL injection and improves performance.
  3. CallableStatement: Used to call stored procedures in the database.

Example: Using Statement and PreparedStatement

import java.sql.*;

public class IKSQueryExample {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/iks_db";
        String user = "root";
        String password = "your_password";

        try (Connection con = DriverManager.getConnection(url, user, password)) {

            // Using Statement
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM vedic_rituals");
            while (rs.next()) {
                System.out.println(rs.getString("ritual_name"));
            }

            // Using PreparedStatement
            PreparedStatement ps = con.prepareStatement(
                "SELECT * FROM yoga_poses WHERE difficulty = ?");
            ps.setString(1, "Beginner");
            ResultSet rs2 = ps.executeQuery();
            while (rs2.next()) {
                System.out.println(rs2.getString("pose_name"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Example: Using CallableStatement

CallableStatement cs = con.prepareCall("{call get_sanskrit_terms()}");
ResultSet rs3 = cs.executeQuery();

Tip: Use PreparedStatement for most IKS data retrievals to ensure secure and efficient queries.

Post a Comment

Thanks for comment.

Previous Post Next Post