Saturday, 9 August 2014

JDBC : JDBC Driver Details


















Click here to watch in Youtube : https://www.youtube.com/watch?v=KbAo5Ii-QLk

Click the below Image to Enlarge
JDBC Driver Details
JDBC Driver Details
JDBC Driver Details
JDBC Driver Details
JDBC Driver Details
JDBC Driver Details
JDBC Driver Details
JDBCDriverDetailsDemo.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDriverDetailsDemo
{
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL      = "jdbc:mysql://localhost:3306/world";

    // Database credentials
    static final String USERNAME    = "root";
    static final String PASSWORD    = "root";

    public static void main( String[] args )
    {

        JDBCDriverDetailsDemo jdbcDriverDetailsDemo = new JDBCDriverDetailsDemo();
        jdbcDriverDetailsDemo.getCityInformation();

    }

    private void getCityInformation()
    {
        Connection connection = null;
        Statement stmt = null;
        try
        {
            /*
             * Register the JDBC driver in DriverManager
             */

            Class.forName(JDBC_DRIVER);

            /*
             * Establish connection to the Database using DriverManager
             */

            connection = DriverManager
                    .getConnection(DB_URL, USERNAME, PASSWORD);

            System.out.println("Connection Object : "
                    + connection.getClass().getName());

            stmt = connection.createStatement();

            System.out.println("Statement Object : "
                    + stmt.getClass().getName());

            String sql = "select ID,Name,CountryCode,District,Population from city";

            /*
             * Execute a query
             */

            ResultSet rs = stmt.executeQuery(sql);

            System.out.println("ResultSet Object : " + rs.getClass().getName());

            rs.close();
            stmt.close();
            connection.close();
        }
        catch( SQLException se )
        {
            /*
             * Handle errors for JDBC
             */
            se.printStackTrace();
        }
        catch( ClassNotFoundException e )
        {
            /*
             * Handle errors for Class.forName
             */
            e.printStackTrace();
        }
        finally
        {
            /*
             * finally block used to close resources
             */
            try
            {
                if( stmt != null )
                {
                    stmt.close();
                }
            }
            catch( SQLException sqlException )
            {
                sqlException.printStackTrace();
            }
            try
            {
                if( connection != null )
                {
                    connection.close();
                }
            }
            catch( SQLException sqlException )
            {
                sqlException.printStackTrace();
            }
        }

    }

}


Output
Connection Object : com.mysql.jdbc.JDBC4Connection
Statement Object : com.mysql.jdbc.StatementImpl
ResultSet Object : com.mysql.jdbc.JDBC4ResultSet

Environment Used 

JDK version : 1.7.0_51
Mysql Server version : 5.6.19 

To Download JDBCDriverDetailsDemoApp Project Click the below link

https://sites.google.com/site/javaee4321/jdbc/JDBCDriverDetailsDemoApp.zip?attredirects=0&d=1

See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge