1 Answer

Java Database Connectivity (JDBC)

Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is Java based data access technology and used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation.

It provides methods to query and update data in a database, and is oriented towards relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the Java virtual machine (JVM) host environment.

Required Steps

The following steps are required to create a new Database using JDBC application −

Import the packages: Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice.

Register the JDBC driver: Requires that you initialize a driver so you can open a communications channel with the database.

Open a connection: Requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with the database server. To create a new database, you need not give any database name while preparing database URL as mentioned in the below example. Execute a query: Requires using an object of type Statement for building and submitting an SQL statement to the database.

Clean up the environment: Requires explicitly closing all database resources versus relying on the JVM's garbage collection.

Sample Code

Copy and past the following example in JDBCExample.java, compile and run as follows –

//STEP 1. Import required packages

import java.sql.;*

public class JDBCExample {

$\hspace{10mm}$ // JDBC driver name and database URL

$\hspace{10mm}$ static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

$\hspace{10mm}$ static final String DB_URL = "jdbc:mysql://localhost/";

$\hspace{10mm}$ // Database credentials

$\hspace{10mm}$ static final String USER = "username";

$\hspace{10mm}$ static final String PASS = "password";

$\hspace{10mm}$ public static void main(String[] args) {

$\hspace{10mm}$ Connection conn = null;

$\hspace{10mm}$Statement stmt = null;

$\hspace{10mm}$ try{

$\hspace{20mm}$ //STEP 2: Register JDBC driver

$\hspace{20mm}$ Class.forName("com.mysql.jdbc.Driver");

$\hspace{20mm}$//STEP 3: Open a connection

$\hspace{20mm}$ System.out.println("Connecting to database...");

$\hspace{20mm}$ conn = DriverManager.getConnection(DB_URL, USER, PASS);

$\hspace{20mm}$ //STEP 4: Execute a query

$\hspace{20mm}$ System.out.println("Creating database...");

$\hspace{20mm}$ stmt = conn.createStatement();

$\hspace{20mm}$String sql = "CREATE DATABASE STUDENTS";


$\hspace{20mm}$System.out.println("Database created successfully...");

$\hspace{10mm}$ }catch(SQLException se){

$\hspace{20mm}$ //Handle errors for JDBC

$\hspace{20mm}$ se.printStackTrace();

$\hspace{10mm}$ }catch(Exception e){

$\hspace{20mm}$ //Handle errors for Class.forName

$\hspace{20mm}$ e.printStackTrace();

$\hspace{10mm}$ }finally{

$\hspace{20mm}$ //finally block used to close resources

$\hspace{20mm}$ try{

$\hspace{30mm}$ if(stmt!=null)

$\hspace{40mm}$ stmt.close();

$\hspace{20mm}$ }catch(SQLException se2){

$\hspace{20mm}$ }// nothing we can do

$\hspace{20mm}$ try{

$\hspace{30mm}$ if(conn!=null)

$\hspace{40mm}$ conn.close();

$\hspace{20mm}$}catch(SQLException se){

$\hspace{30mm}$ se.printStackTrace();

$\hspace{20mm}$ }//end finally try

$\hspace{10mm}$ }//end try

$\hspace{10mm}$ System.out.println("Goodbye!");

}//end main

}//end JDBCExample

Now, let us compile the above example as follows −

C:\>javac JDBCExample.java


When you run JDBCExample, it produces the following result –

C:\>java JDBCExample

Connecting to database...

Creating database...

Database created successfully...



Please log in to add an answer.

Continue reading...

The best way to discover useful content is by searching it.