technical training coding
technical training coding
Copy code
// Q1: Select all rows from a table based on sorting order of salaries
import java.sql.*;
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " +
rs.getDouble("salary"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
java
Copy code
// Q2: Select range of records based on address
import java.sql.*;
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " +
rs.getString("address"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
java
Copy code
// Q3: Select range of records based on initial characters of the employee name
import java.sql.*;
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " +
rs.getString("address"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
java
Copy code
// Q4: Check if a string of parentheses is balanced
import java.util.Stack;
if (choice == 1) {
System.out.print("Enter user ID: ");
int userId = sc.nextInt();
System.out.print("Enter amount: ");
double amount = sc.nextDouble();
System.out.print("Enter category: ");
String category = sc.next();
System.out.print("Enter date (YYYY-MM-DD): ");
String date = sc.next();
String query = "INSERT INTO expenses (user_id, amount, category, date)
VALUES (?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, userId);
ps.setDouble(2, amount);
ps.setString(3, category);
ps.setDate(4, Date.valueOf(date));
ps.executeUpdate();
System.out.println("Expense added!");
} else if (choice == 2) {
System.out.print("Enter category: ");
String category = sc.next();
while (rs.next()) {
System.out.println(rs.getInt("expense_id") + " " +
rs.getDouble("amount") + " " + rs.getString("date"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
java
Copy code
// Q6: JDBC Application for Banking System
import java.sql.*;
import java.util.Scanner;
if (choice == 1) {
System.out.print("Enter name: ");
String name = sc.next();
System.out.print("Enter initial balance: ");
double balance = sc.nextDouble();
while (rs.next()) {
System.out.println(rs.getInt("transaction_id") + " " +
rs.getString("type") + " " + rs.getDouble("amount"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
java
Copy code
// Q7: JDBC Application for Employee Management System
import java.sql.*;
import java.util.Scanner;
if (choice == 1) {
System.out.print("Enter name: ");
String name = sc.next();
System.out.print("Enter email: ");
String email = sc.next();
System.out.print("Enter department ID: ");
int departmentId = sc.nextInt();
System.out.print("Enter salary: ");
double salary = sc.nextDouble();
String query = "INSERT INTO employees (name, email, department_id,
salary) VALUES (?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, name);
ps.setString(2, email);
ps.setInt(3, departmentId);
ps.setDouble(4, salary);
ps.executeUpdate();
System.out.println("Employee added!");
} else if (choice == 4) {
String query = "SELECT * FROM employees";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println(rs.getInt("employee_id") + " " +
rs.getString("name") + " " + rs.getDouble("salary"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
java
Copy code
// Q8: JDBC Application for Library Management System
import java.sql.*;
import java.util.Scanner;
if (choice == 1) {
System.out.print("Enter title: ");
String title = sc.next();
System.out.print("Enter author: ");
String author = sc.next();
System.out.print("Enter publisher: ");
String publisher = sc.next();
System.out.print("Enter quantity: ");
int quantity = sc.nextInt();
Viva:
Explanation:
Packages used:
o java.sql.*: This package provides the JDBC API for database connectivity, including
Connection, Statement, and ResultSet.
Statements used:
o Statement stmt = conn.createStatement();: Creates a Statement object to execute
SQL queries.
o stmt.executeQuery(query);: Executes the query to retrieve the data from the database.
Viva:
Explanation:
Packages used:
o java.sql.*: For JDBC classes like Connection, Statement, ResultSet.
Statements used:
o SELECT * FROM employees WHERE address LIKE 'New York%': SQL query to filter
employees with addresses starting with 'New York'.
Q3: Select range of records based on initial characters of the employee name
Viva:
1. What is the purpose of % in the query?
o The % acts as a wildcard in SQL, meaning it can match any characters after the letter 'A'. This
helps in filtering employees whose name begins with 'A'.
2. How does executeQuery() method work in the program?
o executeQuery() is used to execute SQL SELECT statements and returns a ResultSet
object containing the result of the query.
Explanation:
Packages used:
o java.sql.*: For database connection, creating statement, and processing results.
Statements used:
o stmt.executeQuery(query);: Executes the SQL query to retrieve the data.
Viva:
Explanation:
Packages used:
o java.util.Stack: For using the stack data structure to store characters.
Statements used:
o stack.push(ch);: Pushes a character onto the stack.
o stack.pop();: Removes the top character from the stack.
o The checks inside if ensure that the parentheses match.
Viva:
Explanation:
Packages used:
o java.sql.*: For database connection, prepared statement, result set processing.
o java.util.Scanner: For reading user input.
Statements used:
o ps.setInt(), ps.setDouble(), ps.setString(): These are used to bind the user's input to
the placeholders in the query.
o ps.executeUpdate();: Executes the update query (like insertion).
Viva:
Explanation:
Packages used:
o java.sql.*: For database connections and performing queries.
o java.util.Scanner: To read user inputs.
Statements used:
o PreparedStatement ps = conn.prepareStatement(query);: Prepares the SQL query for
secure execution.
o ps.executeUpdate();: Executes insertion or update commands in the database.
Viva:
Explanation:
Packages used:
o java.sql.*: Includes classes for connecting to the database and managing queries like
Statement and ResultSet.
Statements used:
o ps.setString(), ps.setInt(), ps.setDouble(): Binding user input into the query using
PreparedStatement.
o stmt.executeQuery(query);: Executes the SQL query to retrieve the employee list.
Explanation:
Packages used:
o java.sql.*: Includes essential JDBC classes like Connection, Statement, and ResultSet.
Statements used:
o PreparedStatement: Used to safely insert book data into the database.
o stmt.executeQuery(): Executes the query and retrieves a list of all books.
These explanations cover the core concepts, packages, and statements used in each example, focusing on
JDBC operations for database handling, user input management, and SQL execution in Java applications.