StudyLover
  • Home
  • Study Zone
  • Features
    • Workout Interval Timer
    • Typing Tutor
  • Profiles
  • Contact us
  • Sign in
StudyLover The SQL Executor
Download
  1. Java
  2. Programs
The JDBC Connector : The Hello Servlet
Programs
Initiative: To run secure SQL queries (CRUD) using Statement and PreparedStatement.
import java.sql.*; // Import SQL classes

public class SqlExecutor { // Define class for executing SQL commands from Java
    public static void main(String[] args) { // Program entry point
        String url = "jdbc:mysql://localhost:3306/lab_db"; // Database URL
        String user = "root"; // DB Username
        String pass = "pass123"; // DB Password
        
        System.out.println("--- SQL Command Execution Lab ---"); // Print system header
        
        // Use try-with-resources to manage DB connection and statements
        try (Connection c = DriverManager.getConnection(url, user, pass); // Connect to DB
             Statement s = c.createStatement()) { // Create a generic statement for static SQL
            
            // 1. CREATE TABLE (DDL)
            String create = "CREATE TABLE IF NOT EXISTS Students (id INT PRIMARY KEY, name VARCHAR(50))"; // SQL string
            s.executeUpdate(create); // Execute the table creation command
            System.out.println("Table 'Students' is verified/created."); // Log status
            
            // 2. SECURE INSERT (DML)
            String insert = "INSERT INTO Students (id, name) VALUES (?, ?)"; // SQL with placeholders
            try (PreparedStatement ps = c.prepareStatement(insert)) { // Create a secure prepared statement
                ps.setInt(1, 101); // Set the first '?' to the integer ID 101
                ps.setString(2, "James Watson"); // Set the second '?' to the string name
                ps.executeUpdate(); // Run the insertion query safely
                System.out.println("Successfully inserted ID 101 using Prepared Statement."); // Log success
            } // End of prepared statement try
            
            // 3. READ DATA (SELECT)
            try (ResultSet rs = s.executeQuery("SELECT * FROM Students")) { // Execute select and get results
                System.out.println("\n--- Current Student Records ---"); // Header for results
                while (rs.next()) { // Iterate through the rows returned by SQL
                    System.out.println("ID: " + rs.getInt("id") + " | Name: " + rs.getString("name")); // Print row data
                } // End of result iteration
            } // End of result set try
            
        } catch (SQLException e) { // Catch any database-related exceptions
            System.err.println("SQL Execution Error: " + e.getMessage()); // Print error message
        } // End of main catch block
        
        System.out.println("SQL Lab: Operations Complete."); // Final log for program end
    } // End of main method
} // End of SqlExecutor class
The JDBC Connector The Hello Servlet
Our Products & Services
  • Home
Connect with us
  • Contact us
  • info.studylover@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website