What is JDBC?
JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.
The JDBC library includes APIs for each of the tasks commonly associated with database usage:
JDBC Environment setup
Before connecting your code to the database,make sure that you have done following setup:
1. Core JAVA Installation
2. Any Database Installation
Apart from the above you need to setup a database which you would use for your project.
Creating JDBC Application:
JDBC application creation can be broadly divided into some easy and small steps.
Step 1: Importing required packages
Step 2: Register JDBC driver
It basically initializes a driver so that you can open a communication channel with the database.Following is the code snippet to achieve this:
Step 3: Open a Connection
This step requires a method i.e. DriverManager.getConnection() method to be create a Connection object which represents a physical connection with the database.
Step 4 : Executing a query
This step will help you to build a statement that will build and submit as a SQL statement to the database.
Step 5: Extracting data from result set
This step will help you in fetching data from database.You can use the appropriate Result.getXYZ()
Step 6: Closing the environment
Its a good habit to close all database resources before program exits.
This post was just a simple introduction about JDBC,in the next post we will discuss about the new and powerful database i.e. PostgreSQL.
JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.
The JDBC library includes APIs for each of the tasks commonly associated with database usage:
- Making a connection to a database
- Creating SQL or MySQL statements
- Executing that SQL or MySQL queries in the database
- Viewing & Modifying the resulting records
JDBC Environment setup
Before connecting your code to the database,make sure that you have done following setup:
1. Core JAVA Installation
2. Any Database Installation
Apart from the above you need to setup a database which you would use for your project.
Creating JDBC Application:
JDBC application creation can be broadly divided into some easy and small steps.
Step 1: Importing required packages
import java.sql.*;
Step 2: Register JDBC driver
It basically initializes a driver so that you can open a communication channel with the database.Following is the code snippet to achieve this:
Class.forName("org.postgresql.Driver"); //I am using Postgresql database.
Step 3: Open a Connection
This step requires a method i.e. DriverManager.getConnection() method to be create a Connection object which represents a physical connection with the database.
con = DriverManager.getConnection(DB_URL,USER,PASS);
Step 4 : Executing a query
This step will help you to build a statement that will build and submit as a SQL statement to the database.
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Students";
ResultSet rs = stmt.executeQuery(sql);
Step 5: Extracting data from result set
This step will help you in fetching data from database.You can use the appropriate Result.getXYZ()
while(rs.next())
{ //Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
//Displaying values
System.out.println("ID:"+ id);
System.out.println(",Age :"+ age);
Step 6: Closing the environment
Its a good habit to close all database resources before program exits.
rs.close();
stmt.close();
con.close();
This post was just a simple introduction about JDBC,in the next post we will discuss about the new and powerful database i.e. PostgreSQL.
0 comments: