OracleXE JDBC Example


I have Oracle XE 11g installed on my computer with the associated JDeveloper IDE. To develop this example, I used my preferred IDE which is NetBeans 7.0.1.

To begin with, start a new Java Application project.  I have called mine HumanResources as I am using the sample HR schema of OracleXE. My package is com.training. Add the following JAR files to your libraries folder: ojdl.jar, ojdbc6dms.jar, and dms.jar. These files are located at C:\Oracle\Middleware\oracle_common\modules on my computer.

The code below is for the file HumanResources.java.

package oraclexe;

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

public class HumanResources 
{
    public static void main(String[] args) 
    {
        // 1. Load the database driver.
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        }
        catch (ClassNotFoundException e)
        {
            System.err.println(e);
            System.exit(-1);
        }
        try
        {
            // 2. Open a connection to the database.
            Connection connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:XE"
                    , "HR"
                    , "YourPassword"
            );
            // 3. Build the query.
            String query = "SELECT * FROM HR.COUNTRIES";
            // 4. Execute the query.
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(query);
            // 5. Show the query results.
            while(rs.next())
            {
                System.out.println("Country: " + rs.getString(1)
                        + " - " + rs.getString(2));
            }
            // 6. Close the connection.
            connection.close();
        }
        catch(java.sql.SQLException e)
        {
            System.err.println(e);
            System.exit(-1);
        }
        catch(Exception e)
        {
            System.err.println(e);
            System.exit(-1);   
        }
    }
}

Compile and run the project. In the Output -> Debugger Console window, you should see the following output:

run:
Country: AR - Argentina
Country: AU - Australia
Country: BE - Belgium
Country: BR - Brazil
Country: CA - Canada
Country: CH - Switzerland
Country: CN - China
Country: DE - Germany
Country: DK - Denmark
Country: EG - Egypt
Country: FR - France
Country: HK - HongKong
Country: IL - Israel
Country: IN - India
Country: IT - Italy
Country: JP - Japan
Country: KW - Kuwait
Country: MX - Mexico
Country: NG - Nigeria
Country: NL - Netherlands
Country: SG - Singapore
Country: UK - United Kingdom
Country: US - United States of America
Country: ZM - Zambia
Country: ZW - Zimbabwe
BUILD SUCCESSFUL (total time: 4 seconds)

If you encounter problems, make sure you have included the necessary JAR files in your project. Secondly make sure that the database services are running. Thirdly check your connection string. And finally check your database driver class name is spelt correctly with the correct capitalisation.

Advertisements