JDBC – Java Database Connectivity

Java Database Connectivity (JDBC), which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.
It has four major parts:

  • Making a connection to a database
  • Creating SQL or MySQL statements
  • Executing that SQL or MySQL queries in the database
  • Viewing and modifying the resulting records

Step 1

Open a new Java Application –> Right-click on the project –> select Properties. Then from the left menu select libraries.

selecting JAR file
selecting JAR file

Select Add JAR/Folder.

select the mysql-connector-java-5.1.6-bin.jar file. Which can be found in the Netbeans folder. For Mac, it is in /Application/Netbeans/Netbeans 6.8/contents/Resources/ide12/modules/ext/mysql-connector-java-5.1.6-bin.jar

Now are able to use the JDBC in your program frequently 🙂

Step 2

Here, I’ll be talking about connecting to a MySQL database. But it can be used for other cases also.

Selecting a right driver will be another major task.

For ORACLE the driver will be oracle.jdbc.driver.OracleDriver

For DB2 the driver will be COM.ibm.db2.jdbc.net.DB2Driver

For Sybase the driver will becom.sybase.jdbc.SybDriver

import java.sql.*;

Load the driver class using Class.forname(com.sybase.jdbc.SybDriver)

Define the data source for the driver String sourceURL = “jdbc:mysql://URL/database_name”

Create a connection through the DriverManager

Connection databaseConnection = DriverManager.getConnection(sourceURL)

All this should be in a try-catch block.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s