PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

JDBC in Java

← All stacks

Theory

242/270

JDBC in Java

JDBC is Java’s standard way to talk to a database with SQL. Five steps: get a driver for that database brand → open a Connection (URL, user, password) → create Statement or PreparedStatement → execute and read ResultSet row by row → close resources (try-with-resources is best).

Prefer PreparedStatement. It avoids SQL injection and can reuse plans. Never paste user input into SQL with string concatenation.

Recite the five steps, then say why PreparedStatement is safer.

Let's take this on the board with one tiny Main class — no extra files. JDBC in Java — this Main really runs. If MySQL school DB is up, it prints Connected to school DB. If not, catch prints No DB yet + exception class. Steps stay: connect → prepare → execute → read → close. Start from main, go line by line, and stop at each print. That output is the proof for JDBC.

Diagram
Driver → Connection → Statement
                           │
                           ▼
                       ResultSet → close
Exam tip

Five steps + why PreparedStatement is safer.

Example

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

public class Main {
  public static void main(String[] args) {
    String url = "jdbc:mysql://localhost:3306/school";
    try {
      DriverManager.getConnection(url, "root", "secret");
      System.out.println("Connected to school DB");
    } catch (SQLException e) {
      System.out.println("No DB yet: " + e.getClass().getSimpleName());
    }
  }
}

JDBC in Java — this Main really runs. If MySQL school DB is up, it prints Connected to school DB. If not, catch prints No DB yet + exception class. Steps stay: connect → prepare → execute → read → close.

Short notes

  • DefJDBC = Java’s standard way to talk to a database with SQL.
  • RuleDriver → Connection → Statement → ResultSet → close.
  • RememberPrefer PreparedStatement. Use try-with-resources.
  • TrapString-concatenating user input into SQL (SQL injection).

Questions

1

What is JDBC?

2

Name the five steps.

3

Why PreparedStatement?

Previous← Optional class in JavaNextJDBC Driver →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.