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 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

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.
        catch (ClassNotFoundException e)
            // 2. Open a connection to the database.
            Connection connection = DriverManager.getConnection(
                    , "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.
                System.out.println("Country: " + rs.getString(1)
                        + " - " + rs.getString(2));
            // 6. Close the connection.
        catch(java.sql.SQLException e)
        catch(Exception e)

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

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.