0

Is it correct that the following two are different concepts:

  • a JDBC driver (e.g. a JDBC driver for PostgreSQL) and
  • JavaSE's java.sql package?

Is it correct that the JavaSE's java.sql package is an API built upon a JDBC driver, to become driver-agnostic?

In C for PostgreSQL,

  • what is the equivalence to a JDBC driver, and
  • what is the equivalence to java.sql?
  • Is libpq more like a JDBC driver for PostgreSQL or JavaSE's java.sql package?
Mureinik
  • 297,002
  • 52
  • 306
  • 350

1 Answers1

1

Java SE' java.sql package defines interfaces that Java libraries called JDBC drivers can implement to provide the functionality of working with a database in a way that's [almost] vendor agnostic. With JDBC (the java.sql package), you [almost] never use vendor-specific classes, and just program against the interfaces.

libpq is something completely different - it's a library that's completely specific to . It makes no claim and no attempt to be a generic interface to program against any RDBMS.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thanks. Does a JDBC driver directly implement a DBMS-specific API or the DBMS-agnostic JDBC API? I guess it is the former? –  Jul 12 '18 at 19:05
  • @Ben JDBC drivers usually implement a DBMS-specific transport protocol (e.g., [this](https://www.postgresql.org/docs/10/static/protocol.html)), and expose an [almost] DBMS-agnostic interface for the programmer (e.g., implementing `java.sql.Statement`). – Mureinik Jul 12 '18 at 19:08
  • So there is nothing standing between a JDBC driver and JDBC API, because a JDBC driver has already implemented JDBC API i.e. java.sql? –  Jul 12 '18 at 19:11
  • @Ben that very much depends on the implementation. Modern day drivers are usually pure-java implementations without any component in the middle. – Mureinik Jul 12 '18 at 19:13
  • Since a JDBC driver has already implemented JDBC API i.e. java.sql, when writing a Java program, can we just `import` a JDBC driver instead of `import java.sql`? (I know the latter can reduce dependency on specific JDBC driver, and just want to know if it is possible to do the former) –  Jul 12 '18 at 19:13
  • @Ben Why don't you **try doing** that instead of asking questions that can be self-answered by experimentation? – Mark Rotteveel Jul 12 '18 at 19:43
  • I am not very familia with Java yet. @Mark –  Jul 12 '18 at 21:16
  • @Ben in theory, you can write a program completely with concrete types (e.g., form Postgresql's driver) and not directly use the interfaces from `java.sql`, but I can't think of a single good reason to do that. – Mureinik Jul 13 '18 at 07:14
  • @Ben You may want to consider learning the basics before asking esoteric questions. – Mark Rotteveel Jul 13 '18 at 09:31