NetBeans IDE includes built-in support for Oracle Database. You can easily establish a connection from inside the IDE and begin working with the database. This tutorial demonstrates how to use a local installation of Oracle Database 10g Express Edition (Oracle Database XE), a lightweight database that is free to develop, deploy, and distribute.
This document shows how to set up a connection to a local installation of Oracle Database XE from the NetBeans IDE, use the IDE's built-in SQL editor to handle the database data, and how to enable the OCI 8 PHP extension to write PHP code that connects to an Oracle database.
To follow this tutorial, you need the following software and resources.
|Software or Resource||Version Required|
|NetBeans IDE||7.0, 7.1, 7.2, 7.3 Java EE bundle|
|Java Development Kit (JDK)||Version 6 or 7|
|Oracle Database XE||10 g Express Edition|
|Oracle JDBC driver||ojdbc6.jar|
Before you start walking through this tutorial, consider the following:
Note for Windows users: Windows may change the extension of the downloaded file from .jar to .zip. It is still a .jar file, however. You can rename the file to .jar.
Warning for GlassFish Users: The Oracle Database XE homepage, which you use to administer the database, uses port 8080 by default. Oracle GlassFish Application Server also uses port 8080 by default. If you run both programs at the same time, Oracle Database XE blocks browsers from accessing GlassFish at localhost:8080. All applications deployed on GlassFish return 404 in this case. The simple solution is to shut down Oracle Database XE if you do not need it when you are running GlassFish. If you need to run both at the same time, change the default port that Oracle Database XE uses. This is easier than changing the GlassFish default port. There are many sets of instructions on the Internet for changing the Oracle Database XE default port, including one in Oracle forums.
In this exercise you will test and create a new connection to the database.
|Driver Name||Oracle Thin (with Service ID (SID))|
|Host||localhost or 127.0.0.1.
Note: In the case of a remote connection, provide the IP address or resolvable hostname of the machine where the database is installed.
|Service ID (SID)||XE (default SID for Oracle Database XE).
Note: If you are connecting to a remote database, ask the database administrator to provide you with the database SID.
Enter the username.
|Password||Enter the password for the selected username.|
If the attempt is successful, the message "Connection succeeded" is displayed in the wizard.
Note: You need to unlock the HR schema before you can access it in NetBeans. Unlocking the HR database is described in the Oracle Database XE Getting Started tutorial.
The new connection will appear under the Databases node in the Services window. You can expand it and start browsing the database object's structure.
Change the display name for the connection node: choose Properties from the node's popup menu and click the ellipsis button for the Display Name property. Enter OracleDB as the Display Name and click OK.
Note. Although the steps above demonstrate the case of connecting to a local database instance, the steps for connecting to a remote database are the same. The only difference is that instead of specifying localhost as the hostname, enter the IP address or hostname of the remote machine where Oracle Database is installed.
A common way of interacting with databases is running SQL commands in an SQL editor or by using database management interfaces. For example, Oracle Database XE has a browser-based interface through which you can administer the database, manage database objects, and manipulate data.
Although you can perform most of the database-related tasks through the Oracle Database management interface, in this tutorial we demonstrate how you can make use of the SQL Editor in the NetBeans IDE to perform some of these tasks. The following exercises demonstrate how to create a new user, quickly recreate a table, and copy the table data.
Let's create a new database user account to manipulate tables and data in the database. To create a new user, you must be logged in under a database administrator account, in our case, the default system account created during database installation.
create user jim
identified by mypassword
default tablespace users
temporary tablespace temp
quota unlimited on users;
This command creates a new user jim with the password mypassword. The default tablespace is users and the allocated space is unlimited.
In real life, a database administrator creates custom roles and fine tunes privileges for each role. However, for the purpose of our tutorial, we can use a predefined role, such as CONNECT. For more information about roles and privileges, see Oracle Database Security Guide.
grant connect to jim; grant create table to jim; grant select on hr.departments to jim;
A tablespace is a logical database storage unit of any Oracle database. In fact, all of the database's data is stored in tablespaces. You create tables within allocated tablespaces. If a default tablespace is not explicitly assigned to a user, the system tablespace is used by default (it is better to avoid this situation)
For more information about the tablespace concept, see Oracle FAQ: Tablespace
There are several ways to create a table in the database through the NetBeans IDE. For example, you can run an SQL file (right-click the file and choose Run File), execute an SQL Command (right-click the connection node and choose Execute Command) or use the Create Table dialog box (right-click the Tables node and choose Create Table). In this exercise you will recreate a table by using the structure of another table.
In this example, you want the user jim to create a copy of the Departments table in his schema by recreating the table from the hr database. Before you create the table you will need to disconnect from the server and log in as user jim.
When you created the user jim, the Select privilege was limited to the Departments table.
If you want to copy the data from the original Departments table to the new table, you can enter the data manually in the table editor or run an SQL script on the new table to populate the table.
To enter the data manually, perform the following steps.
For example, you can enter the following values taken from the original DEPARTMENTS table.
To populate the table using an SQL script, perform the following steps.
The following script will populate the first row of the new table with the data from the original table.
INSERT INTO JIM.DEPARTMENTS (DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID) VALUES (10, 'Administration', 200, 1700);
You can retrieve the SQL script for populating the table from the original table by performing the following steps.
You can then copy the script and modify it as necessary to insert the data in your table.
See Tips for more information about working in the SQL Editor.
To work with table data, you can make use of the SQL Editor in NetBeans IDE. By running SQL queries, you can add, modify and delete data maintained in database structures.
At first, create the second table named Locations in the jim schema (stay logged under the jim's user account). This time, we will simply run the ready-to-use SQL file in the IDE:
To open the Favorites window, click Window > Favorites in the main menu (press Ctrl-3). The USER_HOME directory is listed in the Favorites window by default.
Note. If more than one database connection is registered with the IDE, the IDE might prompt you to select the correct connection.
You can see that the Locations table with data was added to the JIM schema.
In our case, we will use a simple "natural join", because both tables have the same "location_id" column that holds values of the same data type. This join selects only the rows that have equal values in the matching location_id column.
Open the SQL Command window (right-click the Tables node under the JIM schema and choose Execute Command), enter the following SQL statement, and click the Run SQL icon.
SELECT DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID, STREET_ADDRESS, POSTAL_CODE, CITY, STATE_PROVINCE FROM departments NATURAL JOIN locations ORDER by DEPARTMENT_NAME;
This SQL query returns the rows from the Departments table whose location_id values are equal to the values in the matching column in the Locations table, with the results being ordered by the Department name. Note that you cannot insert new records directly in the results of this query, as you could do in the representation of a single table.
You can save the SQL join query as a View (right-click the View node and choose Create View) and run it conveniently whenever you want. For this, the database user should be granted the privilege to Create View that our sample user does not have. You can log in under the system account, grant jim the Create View privilege (with this SQL statement: "grant create view to jim;") and try creating your own view.
If you were following this tutorial, you already used the capabilities of the NetBeans IDE SQL Editor. Here we list several other capabilities of the NetBeans IDE SQL Editor that might be useful to you.
You can use the OCI 8 PHP extension and the NetBeans IDE for PHP to write PHP code that communicates with an Oracle database. To use NetBeans IDE for PHP and an Oracle database:
Important: If there is no such line in php.ini, look in the extensions folder for the OCI 8 extension file. If there is no OCI 8 extension file in your extensions folder, see Installing PHP and the Oracle Instant Client for Linux and Windows for information about downloading and installing OCI 8.
For more information about enabling OCI 8, and especially for using OCI 8 with a remote Oracle DB server, see Installing PHP and the Oracle Instant Client for Linux and Windows.
When OCI 8 is enabled, NetBeans IDE for PHP accesses this extension for code completion and debugging.
OCI driver packages are available in the same JAR file as the JDBC Thin driver (ojdbc6.jar). The selection of which driver to use depends on the interface: oracle.jdbc.OracleDriver for the Thin driver and oracle.jdbc.driver.OracleDriver for the OCI driver. To use the OCI driver, you must also install the Oracle Database Instant Client, because it contains all the libraries required for the OCI driver to communicate with the database.
To connect to Oracle Database from the NetBeans IDE by using the Oracle's OCI driver:
The troubleshooting tips below describe only a few exceptions that we met. If your question is not answered here, make your own search or use the Send Feedback on This Tutorial link to provide constructive feedback.
Shutting down v3 due to startup exception : No free port within range: >> 8080=com.sun.enterprise.v3.services.impl.monitor.MonitorableSelectorHandler@7dedadThis happens because both the GlassFish application server and Oracle Database use port 8080. So, if you want to use both applications at the same time, you need to change this default port of one of them. To reset the default port of the Oracle Database, you can use this command:
EXEC DBMS_XDB.SETHTTPPORT(<new port number>);
Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor.This happens when the Service ID (SID) of the database instance provided by the connect descriptor is not known to the listener. There are a number of causes for this exception. For example, it might occur if Oracle Database has not been started (simplest case). Or the SID is incorrect or not known to the listener. If you use a default SID (e.g. for Oracle Database Express Edition, the default SID is XE), this problem is unlikely to appear. The SID is included in the CONNECT DATA parts in the tnsnames.ora file (on a Windows machine, the file is at %ORACLE_HOME%\network\admin\tnsnames.ora).
ORA-12705: Cannot access NLS data files or invalid environment specified.
In a general case, this means that the NLS_LANG environment variable contains an invalid value for language, territory, or character set. If this is your case, the invalid NLS_LANG settings should be disabled at your operating system level. For Windows, rename the NLS_LANG subkey in your Windows registry at \HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE. For Linux/Unix, run the command "unset NLS_LANG".
For more information about administering and working with Oracle Database, see the corresponding Oracle's documentation. Below we provide a brief list of most commonly used documentation
For information on how to work with other databases in the NetBeans IDE, see