Search Tutorials


Java JDBC MCQ Questions and Answers | JavaInUse

Java JDBC MCQ Questions and Answers

Q. What is JDBC in Java?

A. Java DataBase Connectivity
B. Java Database Connection
C. Java Data Base Connector
D. None of the above

Q. What is the purpose of the DriverManager class in JDBC?

A. To manage database connections
B. To execute SQL queries
C. To manage database transactions
D. To provide database drivers

Q. Which interface is used to execute SQL statements in JDBC?

A. Connection
B. Statement
C. ResultSet
D. PreparedStatement

Q. What is the purpose of the Connection interface in JDBC?

A. To manage database connections
B. To execute SQL queries
C. To manage transactions
D. All of the above

Q. How do you establish a connection to a database using JDBC?

A. By creating an instance of the Connection interface
B. By using the DriverManager.getConnection() method
C. By implementing the Connection interface
D. By extending the Connection class

Q. What is the purpose of the ResultSet interface in JDBC?

A. To store the result of a query
B. To execute SQL queries
C. To manage database connections
D. To update data in the database

Q. What is a PreparedStatement in JDBC?

A. A pre-compiled SQL statement
B. A statement used for executing queries
C. A statement used for updating data
D. A statement used for managing transactions

Q. How do you close a ResultSet in JDBC?

A. By calling the close() method on the ResultSet object
B. By calling the closeResultSet() method on the Connection object
C. By setting the ResultSet object to null
D. By calling the closeAll() method on the Statement object

Q. What is connection pooling in JDBC?

A. A technique to manage and reuse database connections
B. A method to pool and distribute database queries
C. A way to pool and manage database transactions
D. A mechanism to pool and optimize SQL statements

Q. What is the difference between Statement and PreparedStatement in JDBC?

A. Statement is used for static SQL queries, while PreparedStatement is used for dynamic queries with parameters.
B. Statement is used for executing queries, while PreparedStatement is used for executing updates.
C. Statement is pre-compiled, while PreparedStatement is compiled at runtime.
D. All of the above





Q. How do you handle database exceptions in JDBC?

A. By using try-catch blocks to catch SQLException
B. By implementing the Exception interface
C. By extending the SQLException class
D. By using the finally block to handle exceptions

Q. What is the purpose of the setAutoCommit(false) method in JDBC?

A. To enable auto-commit mode
B. To disable auto-commit mode and start a transaction
C. To commit a transaction
D. To roll back a transaction

Q. How do you retrieve data from a ResultSet in JDBC?

A. By using getter methods such as getString(), getInt(), etc.
B. By calling the getData() method on the ResultSet object
C. By using the getRow() method to access data by row
D. By iterating through the ResultSet using a for loop

Q. What is the purpose of the commit() method in JDBC?

A. To commit a transaction and make changes permanent
B. To start a new transaction
C. To roll back a transaction
D. To save changes to a database table

Q. How do you roll back a transaction in JDBC?

A. By calling the rollback() method on the Connection object
B. By setting the auto-commit mode to true
C. By calling the commit() method with a false parameter
D. By closing the Connection object

Q. What will be the output of the following code snippet that uses Java Database Connectivity (JDBC) to interact with a database?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseConnection {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            System.out.println("Connected to the database successfully.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
A.
Connected to the database successfully.
B.
Error: Could not establish a connection.
C.
Exception in thread "main" java.sql.SQLException: No suitable driver found
D.
Connected to the database, but an error occurred.

Q. Which of the following code snippets demonstrates the correct way to execute an SQL SELECT query using JDBC and retrieve the results?

A.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class QueryExecution {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM employees");
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                System.out.println("ID: " + id + ", Name: " + name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
B.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class QueryExecution {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM employees");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString("name");
                System.out.println("ID: " + id + ", Name: " + name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
C.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class QueryExecution {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM employees");
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("ID: " + id + ", Name: " + name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
D.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class QueryExecution {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM employees WHERE id = ?");
            preparedStatement.setInt(1, 123);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                System.out.println("ID: " + id + ", Name: " + name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Q. Which of the following code snippets demonstrates the correct way to insert data into a database table using JDBC?

A.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DataInsertion {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO employees (name, age) VALUES (?, ?)");
            preparedStatement.setString(1, "John");
            preparedStatement.setInt(2, 30);
            preparedStatement.executeUpdate();
            System.out.println("Data inserted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
B.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DataInsertion {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            Statement statement = connection.createStatement();
            statement.executeUpdate("INSERT INTO employees (name, age) VALUES ('John', 30)");
            System.out.println("Data inserted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
C.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DataInsertion {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO employees VALUES (?, ?)");
            preparedStatement.setString(1, "John");
            preparedStatement.setInt(2, 30);
            preparedStatement.executeUpdate();
            System.out.println("Data inserted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
D.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DataInsertion {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            Statement statement = connection.createStatement();
            statement.executeUpdate("INSERT INTO employees VALUES ('John', 30)");
            System.out.println("Data inserted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Q. Which of the following code snippets demonstrates the correct way to update data in a database table using JDBC?

A.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DataUpdate {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            PreparedStatement preparedStatement = connection.prepareStatement("UPDATE employees SET age = ? WHERE name = ?");
            preparedStatement.setInt(1, 30);
            preparedStatement.setString(2, "John");
            preparedStatement.executeUpdate();
            System.out.println("Data updated successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
B.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DataUpdate {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            Statement statement = connection.createStatement();
            statement.executeUpdate("UPDATE employees SET age = 30 WHERE name = 'John'");
            System.out.println("Data updated successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
C.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DataUpdate {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            PreparedStatement preparedStatement = connection.prepareStatement("UPDATE employees SET age = 30");
            preparedStatement.setString(1, "John");
            preparedStatement.executeUpdate();
            System.out.println("Data updated successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
D.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DataUpdate {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            Statement statement = connection.createStatement();
            statement.executeUpdate("UPDATE employees SET age WHERE name = 'John'");
            System.out.println("Data updated successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Q. Which of the following code snippets demonstrates the correct way to delete data from a database table using JDBC?

A.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DataDeletion {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM employees WHERE name = ?");
            preparedStatement.setString(1, "John");
            preparedStatement.executeUpdate();
            System.out.println("Data deleted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
B.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DataDeletion {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            Statement statement = connection.createStatement();
            statement.executeUpdate("DELETE FROM employees WHERE name = 'John'");
            System.out.println("Data deleted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
C.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DataDeletion {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM employees");
            preparedStatement.setString(1, "John");
            preparedStatement.executeUpdate();
            System.out.println("Data deleted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
D.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DataDeletion {
    public static void main(String[] args) {
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
            Statement statement = connection.createStatement();
            statement.executeUpdate("DELETE FROM employees SET name = 'John'");
            System.out.println("Data deleted successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}