Monday, 1 September 2014

JDBC Demo - Connect to Oracle Database


















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

Click the below Image to Enlarge
JDBC Demo - Connect to Oracle Database

JDBC Demo - Connect to Oracle Database
JDBC Demo - Connect to Oracle Database

JDBCOracleDemo.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCOracleDemo
{
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
    static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";

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

    public static void main(String[] args)
    {

        JDBCOracleDemo jdbcOracleDemo = new JDBCOracleDemo();
        jdbcOracleDemo.getEmployEEInformation();

    }

    private void getEmployEEInformation()
    {
        Connection connection = null;
        Statement stmt = null;
        try
        {
            /*
             * STEP 1: Register JDBC driver
             */
            Class.forName(JDBC_DRIVER);

            /*
             * STEP 2: Open a connection
             */
            System.out.println("Connecting to database...");
            connection = DriverManager
                    .getConnection(DB_URL, USERNAME, PASSWORD);

            /*
             * STEP 3: Execute a query
             */
            System.out.println("Creating statement...");

            System.out
                    .println("-----------------------------------------------------");

            stmt = connection.createStatement();
            String sql = "select EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER from EMPLOYEES";
            ResultSet rs = stmt.executeQuery(sql);

            /*
             * STEP 4: Extract data from result set
             */
            while (rs.next())
            {
                /*
                 * Retrieve by column name
                 */
                int id = rs.getInt("EMPLOYEE_ID");
                String firstName = rs.getString("FIRST_NAME");
                String lastName = rs.getString("LAST_NAME");
                String email = rs.getString("EMAIL");
                String phoneNumber = rs.getString("PHONE_NUMBER");

                /*
                 * Display values
                 */
                System.out.print("ID: " + id);
                System.out.print(", FirstName: " + firstName);
                System.out.print(", LastName: " + lastName);
                System.out.print(", Email: " + email);
                System.out.println(", PhoneNumber: " + phoneNumber);
            }
            /*
             * STEP 5: close resources
             */
            rs.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
Connecting to database...
Creating statement...
-----------------------------------------------------
ID: 100, FirstName: Steven, LastName: King, Email: SKING, PhoneNumber: 515.123.4567
ID: 101, FirstName: Neena, LastName: Kochhar, Email: NKOCHHAR, PhoneNumber: 515.123.4568
ID: 102, FirstName: Lex, LastName: De Haan, Email: LDEHAAN, PhoneNumber: 515.123.4569
ID: 103, FirstName: Alexander, LastName: Hunold, Email: AHUNOLD, PhoneNumber: 590.423.4567
ID: 104, FirstName: Bruce, LastName: Ernst, Email: BERNST, PhoneNumber: 590.423.4568
ID: 105, FirstName: David, LastName: Austin, Email: DAUSTIN, PhoneNumber: 590.423.4569
ID: 106, FirstName: Valli, LastName: Pataballa, Email: VPATABAL, PhoneNumber: 590.423.4560
ID: 107, FirstName: Diana, LastName: Lorentz, Email: DLORENTZ, PhoneNumber: 590.423.5567
ID: 108, FirstName: Nancy, LastName: Greenberg, Email: NGREENBE, PhoneNumber: 515.124.4569
ID: 109, FirstName: Daniel, LastName: Faviet, Email: DFAVIET, PhoneNumber: 515.124.4169
ID: 110, FirstName: John, LastName: Chen, Email: JCHEN, PhoneNumber: 515.124.4269
ID: 111, FirstName: Ismael, LastName: Sciarra, Email: ISCIARRA, PhoneNumber: 515.124.4369
ID: 112, FirstName: Jose Manuel, LastName: Urman, Email: JMURMAN, PhoneNumber: 515.124.4469
ID: 113, FirstName: Luis, LastName: Popp, Email: LPOPP, PhoneNumber: 515.124.4567

Environment Used 

JDK version : 1.7.0_51
Oracle 11g 

To Download JDBCOracleDemoApp Project Click the below link

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

See also:

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

    Post a Comment

    Google+ Followers

    Google+ Badge