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
- Connect using a connection string or file path.
- Prepare SQL with placeholders for values.
- Execute with the values.
- Read rows, or commit changes.
- 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
| Language | Typical driver | Placeholder | Cleanup |
|---|---|---|---|
| Python | sqlite3, psycopg | ? or %s | close(), with blocks for transactions |
| JavaScript | pg, mysql2 | $1 or ? | end() or release() |
| Java | JDBC driver per database | ? | try-with-resources |
| C# | SqlClient, Npgsql | @name | using |
The same mistakes in every language
- Gluing values into SQL strings. Always use placeholders.
- Forgetting to commit when autocommit is off.
- Not closing connections, which eventually exhausts the database's connection limit.
- Putting passwords in code. Read them from environment variables instead; see keeping a connection string in an environment variable.
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.