The Thorough Guide to Database Drivers in Four Languages

Whatever language you learn, talking to a database follows the same five steps: connect, send SQL with parameters, read results, commit changes, close. This free guide shows those steps side by side in Python, JavaScript, Java and C#, so you can see the shared pattern behind different syntax.

The five steps

  1. Connect using a connection string or file path.
  2. Prepare SQL with placeholders for values.
  3. Execute with the values.
  4. Read rows, or commit changes.
  5. Close the connection, or return it to a pool.

Python with sqlite3

import sqlite3
with sqlite3.connect("app.db") as conn:
    rows = conn.execute("SELECT name FROM members WHERE id = ?", (5,)).fetchall()

Placeholder: ?. The module is part of Python's standard library. Note that the with block commits or rolls back; it does not close the connection, so call conn.close() when you are finished in longer programs.

JavaScript (Node.js) with pg for PostgreSQL

const { Client } = require("pg");
const client = new Client({ connectionString: process.env.DATABASE_URL });
await client.connect();
const res = await client.query("SELECT name FROM members WHERE id = $1", [5]);
console.log(res.rows);
await client.end();

Placeholder: $1, $2 and so on. Most Node.js drivers are asynchronous, so you use await.

Java with JDBC

try (Connection conn = DriverManager.getConnection(url, user, password);
     PreparedStatement ps = conn.prepareStatement("SELECT name FROM members WHERE id = ?")) {
    ps.setInt(1, 5);
    try (ResultSet rs = ps.executeQuery()) {
        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }
    }
}

Placeholder: ?, filled by position with setInt, setString and so on. try-with-resources closes everything automatically.

C# with ADO.NET (Microsoft.Data.SqlClient for SQL Server)

using var conn = new SqlConnection(connectionString);
conn.Open();
using var cmd = new SqlCommand("SELECT name FROM members WHERE id = @id", conn);
cmd.Parameters.AddWithValue("@id", 5);
using var reader = cmd.ExecuteReader();
while (reader.Read()) Console.WriteLine(reader.GetString(0));

Placeholder: named parameters such as @id. using disposes the objects when done.

Side by side

LanguageTypical driverPlaceholderCleanup
Pythonsqlite3, psycopg? or %sclose(), with blocks for transactions
JavaScriptpg, mysql2$1 or ?end() or release()
JavaJDBC driver per database?try-with-resources
C#SqlClient, Npgsql@nameusing

The same mistakes in every language

Where next

Pick your language and follow its driver's official getting-started page, then rewrite one of your practice queries in it. For the idea behind drivers, read the translator in the middle.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

This page is a free outline. No downloadable files, paid content, courses or certificates are offered on this site.