• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » Database » How to Working with JDBC
Next →
← Prev

How to Working with JDBC

By Dinesh Thakur

Java database connectivity is essential for building stand-alone database applications and also for developing dynamic Java-based web applications. Before working with JDBC, it is required to install Java, a JDBC driver and the DBMS on the computer systems. Usually, the JDBC—ODBC bridge driver is bundled with the JDK.

We’ll be covering the following topics in this tutorial:

  • Accessing a database using JDBC
  • Registering the Driver
  • Connecting to the Database

Accessing a database using JDBC

The JDBC API provided by Sun Microsystems is designed to allow access to third-party database products. It causes the applications to be platform-independent and also allows making vendor-specific calls to take advantage of different features of database products. To be able to exploit this feature vendors must provide classes for using their database. The JDBC API also allows the programmer to access databases that reside on local as well as remote systems.

Registering the Driver

To start the process of accessing a database, first load the Driver class into the JVM for a specified JDBC driver, which allows the program to communicate with the database. Loading of JDBC Driver class registers itself with the DriverManager. The method Class.forName () is used for this purpose.

The following statements describe the way of loading drivers of the MS SQL server type

(type 4), Oracle thin driver type (type 4) and the IDBC-ODBC bridge driver type (type I),

respectively.

• Class.forName (“com.microsoft.jdbc.sqlserver.SQLServerDriver”);

• Class.forName (“oracle.jdbc.driver.OracleDriver”);

• Class.forName (“sun.jdbc.odbc.jdbcOdbcDriver”);

Before loading the Driver class, classpath is to be set equal to the driver classes. If this is not done, then the Class.forName () method throws the exception ClassNotFoundException.

Consider the example of setting the class path for an Oracle thin driver:

Set classpath=%classpath%;<location-of-oracle-driver>/classes12.jar ;

location-of-oracle-driver will specify the path of the Oracle driver class. This class had one static block. Once the class is loaded, this static block is executed for the purpose of registering the driver with the DriverManager class. This DriverManager class internally uses the Driver class.

Connecting to the Database

Once the driver is loaded, we can get the connection to the database using the static get connection () method of the DriverManager class.

Connection dbcon = DriverManager.getConnection (url, userid, password);

The getConnection () method has three parameters: url, userid and password. The parameters userid and password represent the user name and password that are used to access users’ database. DriverManager matches the parameter url with the registered driver classes and returns the Connection object related to that URL.This Connection object is used to create the JDBC statement that passes the SQL statement to the database management system.

The location of a database is specified using an URL of the following form:

<Protocol>:<subprotocol>:<subname>

Here, we are using the IDBC so protocol is normally jdbc. Subprotocol specifies the database we are using and the version of the database and subname is the name of the data source, which identifies the location of the database.

In the following example, the specification to be following when using the JDBC-ODBC bridge driver (that uses the ODBC) to access the database is given:

jdbc:odbc:dsn

Here, dsn is the data source name for any database (SQL server, Oracle, MS-access) system.

Note that subname can take a slightly different form if the database is on a remote system, it includes the machine, port number and database location on the remote system.

Connecting to a SQL server database with a type-four SQL driver

The statements to be used in this case are the following:

Class.forName (“com.microsoft.jdbc.sqlserver.SQLServerDriver”); //registering driver

String url = “jdbc:microsoft:sqlserver://”+host+”:1433″;

Connection dbcon = DriverManager.getConnection(url, userid, password);

Here, the host specifies the name of the system where the database server is located. The actual name of the host has to be substituted in place of the word host.

Connecting to Oracle server using Oracle thin driver

The statements to be used in this case are the following:

Class.forName (“oracle.jdbc.driver.OracieDriver”);

String url = “jdbc:oracle:thin:@” +host+”:1521 :”+dsource;

Connection dbcon= DriverManager.getConnection (url, userid, password);

The term dsource specifies the service name of the oracle.

Connecting to any database using the bridge driver

The statements used in this case are the following:

Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);

url = “jdbc:odbc:dsn”;

Connection dbcon=DriverManager.getConnection (url);

Here, dsn is the data source name of the database that we want to connect to. The actual name of the dsource and dsn have to be given here in a real program.

The object returned by getConnection () is a Connection object for the database. We use this connection object to submit any SQL queries or updates to the database.

Some of the methods in the DriverManager class are given in Table

Table Some important methods in the DriverManager class.

Method

Description

public static void deRegisterDriver (Driver driver) throws SQLException;  

This method unregisters or cancels the registration of the specified driver.

public static Connection getConnection (String url) throws SQLException;  

This method returns the Connection object by using the URL.

public static Connection getConnection (String url, Properties info) throws SQLException;

Properties naturally specifies user name and the password.

public static Connection getConnection (String url, String user, String pass) throws SQLException; 

The third form takes url and userid and password and returns the Connection object.

  

public static Driver getDriver (String url)

This method Returns the Driver object of the

specified URL.

public static Enumeration getDrivers () throws SQLException;  

This method Returns the all JDBC drivers currently loaded or registered with the DriverManager class.

public static void registerDriver (Driver driver) throws SQLException

This method registers the currently specified driver with the DriverManager. A newly loaded Driver implicitly calls this method.

 

You’ll also like:

  1. What is JDBC API? Important Goals of JDBC-API.
  2. WHAT IS JDBC?
  3. Explain About JDBC Architecture
  4. JDBC Processing Queries
  5. Multi-tier Applications using JDBC from a Servlet
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW