Reading:  

Getting started with JDBC


JDBC Connection - Easy how to!

After installing the driver establishing a JDBC connection can be done as follow:

  • JDBC Package Import
  • Formulation of Database URL
  • Connection Object Creation
  • JDBC driver Register

JDBC Package Import:

Import statement is placed at beginning source code to inform java complier to check classes and reference. The JDBC packages allow select, update, delete and insert data into SQL tables.

import java.sql.* ;  // JDBC programs standard
import java.math.* ;

Register JDBC Driver:

Loading Oracle driver's class file into memory is the part of registering driver process. The registration should be done only once in program. The registration process can be done two ways :

First:

Java's Class.forName() method dynamically load the driver's class file in memory which registers automatically .

try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex) {
   System.out.println("Error: loading driver class failed!");
   System.exit(1);
}

Second:

The second approach allow to register driver using the static DriverManager.registerDriver() method.

try {
   Driver myODriver = new oracle.jdbc.driver.OracleDriver();
   DriverManager.registerDriver(myODriver);
}
catch(ClassNotFoundException ex) {
   System.out.println("Error: unable to load oracle Driver!");
   System.exit(1);
}

Formulation Database URL:

After  driver is loaded connection is established using DriverManager.getConnection() method which has three overloaded methods:

  • getConnection(String url, Properties prop)
  • getConnection(String url)
  • getConnection(String url, String user, String password)

JDBC driver and URL format:

RDBMS

JDBC driver name

format URL

MySQL

com.mysql.jdbc.Driver

jdbc:mysql://hostname1/ databaseName1

ORACLE

oracle.jdbc.driver.OracleDriver

jdbc:oracle:thin:@hostname:port Number:databaseName

Connection Object Creation:

The connection of object creation can be done in 3 different ways:

  • Database URL with username and password: The DriverManager.getConnection() requires database URL, username and password.
       String connection_url = "jdbc:oracle:thin:@abc:1521:Users";
         String UserName = "db_username";
         String Pwd = "db_password"
         Connection conn = DriverManager.getConnection(connection_url, UserName, Pwd);
  • Use only database URL: 

    In this DriverManager.getConnection( ) requires passing string url includes username and password following form: jdbc:oracle:driver:username/password@database
  • Properties object and database URL : In this getConnection( ) method requires Properties object and database URL.

    DriverManager.getConnection(String url, Properties info);
    import java.util.*;
    String URL = " jdbc:oracle:thin:@abc:1521:Users";
    Properties info = new Properties( );
    info.put( "user", "db_username" );
    info.put( "password", "db_password" );
    Connection conn1 = DriverManager.getConnection(URL, info);

 

Closing JDBC connections:

At end of JDBC program it requires explicitly close all connections to the database to each database session. Closing the connection should be done in Finally block so that it gets executed regardless of exception occurs or not.

conn1.close();

Description

This tutorial is focused on getting you started with JDBC 4.1. This tutorial has following parts

  1. Introduction
  2. SQL Syntax
  3. Setting up Environment
  4. Getting started with some samples
  5. Connecting to database
  6. Statements
  7. Result Sets
  8. Data Types
  9. Transactions
  10. Exception handling
  11. Batch Processing
  12. Streaming Data
  13. More JDBC Examples

Leave your feedback to help us improve next time. Also let us know if you see any errors that needs to be corrected



Prerequisites

Understanding of Java language a must to understand various things in this tutorial

Audience

Absolute beginners who wants to get started with JDBC

Author: Subject Coach
Added on: 8th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!