Advance java JDBC tutorial

JDBC tutorial

JDBC API causes us recorded as a hard copy inexactly coupled code from database drivers. This article clarifies about various sorts of database drivers we have and which one is the most regularly utilized and why. This is a decent post to begin with to find out about Database Connection, Statements and ResultSet. How they all work together to perform explicit database tasks. 


This article additionally tells you the best way to compose JDBC programs so as to keep them approximately coupled from Database drivers, that helps in changing starting with one database server then onto the next effectively with just arrangement changes.

The majority of the occasions we are searching for in excess of a database association. Making an association is an overwhelming procedure and it is anything but a smart thought to let all aspects of the program to make it's own association. This can prompt asset starvation and moderate execution. 

That is the reason we use Connection Pooling in the greater part of the undertaking application. A large portion of the database drivers give DataSource usage classes that can be utilized in the association pool. 


This instructional exercise give case of MySQL and Oracle DataSource and how to utilize them. The article likewise give insights regarding Apache DBCP that fills in as a wrapper around the distinctive DataSource usage to accomplish free coupling.

Most of the servlet containers supports JNDI resources for DataSource that we can use to offload the transaction management and connection pooling tasks to the container. This article explains about different ways through which we can configure DataSource in Apache Tomcat server and use JNDI context lookup to get the DataSource and work with it.

JDBC Connection Example

package com.howtodoinjava.jdbc.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionDemo {
    public static void main(String[] argv) {

        System.out.println("-------- MySQL JDBC Connection Demo Example------------");
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        } 
        catch (ClassNotFoundException e) {
            System.out.println("MySQL JDBC Driver not found !!");
            return;
        }
        System.out.println("MySQL JDBC Registered!");
        Connection connection = null;
        try {
            connection = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password");
            System.out.println("SQL Connection to database is now established!");

        } catch (SQLException e) {
            System.out.println("Connection Failed! ");
            return;
        } finally {
            try
            {
                if(connection != null)
                    connection.close();
                System.out.println("Connection is now closed !!");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Output:

-------- MySQL JDBC Connection Demo Example ------------
MySQL JDBC Registered!
SQL Connection to database is now established!

Connection is now closed !!

Comments

Popular posts from this blog

Java Variables and Datatype

JAVA Features

Java Fundamentals