Sobes.tech
Junior — Middle

What methods of connecting to a database have you used in your projects?

sobes.tech AI

Answer from AI

Connecting to a database in projects is usually done in the following ways:

  • Using drivers and libraries for specific DBMS (for example, JDBC for Java, psycopg2 for PostgreSQL in Python, ADO.NET for C#).
  • Through ORM (Object-Relational Mapping) — for example, Entity Framework, Hibernate, Sequelize, which simplify working with the database by allowing you to work with objects instead of direct SQL queries.
  • Using connection pools to optimize database operations and reduce the overhead of establishing connections.

Example of connecting to a PostgreSQL database in Python using psycopg2:

import psycopg2

conn = psycopg2.connect(
    dbname="mydb",
    user="user",
    password="password",
    host="localhost",
    port=5432
)
cursor = conn.cursor()
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()
for row in rows:
    print(row)
conn.close()
What methods of connecting to a database have you… - sobes.tech