1

I want to understand how the interaction goes between JDBC and MySQL DB.

From what I am aware of, MYSQL server is written in C/C++ language. Now we used JDBC API's to do DB related interactions with MYSQL DB. MYSQL provides JDBC drivers and these drivers are loaded into the Application programs.

I believe JDBC drivers are written in Java (Pure Java JDBC driver), so how does this interaction happens between Java program and MYSQL DB (which is written in C) and assuming that MYSQL DB is running on a different physical server.

How does this work? Can anyone help me understand this?

9
  • 1
    This question is off topic. So I will not answer. But what happens here is that communication between your java app and the server occurs over TCP/IP (or unix domain sockets). With that any combination of programming languages is possible. Commented May 4, 2017 at 11:31
  • Thanks for the comment, so where can I get this info from? SO seems reliable as it has talented people from whose experience one learns. Doing google gives broad results and not sure which is the correct one. That is the reason for asking here. Thanks again Commented May 4, 2017 at 11:34
  • 1
    @Yazan: I don't think it would be JNI, because JNI is calling C/C++ from within Java program. It might be sort of Socket ; TCP/IP communication. Thanks for your points. Commented May 4, 2017 at 11:39
  • 1
    as i told you, and as it's in the wiki, sometimes it Java-->Network-->DB and some times it uses native libraries (from the database) then the native libraries does the network thing, Java-->NativeLib-->Network-->DB. have a look here oracle.com/technetwork/database/enterprise-edition/… look for this section JDBC OCI client-side driver (it's about oracle) Commented May 4, 2017 at 11:46
  • 2
    The JDBC driver implements the network communication protocol provided by the MySQL server in Java. The same (or similar) protocol is used between the mysql command line client and the server. No magic. Commented May 4, 2017 at 11:47

2 Answers 2

2

JDBC drivers are divided into four different categories:

  • Type 1: JDBC-ODBC (Open Database Connectivity) Bridge

    With type 1, a JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers.

  • Type 2: Native-API, partly Java driver

    A native-API partly Java technology-enabled driver converts JDBC calls into calls on the client API

  • Type 3: Network-protocol, all-Java driver

    A network-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server.

  • Type 4: Native-protocol, all-Java driver

    A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly.

As far as I know (please correct me if I am wrong) types 3 and 4 are the ones that are used most often in the industry, since they usually offer better performance and scalability.

If you are looking for more specific/detailed information on JDBC drivers and how they work, refer to Oracle's documentation or check out this tutorial where you can check out how to actually implement a JDBC-driver yourself

Sign up to request clarification or add additional context in comments.

1 Comment

No, type 4 is used most, followed by type 2.
2

This interaction has nothing to do with the languages used for mysql implementation or the client implementation. This is a pure network communication using TCP/IP (Transmission Control Protocol/Internet Protocol) which is the standard communication language or protocol between two network hosts.

So where is the jdbc role here? Simply say, the jdbc just creates the data payload which is wrapped by TCP/IP protocol headers and sent to mysql host. Upon receiving the network request, mysql host unwraps the data payload, passes it to mysql server application and as the data payload was created by jdbc, the mysql server can parse and understand it.

1 Comment

Makes sense , nice one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.