Skip to content
Exercises · Q10

Q.List, in order, the five steps a Java program follows to connect to and read from a MySQL database using JDBC, naming the key object involved in each step.

ChseodishaTextbookSubjectiveImportance★★★★★est
71% · 10/14 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

A JDBC program always follows the same five steps, each using a specific JDBC object:

(i) Load the driver. Tell Java which driver knows how to talk to MySQL:

Class.forName("com.mysql.cj.jdbc.Driver");

(ii) Establish a connection. Ask DriverManager to open a link to the database, giving its address, username and password; it returns a Connection object:

Connection con = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/school", "root", "pass123");

(iii) Create a statement. Make a Statement object from the connection to carry SQL:

Statement st = con.createStatement();

(iv) Execute the SQL. Run the command. A SELECT uses executeQuery, which returns a ResultSet — the table of rows — read one row at a time with next():

ResultSet rs = st.executeQuery("SELECT name, marks FROM student");
while (rs.next()) {
    System.out.println(rs.getString("name") + " - " + rs.getInt("marks"));
}
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.