Regarding Incorrect Answer in Assignment 11, Question 5 – Programming in Java

66 views
Skip to first unread message

Anup Pandey

unread,
May 5, 2025, 1:57:30 AM5/5/25
to Discussion forum for Programming In Java

Dear NPTEL Support Team,

I hope this message finds you well.

I am writing to bring to your attention a possible error in Assignment 11 of the Programming in Java course. Specifically, there seems to be a mistake in the answer key for Question 5, which involves inserting a new user into a database using JDBC.

Below is the question for your reference:


---

Question 5:

Complete the following code to insert a new user into a users table.

import java.sql.*;

public class InsertUser {
    public static void main(String[] args) {
        try {
            Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/mydb", "root", "password");

            String query = "INSERT INTO users (username, email) VALUES (?, ?)";

            PreparedStatement pstmt = // INSERT CODE HERE;
            pstmt.setString(1, "john doe");
            pstmt.setString(2, "jo...@example.com");
            pstmt.executeUpdate();

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

What should replace // INSERT CODE HERE?

a. conn.createStatement(query);
b. conn.prepareStatement(query);
c. conn.execute(query);
d. conn.runStatement(query);


---

The system currently marks option (a) as correct. However, this appears to be incorrect because conn.createStatement(query) is not a valid method in JDBC and will result in a compile-time error. The correct approach to use a parameterized query with placeholders (?) is:

PreparedStatement pstmt = conn.prepareStatement(query);

Hence, the correct answer should be option (b).


I kindly request you to review this question and consider correcting the answer key or issuing necessary clarification. Thank you for your time and support.

Sincerely,
Anup Kumar Pandey
darede...@gmail.com
Course: Programming in Java

Reply all
Reply to author
Forward
0 new messages