Java is one of the most powerful development tools in the world, gaining more and more followers and fans.

Accessing Data with JDBC

Without a doubt the data access is one of the most used features in development. Practically the whole system needs a database, with rare exceptions.

Java, different than languages ​​like PHP does not support access to the database directly, so that it uses an API (group of classes and interfaces) to do the job. The JDBC (Java Database Connectivity) make the sending of SQL statements to any relational database, provided there is a driver that matches the same gift.

Figure 1: Schematic of operation of the JDBC

There are four types of JDBC drivers: 1, 2, 3 and 4, they are:

Type 1: JDBC-ODBC Bridge

It is the simplest type but restricted to the Windows platform. Uses ODBC to connect to the database, converting methods in JDBC calls to ODBC functions. This bridge is typically used when there is a pure-Java driver (type 4) for a given database, because its use is discouraged due to the dependence of the platform.

Type 2: Native-API Driver

The Native-API driver translates JDBC calls to the API calls customer database used. As the JDBC-ODBC Bridge, may need extra software installed on the client machine.

Type 3: Driver Network Protocol

Translates the JDBC call to a network protocol independent of the database used, which is translated into the protocol database for a server. By using an independent protocol, clients can connect Java applications to several different databases. It's more flexible model.

Type 4: Native Driver

Converts JDBC calls directly into the protocol database. Implemented in Java, normally is independent platform and written by the developers themselves. It is the most recommended to be used.

In this article we use the type 4 being the most recommended. Because he takes calls directly in the protocol of the database in question, thus giving better performance, besides, of course, being the simplest to use.

Many may find a certain similarity between JDBC and ODBC, and are absolutely correct, we can say "roughly" the two follow the same idea. Both work as a communication application x Bank, but ODBC is a Windows application restricted to it, while the JDBC because it is written in java, is multiplatform.

Another advantage of JDBC is that it act as a data abstraction layer. Independent of SGBD used, the API is the same, greatly facilitating the life of programmers if there is a need for a database migration.

Class.forName - This is without doubt the main command. It is through him that we are calling the JDBC driver.

Connection - Here we are creating an object of type "Connection". This is where the information is stored in your database connection. To be more direct, we use the method "getConnection" of the object "DriverManager" contained in "java.sql" instead of the default constructor, this causes the connection is established immediately. Note the string passed as parameter, the information contained in it are our connection, they are respectively: JDBC driver, host, database path, username and password for last.

Statement - a statement is simple, but vital for any project. The object "Statement" is responsible for receiving commands and SQL to relay information, as well as the return.

Now that we know how to create the connection and loading the driver, what do you think of seeing some basic operations?

Deleting all records from the table

We will see now how to delete all records of a table. Be very careful with this code because it will clear all records of your table.

To have full access to this post (or download the associated files) you must have MrBool Credits.

See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video.Subscribe for this package!