Showing posts with label JDBC. Show all posts
Showing posts with label JDBC. Show all posts

META DATA

The data about data is called as MetaData. It gives more information about the original data. In JDBC we have 3 metadata objects. They are:
  1. ResultSetMetaData (provides information about ResultSet object)
  2. DatabaseMetaData (provides information about Database)
  3. ParameterMetaData (provides information about Parameters [positional parameters])

Role of Exceptions in JDBC

In JSE API, java.sql package comes under JDBC. When we observe this package, every method in this package throws an SQLException. That means the successful working of these method is based on user. In order to overcome this problem user should write error less code. Since every method throws an exception it is recommended to handle the exception instead of throwing them.

Ways to register a JDBC Driver

We can register a JDBC driver in more than 10 ways. But there are majorly four ways to register a JDBC Driver. They are as follows:
  1. DriverManager.registerDriver();
  2. Class.forName();
  3. predefined System property(jdbc.driver);
  4. JDBC 4.0 Auto Loading of the JDBC Driver.

Working with PreparedStatement

In JDBC, we have the following three Statement objects.
  • Statement
  • PreparedStatement
  • CallableStatement 
We have done with Statement, now let us know PreparedStatement. 

Communicating with DataBase Sever

Up to now we have seen how to register the driver and how to establish the connection with DB server.  Now let us see how to communicate with the database using java application. We can send the query to the database using the Statement object. Let us see how to create this object. Follow the below steps.


Goto JSE API - java.sql package - Connection Interface - Method Summary - createStatement(). The following figure shows the method. 

createStatement() method in JSE API
Fig 1: createStatement() method in JSE API

Downloading & Installing of Oracle XE DataBase Server

Download:
       In order to work with database server we need database software. Many companies provide DataBase server software, we can download any one. Now let us see how to download and install Oracle Database. This Oracle Database software is of two types:
  • Oracle Database Enterprize Edition
  • Oracle Database Xpress Edition

Establishing the connection with DataBase Server

In order to establish a connection with DB server, first we need to know, which class is used to establish the connection. DriverManager is the class which contains the method named getConnection(). The following figure shows the syntax for this method. 
Syntax of getConneciton() method
Fig: method in DriverManager class that connects DBServer
      In the above diagram, observe the third syntax, it contains three parameters and it returns a Connection class object. Connection is an interface, but the class which implements this Connection interface is called as Connection class, the object of which this method returns. To store this object we should create a reference variable as follows. 
  •  Connection con=DriverManager.getConnection("url ", " usr_name" , "psswrd" );
In the above syntax, since getConnection() is a static method we are calling it using its class name. Coming to the parameters, it has three parameters. The following program shows, how to connect with database.

program to establish connection with DBserver
Fig: program to establish connection with DBserver
  

First step towards JDBC Programming

I recommend to read JDBC introduction before reading this post..

Before moving to the programming, let us first know what are the important Classes and Interfaces in JDBC API. It contains of two important packages. They are: 
  1. java.sql 
  2. javax.sql
The following figure shows the important Classes and Interfaces in these two packages.

JDBC Introduction

JDBC is an acronym Java Data Base Connectivity. JDBC helps us to connect to the database and execute SQL statements against the database. First let us know the basics of JDBC. Why we are using Java instead of other languages like C language.