Connecting to Oracle Database From NetBeans IDE

Embed Size (px)

Citation preview

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    1/46

    Connecting to Oracle Database from NetBeans IDE

    NetBeans IDE includes built-in support for Oracle Database. You can easily establish a connection from inside the IDEand begin working with the database. This tutorial demonstrates how to use a local installation of Oracle Database 10gExpress Edition (Oracle Database XE), a lightweight database that is free to develop, deploy, and distribute.

    This document shows how toset up a connection to a local installationof Oracle Database XE from the NetBeans IDE,use the IDE's built-in SQL editor tohandle the database data,and how toenable the OCI 8 PHP extensionto write PHPcode 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.2, 7.3, 7.4, 8.0, Java EE bundle

    Java Development Kit (JDK) Version 7 or 8

    Oracle Database XE 10 gExpress Edition

    Oracle JDBC driver ojdbc6.jar

    Before You Begin

    Before you start walking through this tutorial, consider the following:

    This tutorial demonstrates how to connect to an Oracle Database XE instance installed on your local system, butthe steps can also be applied when you are connecting to a remote instance. If you are connecting to a localinstance you need todownloadand install Oracle Database XE. The installation process is simple and intuitive,but if you have questions, refer to theOracle Database XE installation guidefor your platform.

    There are two categories of Oracle JDBC drivers: OCI and JDBC Thin.o Oracle's JDBC Thin driver is based on Java and is platform independent. This standalone driver does not

    require the presence of other Oracle libraries and allows a direct connection to an Oracle Database. Thistutorial uses this driver to show how to connect to Oracle Database. Before walking through the tutorial,you need todownloadthe ojdbc6.jar file and save it on your system.

    Note for Windows users:Windows may change the extension of the downloaded file from .jar to .zip. Itis still a .jar file, however. You can rename the file to .jar.

    o Oracle's OCI driver uses Oracle's native client libraries to communicate with databases. These librariesare obtained as part of the Oracle Instant Client. Although the Thin driver is sufficient in most cases, youmight also want to use the OCI driver by following the steps inUsing OCI JDBC Driver with the NetBeansIDE.

    A good example of the OCI driver use is accessing a remote Oracle database from a PHP applicationusing the Oracle Instant Client libraries. See theOCI 8 and the NetBeans IDE for PHPsection in thistutorial for information on how to enable the OCI8 extension for PHP.

    If you have not used Oracle Database XE before, take the Oracle Database XEGetting Started tutorial.

    Warning for GlassFish Users:The Oracle Database XE homepage, which you use to administer the database, usesport 8080 by default. Oracle GlassFish Application Server also uses port 8080 by default. If you run both programs at thesame time, Oracle Database XE blocks browsers from accessing GlassFish at localhost:8080. All applications deployedon GlassFish return 404 in this case. The simple solution is to shut down Oracle Database XE if you do not need it whenyou are running GlassFish. If you need to run both at the same time, change the default port that Oracle Database XEuses. This is easier than changing the GlassFish default port. There are many sets of instructions on the Internet forchanging the Oracle Database XE default port, including one inOracle forums.

    Establishing a Connection to Oracle Database

    https://netbeans.org/kb/docs/ide/oracle-db.html#connecthttps://netbeans.org/kb/docs/ide/oracle-db.html#connecthttps://netbeans.org/kb/docs/ide/oracle-db.html#connecthttps://netbeans.org/kb/docs/ide/oracle-db.html#createuserhttps://netbeans.org/kb/docs/ide/oracle-db.html#createuserhttps://netbeans.org/kb/docs/ide/oracle-db.html#createuserhttps://netbeans.org/kb/docs/ide/oracle-db.html#oci8https://netbeans.org/kb/docs/ide/oracle-db.html#oci8https://netbeans.org/kb/docs/ide/oracle-db.html#oci8https://netbeans.org/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/database/express-edition/overview/index.htmlhttp://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.htmlhttp://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.htmlhttp://download.oracle.com/otn/utilities_drivers/jdbc/11202/ojdbc6.jarhttp://download.oracle.com/otn/utilities_drivers/jdbc/11202/ojdbc6.jarhttp://www.oracle.com/technetwork/database/express-edition/overview/index.htmlhttp://www.oracle.com/technetwork/database/express-edition/overview/index.htmlhttp://www.oracle.com/technetwork/database/express-edition/overview/index.htmlhttp://www.oracle.com/pls/xe102/homepagehttp://www.oracle.com/pls/xe102/homepagehttp://www.oracle.com/pls/xe102/homepagehttp://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.htmlhttp://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.htmlhttp://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.htmlhttps://netbeans.org/kb/docs/ide/oracle-db.html#ocihttps://netbeans.org/kb/docs/ide/oracle-db.html#ocihttps://netbeans.org/kb/docs/ide/oracle-db.html#ocihttps://netbeans.org/kb/docs/ide/oracle-db.html#ocihttps://netbeans.org/kb/docs/ide/oracle-db.html#oci8https://netbeans.org/kb/docs/ide/oracle-db.html#oci8https://netbeans.org/kb/docs/ide/oracle-db.html#oci8http://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htmhttp://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htmhttp://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htmhttps://forums.oracle.com/forums/thread.jspa?threadID=336855https://forums.oracle.com/forums/thread.jspa?threadID=336855https://forums.oracle.com/forums/thread.jspa?threadID=336855https://forums.oracle.com/forums/thread.jspa?threadID=336855http://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htmhttps://netbeans.org/kb/docs/ide/oracle-db.html#oci8https://netbeans.org/kb/docs/ide/oracle-db.html#ocihttps://netbeans.org/kb/docs/ide/oracle-db.html#ocihttp://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.htmlhttp://www.oracle.com/pls/xe102/homepagehttp://www.oracle.com/technetwork/database/express-edition/overview/index.htmlhttp://download.oracle.com/otn/utilities_drivers/jdbc/11202/ojdbc6.jarhttp://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.htmlhttp://www.oracle.com/technetwork/database/express-edition/overview/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttps://netbeans.org/kb/docs/ide/oracle-db.html#oci8https://netbeans.org/kb/docs/ide/oracle-db.html#createuserhttps://netbeans.org/kb/docs/ide/oracle-db.html#connect
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    2/46

    In this exercise you will test and create a new connection to the database.

    1. Start the Oracle database.2. Open the Services window (Window > Services or Ctrl-5;-5 on Mac). In the Services window, right-click the

    Databases node and choose New Connection.

    3. In the New Connection wizard, select Oracle Thin in the Driver dropdown list.4. Click Add and locate the ojdbc6.jar file that you previously downloaded. Click Next.5. In the Customize Connection panel of the wizard, enter the following values and click Next.

    Name Value

    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 orresolvable hostname of the machine where the database is installed.

    Port 1521 (default)

    Service ID (SID)XE (default SID for Oracle Database XE).Note: If you are connecting to a remote database, ask the databaseadministrator to provide you with the database SID.

    Username

    Enter the username.For the purpose of our tutorial, enter system (the default databaseadministrator account) and password that you used during databaseinstallation.

    Password Enter the password for the selected username.

    6. Click Test Connection to confirm that the IDE is able to connect to the database. Click Next.

    If the attempt is successful, the message "Connection succeeded" is displayed in the wizard.

    7. Select HR in the Select Schema dropdown list. Click Finish.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    3/46

    Note:You need to unlock the HR schema before you can access it in NetBeans. Unlocking the HR database isdescribed in the Oracle Database XEGetting Started tutorial.

    The new connection will appear under the Databases node in the Services window. You can expand it and start browsingthe database object's structure.Change the display name for the connection node: choose Properties from the node's popup menu and click the ellipsisbutton 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 forconnecting to a remotedatabase are the same. The only difference is that instead of specifying localhost as thehostname, enter the IP address or hostname of the remote machine where Oracle Database is installed.

    Manipulating Data in Oracle Database

    A common way of interacting with databases is running SQL commands in an SQL editor or by using databasemanagement interfaces. For example, Oracle Database XE has a browser-based interface through which you canadminister 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.

    Creating a User

    Let's create a new database user account to manipulate tables and data in the database. To create a new user, you mustbe logged in under a database administrator account, in our case, the default system account created during databaseinstallation.

    1. In the Services window, right-click the OracleDB connection node and choose Execute Command. This opens theNetBeans IDE's SQL editor, in which you can enter SQL commands that will be sent to the database.

    http://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htmhttp://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htmhttp://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htmhttp://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htm
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    4/46

    2. To create a new user, enter the following command in the SQL Editor window and click the Run SQL button onthe toolbar.

    create user jimidentified by mypassworddefault tablespace userstemporary tablespace tempquota unlimited on users;

    This command creates a new user jim with the password mypassword. The default tablespace is users and theallocated space is unlimited.

    3. The next step is to grant the jim user account privileges to do actions in the database. We need to allow the userto connect to the database, create and modify tables in user's default tablespace, and access the Employees

    table in the sample hr database.

    In real life, a database administrator creates custom roles and fine tunes privileges for each role. However, for thepurpose of our tutorial, we can use a predefined role, such as CONNECT. For more information about roles andprivileges, seeOracle Database Security Guide.

    grant connect to jim;grant create table to jim;grant select on hr.departments to jim;

    Tablespaces in Oracle Databases

    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, thesystem tablespace is used by default (it is better to avoid this situation)

    Creating a Table

    There are several ways to create a table in the database through the NetBeans IDE. For example, you can run an SQLfile (right-click the file and choose Run File), execute an SQL Command (right-click the connection node and chooseExecute Command) or use the Create Table dialog box (right-click the Tables node and choose Create Table). In thisexercise 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 tablefrom the hr database. Before you create the table you will need to disconnect from the server and log in as user jim.

    1. Right-click the OracleDB connection node in the Services window and choose Disconnect.2. Right-click the OracleDB connection node and choose Connect and log in as jim.3. Expand the Tables node under the HR schema and confirm that only the Departments table is accessible to user

    jim.

    When you created the user jim, the Select privilege was limited to the Departments table.

    http://download.oracle.com/docs/cd/E11882_01/network.112/e16543/toc.htmhttp://download.oracle.com/docs/cd/E11882_01/network.112/e16543/toc.htmhttp://download.oracle.com/docs/cd/E11882_01/network.112/e16543/toc.htmhttp://download.oracle.com/docs/cd/E11882_01/network.112/e16543/toc.htm
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    5/46

    4. Right-click the Departments table node and select Grab Structure. Save the .grab file on your disk.5. Expand the JIM schema, right-click the Tables node and choose Recreate Table.

    Point to the .grab file that you created.

    6. Review the SQL script that will be used to create the table. Click OK.

    When you click OK, the new DEPARTMENTS table is created and appears under the JIM schema node. If youright-click the table node and choose View Data you will see that the table is empty.

    If you want to copy the data from the original Departments table to the new table, you can enter the data manually in thetable editor or run an SQL script on the new table to populate the table.

    To enter the data manually, perform the follow ing steps.

    1. Right-click the DEPARTMENTS table under the JIM schema and choose View Data.

    2. Click the Insert Records icon on the View Data toolbar and to open the Insert Record window.

    3. Type in the fields to enter the data. Click OK.

    For example, you can enter the following values taken from the original DEPARTMENTS table.

    Column Value

    DEPARTMENT_ID 10

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    6/46

    DEPARTMENT_NAME Administration

    MANAGER_ID 200

    LOCATION_ID 1700

    To populate the table using an SQL script, perform the following steps.

    1. Right-click the DEPARTMENTS table under the JIM schema and choose Execute Command.2. Enter the script in the SQL Command tab. Click the Run button in the toolbar.

    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.

    1. Right-click the DEPARTMENTS table under the HR schema and choose View Data.2. Select all rows in the View Data window, then right-click in the table and choose Show SQL Script for INSERT

    from the popup menu to open the Show SQL dialog that contains the script.

    You can then copy the script and modify it as necessary to insert the data in your table.

    Working with Table Data

    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:

    1. Download and save thelocations.sqlfile to the USER_HOMEdirectory on your computer.2. Open the Favorites window of the IDE and locate the locations.sql file.

    To open the Favorites window, click Window > Favorites in the main menu (press Ctrl-3). The USER_HOMEdirectory is listed in the Favorites window by default.

    3. Right-click the locations.sql file and choose Run File.

    Note.If more than one database connection is registered with the IDE, the IDE might prompt you to select thecorrect connection.

    4. In the Services window, right-click the Tables node and choose Refresh in the popup menu.

    You can see that the Locations table with data was added to the JIM schema.

    https://netbeans.org/project_downloads/samples/Samples/Java/locations.sqlhttps://netbeans.org/project_downloads/samples/Samples/Java/locations.sqlhttps://netbeans.org/project_downloads/samples/Samples/Java/locations.sqlhttps://netbeans.org/project_downloads/samples/Samples/Java/locations.sql
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    7/46

    5. Right-click the Locations table node and choose View Data to see the table contents. You will see the contents ofthe Locations table.You can insert new records and modify existing data directly in this view window.

    6. Next, we run a query to display information from two tables: Departments and Locations.

    In our case, we will use a simple "natural join", because both tables have the same "location_id" column thatholds values of the same data type. This join selects only the rows that have equal values in the matchinglocation_id column.

    Open the SQL Command window (right-click the Tables node under the JIM schema and choose ExecuteCommand), enter the following SQL statement, and click the Run SQL icon.

    SELECT DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID, STREET_ADDRESS, POSTAL_CODE, CITY,STATE_PROVINCEFROM departments NATURAL JOIN locationsORDER by DEPARTMENT_NAME;

    This SQL query returns the rows from the Departments table whose location_id values are equal to the values inthe matching column in the Locations table, with the results being ordered by the Department name. Note thatyou cannot insert new records directly in the results of this query, as you could do in the representation of a single

    table.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    8/46

    You can save the SQL join query as a View (right-click the View node and choose Create View) and run itconveniently whenever you want. For this, the database user should be granted the privilege to Create View thatour 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.

    Tips for Working in the NetBeans IDE SQL Editor

    If you were following this tutorial, you already used the capabilities of the NetBeans IDE SQL Editor. Here we list severalother capabilities of the NetBeans IDE SQL Editor that might be useful to you.

    1. GUI View o f Database Tables. When you right-click a table node in the Services window and choose View Data,the IDE displays a visual representation of the table and its data (as shown in the figure above). You can alsoadd, modify, and delete table data directly in this view.

    o To add a record, click the Insert Records icon and insert new data in the Insert Records window thatopens. Click the Show SQL button to see the SQL code for this operation. The table will be automaticallyupdated with the new records.

    o To modify a record, double-click directly inside any cell in the GUI View of a table and type the new value.Until the change is committed, the modified text is shown in green. To commit your changes, click the

    Commit Changes icon. To cancel changes, click the Cancel Edits icon.

    o To delete a row, select it and click the Delete Selected Records icon.

    2. Keep Prior Tabs. Click the Keep Prior Tabs icon on the SQL Editor toolbar to keep the windows with the

    results of previous queries open. This can be helpful if you want to compare the results of several queries.3. SQL History(Ctrl-Alt-Shift-H). Use the SQL History icon on the SQL Editor toolbar to view all SQL statements

    that you ran for each of the database connections. Choose the connection from the drop-down list, find the SQLstatement that you need and click Insert to place the statement to the SQL Command window.

    4. Connection list. If you have several database connections and you need to quickly switch between them in theSQL Editor, use the Connections drop-down list.

    5. Run SQL Statements. To run the entire statement that is currently in the SQL Command window, click the Run

    SQL icon. If you want to run only a part of SQL, select it in the SQL Command window, right-click the selectionand choose Run Selection. In this case, only the selected part will be executed.

    OCI 8 and the NetBeans IDE for PHP

    You can use the OCI 8 PHP extension and the NetBeans IDE for PHP to write PHP code that communicates with anOracle database. To use NetBeans IDE for PHP and an Oracle database:

    1. Set up the PHP environment as described in the Configuring Your Environment for PHP Development section ofthePHP Learning Trail.Note that NetBeans IDE supports only PHP 5.2 or 5.3.

    2. Open your php.ini file in an editor. Make certain that the extension_dir property is set to the PHP extensionsdirectory. This directory is usually PHP_HOME/ext. For example, with PHP 5.2.9 installed to the root directory ofC:, the extension_dir setting should be extension_dir="C:\php-5.2.9\ext".

    3. Locate and uncomment the line extension=php_oci8_11g.dll (for Oracle 11g) or extension=php_oci8.dll (forOracle 10.2 or XE). Only one of these extensions can be enabled at one time.

    Important: If there is no such line in php.ini, look in the extensions folder for the OCI 8 extension file. If there is noOCI 8 extension file in your extensions folder, seeInstalling PHP and the Oracle Instant Client for Linux andWindowsfor information about downloading and installing OCI 8.

    4. Restart Apache. (Windows users should restart their computer.)5. Run phpinfo(). If you successfully enabled OCI 8, an OCI 8 section appears in phpinfo() output.

    When OCI 8 is enabled, NetBeans IDE for PHP accesses this extension for code completion and debugging.

    https://netbeans.org/kb/trails/php.htmlhttps://netbeans.org/kb/trails/php.htmlhttps://netbeans.org/kb/trails/php.htmlhttp://www.oracle.com/technetwork/articles/technote-php-instant-084410.htmlhttp://www.oracle.com/technetwork/articles/technote-php-instant-084410.htmlhttp://www.oracle.com/technetwork/articles/technote-php-instant-084410.htmlhttp://www.oracle.com/technetwork/articles/technote-php-instant-084410.htmlhttp://www.oracle.com/technetwork/articles/technote-php-instant-084410.htmlhttp://www.oracle.com/technetwork/articles/technote-php-instant-084410.htmlhttps://netbeans.org/kb/trails/php.html
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    9/46

    Using OCI JDBC Driver wi th the NetBeans IDE

    OCI driver packages are available in the same JAR file as the JDBC Thin driver (ojdbc6.jar). The selection of which driverto use depends on the interface: oracle.jdbc.OracleDriver for the Thin driver and oracle.jdbc.driver.OracleDriver for theOCI driver. To use the OCI driver, you must also install the Oracle Database Instant Client, because it contains all thelibraries 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:

    1. Downloadthe "Basic" package of Oracle Database Instant Client for your platform. Follow the installationinstructions onthis page.

    2. In the IDE's Services window, right-click the Databases node and choose New Connection.3. In the Locate Driver step, choose Oracle OCI, click Add and specify the ojdbc6.jar file.4. In the Customize Connection dialog box, provide the connection details: IP address, port, SID, username and

    password.Notice the difference in the JDBC URL for the OCI and Thin drivers.

    Troubleshooting

    The troubleshooting tips below describe only a few exceptions that we met. If your question is not answered here, makeyour own search or use the Send Feedback on This Tutorial link to provide constructive feedback.

    You see the error similar to the following: Shutting down v3 due to startup exception : No free port within range:

    >> 8080=com.sun.enterprise.v3.services.impl.monitor.MonitorableSelectorHandler@7dedad

    This happens because both the GlassFish application server and Oracle Database use port 8080. So, if you wantto use both applications at the same time, you need to change this default port of one of them. To reset thedefault port of the Oracle Database, you can use this command:

    CONNECT SYSTEM/passwordEXEC DBMS_XDB.SETHTTPPORT();

    http://www.oracle.com/technetwork/database/features/instant-client/index-100365.htmlhttp://www.oracle.com/technetwork/database/features/instant-client/index-100365.htmlhttp://www.oracle.com/technetwork/database/features/instant-client/index-100365.htmlhttp://www.oracle.com/technetwork/database/features/instant-client/index-100365.htmlhttp://www.oracle.com/technetwork/database/features/instant-client/index-100365.htmlhttp://www.oracle.com/technetwork/database/features/instant-client/index-100365.htmlhttp://www.oracle.com/technetwork/database/features/instant-client/index-100365.html
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    10/46

    You receive the following error:

    Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SIDgiven in connect descriptor.

    This happens when the Service ID (SID) of the database instance provided by the connect descriptor is notknown to the listener. There are a number of causes for this exception. For example, it might occur if OracleDatabase has not been started (simplest case). Or the SID is incorrect or not known to the listener. If you use adefault 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).

    You receive the following error:

    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"

    Working with the Java DB (Derby) Database

    This document demonstrates how to set up a connection toJava DBdatabase in NetBeans IDE. Once a connection ismade, you can begin working with the database in the IDE, allowing you to create tables, populate them with data, runSQL statements and queries, and more.

    The Java DB database is Sun's supported distribution ofApache Derby.Java DB is a fully transactional, secure,standards-based database server, written entirely in Java, and fully supports SQL, JDBC API, and Java EE technology.The Java DB database is packaged with theGlassFishapplication server, and is included inJDK 6as well. For moreinformation on Java DB database, consult theofficial documentation.

    To follow this tutorial, you need the following software and resources.

    Software or Resource Version Required

    NetBeans IDE 7.2, 7.3, 7.4, 8.0, Java EE

    Java Development Kit (JDK) Version 7 or 8

    Java DB version 10.4.x, 10.5.x

    Note.

    Java DB is installed when you install JDK 7 or JDK 8 (except on Mac OS X). If you are using Mac OS X you can

    download and installJava DBmanually or use the Java DB that is installed by Java EE version of the NetBeansIDE installer.

    Configuri ng the Database

    If you have the GlassFish Server registered in your NetBeans IDE installation, Java DB will already be registered for you.Therefore, you can skip ahead toStarting the Server and Creating a Database.

    If you downloaded the GlassFish server separately and need help registering it in NetBeans IDE, see Registering aGlassFish Server Instancein the IDE's Help Contents (F1).

    If you just downloaded Java DB on its own, perform the following steps.

    http://www.oracle.com/technetwork/java/javadb/overview/index.htmlhttp://www.oracle.com/technetwork/java/javadb/overview/index.htmlhttp://www.oracle.com/technetwork/java/javadb/overview/index.htmlhttp://db.apache.org/derby/http://db.apache.org/derby/http://db.apache.org/derby/http://glassfish.dev.java.net/http://glassfish.dev.java.net/http://glassfish.dev.java.net/http://download.oracle.com/javase/6/http://download.oracle.com/javase/6/http://download.oracle.com/javase/6/http://www.oracle.com/technetwork/java/javadb/documentation/index.htmlhttp://www.oracle.com/technetwork/java/javadb/documentation/index.htmlhttp://www.oracle.com/technetwork/java/javadb/documentation/index.htmlhttps://netbeans.org/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javadb/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javadb/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javadb/downloads/index.htmlhttps://netbeans.org/kb/docs/ide/java-db.html#startinghttps://netbeans.org/kb/docs/ide/java-db.html#startinghttps://netbeans.org/kb/docs/ide/java-db.html#startinghttps://netbeans.org/kb/docs/ide/java-db.html#startinghttp://www.oracle.com/technetwork/java/javadb/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javadb/documentation/index.htmlhttp://download.oracle.com/javase/6/http://glassfish.dev.java.net/http://db.apache.org/derby/http://www.oracle.com/technetwork/java/javadb/overview/index.html
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    11/46

    1. Run the self-extracting file. A folder named 'javadb' will be created in the same location as the file. If you justdownloaded Java DB and want to have the database server reside in a different location than where it wasextracted to, you should relocate it now.

    2. On your system, create a new directory to be used as a home directory for the individual instances of thedatabase server. For example, you can create this folder in the Java DB root directory (javadb) or in any otherlocation.

    Before continuing further, it is important to understand the components found in Java DB's root directory:

    The demo subdirectory contains the demonstration programs. The bin subdirectory contains the scripts for executing utilities and setting up the environment. The javadoc subdirectory contains the API documentation that was generated from source code comments. The docs subdirectory contains the Java DB documentation. The lib subdirectory contains the Java DB jar files.

    Registering the Database in NetBeans IDE

    Now that the database is configured, perform the following steps to register Java DB in the IDE.

    1. In the Services window, right-click the Java DB Database node and choose Properties to open the Java DBSettings dialog box.

    2. For the Java DB Installation text field, enter the path to the Java DB root directory (javadb) that you specified inthe previous step.

    3. For Database Location, use the default location if a location is already provided. Click OK

    For example, the default location might look like C:\Documents and Settings\username\.netbeans-derby on aWindows machine.

    Note.If the Database Location field is empty you will need to set the path to the directory that contains yourdatabases. You will need to create a directory for the databases if no directory exists.

    Starting the Server and Creating a Database

    The Java DB Database menu options are displayed when you right-click the Java DB node in the Services window. Thiscontextual menu items allow you to start and stop the database server, create a new database instance, as well asregister database servers in the IDE (as demonstrated in the previous step). To start the database server:

    1. In the Services window, right-click the Java DB node and choose Start Server. Note the following output in theOutput window, indicating that the server has started:

    2. Right-click the Java DB node and choose Create Database to open the Create Java DB Database dialog.3. Type contact for the Database Name.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    12/46

    4. Type nbuser for the User Name and Password. Click OK.

    Note.The Database Location is the default location set during installation of Java DB from GlassFish. If youinstalled Java DB separately, this location might be different.

    After you create the database, if you expand the Databases node in the Services window you can see that the IDEcreated a database connection and that the database was added to the list under the Java DB node.

    Connecting to the Database

    So far, you have successfully started the the database server and created a database instance named contact in the IDE.In the Services window of the IDE you can perform the following common tasks on database structures.

    creating, deleting, modifying tables populating tables with data viewing tabular data executing SQL statements and queries

    In order to begin working with the contact database, you need to create a connection to it. To connect to the contactdatabase perform the following steps.

    1. Expand the Databases node in the Services window and locate the new database and the database connectionnodes.

    The database connection node( ) is displayed under the Databases node. The name of the database isdisplayed under the Java DB node.

    Note.You will also see the sample [app on APP] database connection that is the default database schema.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    13/46

    2. Right-click the contact database connection node (jdbc:derby://localhost:1527/contact [nbuser on NBUSER]) andchoose Connect.

    The connection node icon appears whole ( ), signifying that the connection was successful.

    3. Create a convenient display name for the database by right-clicking the database connection node(jdbc:derby://localhost:1527/contact [nbuser on NBUSER]) and choosing Rename. Type Contact DB in the textfield and click OK.

    Creating Tables

    The contact database that you just created is currently empty. It does not yet contain any tables or data. In NetBeans IDEyou can add a database table by either using the Create Table dialog, or by inputting an SQL statement and running itdirectly from the SQL Editor. You can explore both methods:

    Using the Create Table Dialog Using the SQL Editor

    Using the Create Table Dialog

    1. Expand the Contact DB connection node and note that there are several schema subnodes. The app schema is

    the only schema that applies to this tutorial. Right-click the APP node and choose Set as Default Schema.2. Expand the APP node and note that there are three subfolders: Tables, Views and Procedures. Right-click the

    Tables node and choose Create Table to open the Create Table dialog box.3. In the Table Name text field, type FRIENDS.4. Click Add Column. The Add Column dialog box appears.5. For Column Name, enter id. For Data Type, select INTEGER from the drop-down list.6. Under Constraints, select the Primary Key checkbox to specify that this column is the primary key for your table.

    All tables found in relational databases must contain a primary key. Note that when you select the Primary Keycheck box, the Index and Unique check boxes are also automatically selected and the Null check box isdeselected. This is because primary keys are used to identify a unique row in the database, and by default areused as the table index. Because all rows must be identified, primary keys cannot contain a Null value.

    7. Repeat this procedure now by specifying fields as shown in the table below:

    Key Index Null Unique Column name Data type Size

    [checked] [checked] [checked] id INTEGER 0

    [checked] firstName VARCHAR 20

    [checked] lastName VARCHAR 20

    [checked] nickName VARCHAR 30

    [checked] friendSince DATE 0

    [checked] email VARCHAR 60

    8. You are creating a table named FRIENDS that holds the following data for each contact record:

    https://netbeans.org/kb/docs/ide/java-db.html#createTablehttps://netbeans.org/kb/docs/ide/java-db.html#createTablehttps://netbeans.org/kb/docs/ide/java-db.html#sqlEditorhttps://netbeans.org/kb/docs/ide/java-db.html#sqlEditorhttps://netbeans.org/kb/docs/ide/java-db.html#sqlEditorhttps://netbeans.org/kb/docs/ide/java-db.html#createTable
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    14/46

    o First Nameo Last Nameo Nick Nameo Friend Since Dateo Email Address

    9. When you are sure that your Create Table dialog contains the same specifications as those shown above, click

    OK. The IDE generates the FRIENDS table in the database, and you can see a new FRIENDS table node ( )display under the Tables node. Beneath the table node the columns (fields) are listed, starting with the primary

    key ( ).

    Using the SQL Editor:

    1. In the Service window, either right-click the Contact DB connection node or the Tables node beneath it andchoose Execute Command. A blank canvas opens in the SQL Editor in the main window.

    2. Enter the following query in the SQL Editor. This is a table definition for the COLLEAGUES table you are about to

    create:3. CREATE TABLE "COLLEAGUES" (4. "ID" INTEGER not null primary key,5. "FIRSTNAME" VARCHAR(30),6. "LASTNAME" VARCHAR(30),7. "TITLE" VARCHAR(10),8. "DEPARTMENT" VARCHAR(20),9. "EMAIL" VARCHAR(60)

    );

    Note: Statements and queries formed in the SQL Editor are parsed in Structured Query Language. SQL adheresto strict syntax rules which you should be familiar with when working in the IDE's editor. SQL syntax can also

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    15/46

    differ depending on the database management system. See theJavaDB Reference Manualfor specificguidelines.

    10. Click the Run SQL ( ) button in the task bar at the top of the editor (Ctrl-Shift-E) to execute the query. In theOutput window (Ctrl-4), a message displays indicating that the statement was successfully executed.

    11. To verify changes, right-click the Contact DB connection node in the Services window and choose Refresh. Thisupdates the Runtime UI component to the current status of the specified database. This step is necessary when

    running queries from the SQL Editor in NetBeans IDE. Note that the new COLLEAGUES table node ( ) nowdisplays under Tables in the Services window.

    Adding Table Data

    Now that you have created one or more tables in the contact database, you can start populating it with data. There areseveral ways that you can add records to your table.

    Write an SQL statementin the SQL Editor that supplies a value for every field present in the table schema. Use the SQL Editorto add records to the table. Use an external SQL scriptto import records to the table.

    Read the sections below to learn how to use all these methods of populating the FRIENDS table with data.

    Running an SQL Statement

    1. Expand the Tables under the Contact DB node in the Services window, right-click the FRIENDS table and chooseExecute Command to open the SQL Editor window.

    2. In the SQL Editor, enter the following statement.

    INSERT INTO APP.FRIENDS VALUES (1,'Theodore','Bagwell','T-Bag','2004-12-25','[email protected]')

    While you are typing, you can use the SQL Editor code completion.

    3. Right-click inside the SQL Editor and choose Run Statement. The Output window displays a message indicatingthat the statement was successfully executed.

    4. To verify that the new record has been added to the FRIENDS table, right-click the FRIENDS table node in theServices window and choose View Data.

    When you choose View Data, a query to select all the data from the table is automatically generated in the upperpane of the SQL Editor. The results of the statement are displayed in the lower pane of the SQL Editor. In thiscase, the FRIENDS table displays in the lower pane. Note that a new row has been added with the data you just

    supplied from the SQL statement.

    Using the SQL Edito r

    http://www.oracle.com/technetwork/java/javadb/documentation/index.htmlhttp://www.oracle.com/technetwork/java/javadb/documentation/index.htmlhttp://www.oracle.com/technetwork/java/javadb/documentation/index.htmlhttps://netbeans.org/kb/docs/ide/java-db.html#sqlstatementhttps://netbeans.org/kb/docs/ide/java-db.html#sqlstatementhttps://netbeans.org/kb/docs/ide/java-db.html#usesqleditorhttps://netbeans.org/kb/docs/ide/java-db.html#usesqleditorhttps://netbeans.org/kb/docs/ide/java-db.html#usinghttps://netbeans.org/kb/docs/ide/java-db.html#usinghttps://netbeans.org/kb/docs/ide/java-db.html#usinghttps://netbeans.org/kb/docs/ide/java-db.html#usesqleditorhttps://netbeans.org/kb/docs/ide/java-db.html#sqlstatementhttp://www.oracle.com/technetwork/java/javadb/documentation/index.html
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    16/46

    1. Right-click the FRIENDS table node and choose View Data (if you have not done this at the last step of theprevious section).

    2. Click the Insert Record(s) (Alt-I) button to add a row.The Insert Records dialog box appears.

    3. Click in each cell and enter records. Note that for the cells with Date data type, you can choose a date from thecalendar. Click OK when you are done.

    In the SQL Editor, you can sort the results by clicking on a row header, modify and delete existing records, andsee the SQL script for the actions you are doing in the editor (the Show SQL Script command from the pop-upmenu).

    Deleting Tables

    In the following step, you use an external SQL script to create a new COLLEAGUES table. However, you just created aCOLLEAGUES table in theUsing the SQL Editorsection above. In order to make it clear that the SQL script indeedcreates a new table, you can delete the already created COLLEAGUES table now. To delete a database table perform thefollowing steps.

    1. Expand the Tables node under the database connection node in the Services window.2. Right-click the table that you want to delete and choose Delete.

    Using an External SQL Script

    Issuing commands from an external SQL script is a popular way to manage your database. You may have already

    created an SQL script elsewhere, and want to import it into NetBeans IDE to run it on a specified database.

    In this exercise the script will create a new table named COLLEAGUES and populate it with data. Perform the followingsteps to run the script on the contact database.

    1. Downloadcolleagues.sqlto your local system2. Choose File > Open File from the IDE's main menu. In the file browser navigate to the location of the saved

    colleagues.sql file and click Open. The script automatically opens in the SQL Editor.

    Alternatively, you can copy the contents ofcolleagues.sqland then open the SQL editor and paste the contents ofthe file into the SQL editor.

    3. Make sure your connection to Contact DB is selected from the Connection drop-down box in the tool bar at thetop of the editor.

    https://netbeans.org/kb/docs/ide/java-db.html#sqlEditorhttps://netbeans.org/kb/docs/ide/java-db.html#sqlEditorhttps://netbeans.org/kb/docs/ide/java-db.html#sqlEditorhttps://netbeans.org/project_downloads/usersguide/colleagues.sqlhttps://netbeans.org/project_downloads/usersguide/colleagues.sqlhttps://netbeans.org/project_downloads/usersguide/colleagues.sqlhttps://netbeans.org/project_downloads/usersguide/colleagues.sqlhttps://netbeans.org/project_downloads/usersguide/colleagues.sqlhttps://netbeans.org/project_downloads/usersguide/colleagues.sqlhttps://netbeans.org/project_downloads/usersguide/colleagues.sqlhttps://netbeans.org/project_downloads/usersguide/colleagues.sqlhttps://netbeans.org/kb/docs/ide/java-db.html#sqlEditor
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    17/46

    4. Click the Run SQL ( ) button in the SQL Editor's task bar. The script is executed against the selected database,and any feedback is generated in the Output window.

    5. To verify changes, right-click the Contact DB connection node in the Services window and choose Refresh. Notethat the new COLLEAGUES table from the SQL script now displays as a table node under contact in the Serviceswindow.

    6. To view the data contained in the new tables, right-click the COLLEAGUES table and choose View Data. In thismanner, you can also compare the tabular data with the data contained in the SQL script to see that they match.

    Recreating Tables from a Different Database

    If you have a table from another database which you would like to recreate in the database you are working in fromNetBeans IDE, the IDE offers a handy tool for this. You first need to have the second database registered in the IDE,similar to what was described at the beginning of this tutorial. For the purposes of this tutorial, use the sample databasethat comes packaged with Java DB. This process is essentially carried out in two parts: You first 'grab' the table definitionof the selected table, then you can recreate the table in your chosen database:

    1. Connect to the sample database by right-clicking the connection node under the Databases node in the Serviceswindow and choosing Connect (username and password is app).

    2. Expand the Tables node under the sample database connection, right-click the CUSTOMER table node andchoose Grab Structure.

    3. In the Grab Table dialog that opens, specify a location on your computer to save the grab file that will be created.Click Save.

    The grab file records the table definition of the selected table.

    4. Expand the APP schema node under the Contact DB database connection, right-click the Tables node andchoose Recreate Table to open the Recreate Table dialog box.

    5. In the Recreate Table dialog box, navigate to the location where you saved the CUSTOMER grab file and clickOpen to open the Name the Table dialog box.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    18/46

    6. At this point you can change the table name or edit the table definition. Otherwise, click OK to immediately createthe table in the contact database. A new CUSTOMER table node appears beneath the Contact DB connectionnode.

    If you view the data in the new CUSTOMER table you will see that there are no records in the database, but that thestructure of the table is identical to the table that you grabbed

    Connecting to a MySQL Database

    This document demonstrates how to set up a connection to a MySQL database from the NetBeans IDE. Once connected,you can begin working with MySQL in the IDE's Database Explorer by creating new databases and tables, populatingtables with data, and running SQL queries on database structures and content. This tutorial is designed for beginners witha basic understanding of database management, who want to apply their knowledge to working with MySQL in NetBeansIDE.

    MySQLis a popular Open Source relational database management system (RDBMS) commonly used in web applicationsdue to its speed, flexibility and reliability. MySQL employs SQL, or Structured Query Language, for accessing andprocessing data contained in databases.

    To follow this tutorial, you need the following software and resources.

    Software or Resource Version Required

    NetBeans IDE 7.2, 7.3, 7.4, 8.0, Java

    http://www.mysql.com/http://www.mysql.com/https://netbeans.org/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttp://www.mysql.com/
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    19/46

    Java Development Kit (JDK) Version 7 or 8

    MySQL database server version 5.x

    Note:This tutorial assumes that you already have the MySQL RDBMS installed and configured on your computer. If youare installing for the first time, please refer to the officialMySQL documentationfor help. You can also refer toSetting Upthe MySQL Database Server in the Windows Operating System.

    Configuri ng MySQL Server Properties

    NetBeans IDE comes bundled with support for the MySQL RDBMS. Before you can access the MySQL Database Serverin NetBeans IDE, you must configure the MySQL Server properties.

    1. Right-click the Databases node in the Services window and choose Register MySQL Server to open the MySQLServer Properties dialog box.

    2. Confirm that the server host name and port are correct.

    Notice that the IDE enters localhost as the default server host name and 3306 as the default server port number.

    3. Enter the Administrator user name (if not displayed).

    Note: You need administrative access to be able to create and remove databases.

    4. Enter the Administrator password. The default is set to blank.

    Note:A blank password can also be a password.

    5. Click the Admin Properties tab at the top of the dialog box.

    The Admin Properties tab is then displayed, allowing you to enter information for controlling the MySQL Server.

    6. In the Path/URL to admin tool field, type or browse to the location of your MySQL Administration application suchas the MySQL Admin Tool, PhpMyAdmin, or other web-based administration tools.

    Note: mysqladmin is the MySQL admin tool found in the bin folder of the MySQL installation directory. It is acommand-line tool and not ideal for use with the IDE.

    Type any arguments for the admin tool in the Arguments field.

    7. In the Path to start command, type or browse to the location of the MySQL start command. To find the startcommand, look for mysqld in the bin folder of the MySQL installation directory.

    Note: The recommended binary for Unix and NetWare is mysql_safe. The start command may also vary ifMySQL was installed as part of an AMP installation.

    Type any arguments for the start command in the Arguments field.

    http://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://dev.mysql.com/downloads/mysql/http://dev.mysql.com/downloads/mysql/http://dev.mysql.com/doc/refman/5.0/en/installing-cs.htmlhttp://dev.mysql.com/doc/refman/5.0/en/installing-cs.htmlhttp://dev.mysql.com/doc/refman/5.0/en/installing-cs.htmlhttps://netbeans.org/kb/docs/ide/install-and-configure-mysql-server.htmlhttps://netbeans.org/kb/docs/ide/install-and-configure-mysql-server.htmlhttps://netbeans.org/kb/docs/ide/install-and-configure-mysql-server.htmlhttps://netbeans.org/kb/docs/ide/install-and-configure-mysql-server.htmlhttps://netbeans.org/kb/docs/ide/install-and-configure-mysql-server.htmlhttps://netbeans.org/kb/docs/ide/install-and-configure-mysql-server.htmlhttp://dev.mysql.com/doc/refman/5.0/en/installing-cs.htmlhttp://dev.mysql.com/downloads/mysql/http://www.oracle.com/technetwork/java/javase/downloads/index.html
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    20/46

    8. In the Path to stop command field, type or browse to the location of the MySQL stop command. This is usually thepath to mysqladmin in the bin folder of the MySQL installation directory. If the command is mysqladmin, in the

    Arguments field, type -u root stop to grant root permissions for stopping the server.9. When finished, the Admin Properties tab should resemble the following figure. If you are satified with your

    configuration, click OK.

    Starting the MySQL Server

    Before you can connect to a MySQL Database Server, you must first ensure that the MySQL Database Server is runningon your machine. If the database server is not connected you will see (disconnected)next to the user name in theMySQL Server node in the Service window and you will not be able to expand the node.

    To connect to the database server, confirm that the MySQL Database Server is running on your machine, right-click theDatabases > MySQL Server node in the Services window and choose Connect. You might be prompted to supply apassword to connect to the server.

    When the server is connected you will be able to expand the MySQL Server node and view the all available MySQLdatabases.

    Creating and Connecting to the Database Instance

    A common way of interacting with databases is through an SQL editor. NetBeans IDE has a built-in SQL Editor for thispurpose. The SQL Editor is generally accessible via the Execute Command option from the right-click menu of theconnection node (or of the connection node's child nodes). Now that you are connected to the MySQL server, you cancreate a new database instance using the SQL Editor. For purposes of this tutorial, create an instance calledMyNewDatabase:

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    21/46

    1. In the IDE's Services window, right-click the MySQL Server node and choose Create Database.

    The Create MySQL Database dialog box opens.

    2. In the Create MySQL Database dialog box, type the name of the new database. We will use MyNewDatabase forthis tutorial. Leave the checkbox unselected at this time.

    Note: You can also grant full access to a given user. By default, only the admin user has the permissions toperform certain commands. The drop down list lets you assign these permissions to a specified user.

    3. Click OK.

    The new database appears under the MySQL Server node in the Services window.

    4. Right-click the new database node and choose Connect in the popup menu to open the connection to thedatabase.

    Database connections that are open are represented by a complete connection node ( ) in the Serviceswindow.

    Creating Database Tables

    Now that you have connected to MyNewDatabase, you can begin exploring how to create tables, populate them with dataand modify data maintained in tables. This allows you to take a closer look at the functionality offered by the DatabaseExplorer, as well as NetBeans IDE's support for SQL files.

    MyNewDatabase is currently empty. In the IDE it is possible to add a database table by either using the Create Tabledialog, or by inputting an SQL query and running it directly from the SQL Editor. In the following exercises you will use theSQL editor to create the Counselor table and the Create Table dialog box to create the Subject table. After you create thetables you will run an SQL script to populate the tables.

    1. Using the SQL Editor2. Using the Create Table Dialog

    Using the SQL Editor

    In this exercise you will use the SQL editor to create the Counselor table.

    1. In the Database Explorer, expand the MyNewDatabase connection node ( ) and note that there are threesubfolders: Tables, Views and Procedures.

    2. Right-click the Tables folder and choose Execute Command. A blank canvas opens in the SQL Editor in the mainwindow.

    3. In the SQL Editor, type in the following query. This is a table definition for the Counselor table you are about tocreate.

    4. CREATE TABLE Counselor (5. id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,6. firstName VARCHAR (50),7. nickName VARCHAR (50),8. lastName VARCHAR (50),9. telephone VARCHAR (25),10. email VARCHAR (50),

    https://netbeans.org/kb/docs/ide/mysql.html#usingSQLEditorhttps://netbeans.org/kb/docs/ide/mysql.html#usingSQLEditorhttps://netbeans.org/kb/docs/ide/mysql.html#usingCreateTablehttps://netbeans.org/kb/docs/ide/mysql.html#usingCreateTablehttps://netbeans.org/kb/docs/ide/mysql.html#usingCreateTablehttps://netbeans.org/kb/docs/ide/mysql.html#usingSQLEditor
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    22/46

    11. memberSince DATE DEFAULT '0000-00-00',12. PRIMARY KEY (id)

    );

    Note: Queries formed in the SQL Editor are parsed in Structured Query Language (SQL). SQL adheres to strict

    syntax rules which you should be familiar with when working in the IDE's Editor. Upon running a query, feedbackfrom the SQL engine is generated in the Output window indicating whether execution was successful or not.

    13. To execute the query, either click the Run SQL ( ) button in the task bar at the top (Ctrl-Shift-E), or right-click

    within the SQL Editor and choose Run Statement. The IDE generates the Counselor table in the database, andyou receive a message similar to the following in the Output window.

    14. To verify changes, right-click the Tables node in the Database Explorer and choose Refresh. The Refresh optionupdates the Database Explorer's UI component to the current status of the specified database. Note that the new

    Counselor table node ( ) now displays under Tables in the Database explorer. If you expand the table node you

    can see the columns (fields) you created, starting with the primary key ( ).

    Using the Create Table Dialog

    In this exercise you will use the Create Table dialog box to create the Subject table.

    1. In the Database Explorer, right-click the Tables node and choose Create Table. The Create Table dialog opens.2. In the Table name text field, type Subject.3. Click Add Column.4. For the Name of the column, enter id. Choose SMALLINT for data type from the Type drop-down list. Click OK.

    5. Select the Primary Key check box in the Add Column dialog box. You are specifying the primary key for yourtable. All tables found in relational databases must contain a primary key. Note that when you select the Keycheck box, the Index and Unique check boxes are also automatically selected and the Null check box isdeselected. This is because primary keys are used to identify a unique row in the database, and by default formthe table index. Because all rows need to be identified, primary keys cannot contain a Null value.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    23/46

    6. Repeat this procedure by adding the remaining columns, as shown in the following table.

    Key Index Null Unique Column Name Data Type Size

    [checked] [checked] [checked] id SMALLINT 0

    [checked] name VARCHAR 50

    [checked] description VARCHAR 500

    [checked] FK_counselorID SMALLINT 0

    7. You are creating a table named Subject that will hold data for each of the following records.o Name: name of the subjecto Description: description of the subjecto Counselor ID: counselor ID that corresponds to an ID from the Counselor table

    Make sure that the fields in your Create Table dialog match those shown above, then click OK. The IDE

    generates the Subject table in the database, and you can see a new Subject table node ( ) immediately displayunder Tables in the Database Explorer.

    Working w ith Table Data

    In order to work with table data, you can make use of the SQL Editor in NetBeans IDE. By running SQL queries on adatabase, you can add, modify and delete data maintained in database structures. To add a new record (row) to theCounselor table, do the following:

    1. Choose Execute Command from the Tables folder in the Database Explorer. A blank canvas opens in the SQLEditor in the main window.

    2. In the SQL Editor, type in the following query.3. INSERT INTO Counselor

    VALUES (1, 'Ricky', '"The Dragon"', 'Steamboat','334 612-5678', '[email protected]', '1996-01-01')

    4. To execute the query, right-click within the SQL Editor and choose Run Statement. In the Output window, you cansee a message indicating that the query was successfully executed.5. To verify that the new record has been added to the Counselor table, in the Database Explorer, right-click the

    Counselor table node and choose View Data. A new SQL Editor pane opens in the main window. When youchoose View Data, a query to select all the data from the table is automatically generated in the upper region ofthe SQL Editor. The results of the statement are displayed in a table view in the lower region. In this example, theCounselor table displays. Note that a new row has been added with the data you just supplied from the SQLquery.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    24/46

    Running an SQL Script

    Another way to manage table data in NetBeans IDE is by running an external SQL script directly in the IDE. If you havecreated an SQL script elsewhere, you can simply open it in NetBeans IDE and run it in the SQL Editor.

    For demonstrative purposes, downloadifpwafcad.sqland save it to a location on your computer. This script creates twotables similar to what you just created above (Counselor and Subject), and immediately populates them with data.

    Because the script overwrites these tables if they already exist, delete the Counselor and Subject tables now so itbecomes obvious that new tables are being created when the script is run. To delete tables:

    1. Right-click the Counselor and Subject table nodes in the Database Explorer and choose Delete.2. Click Yes in the Confirm Object Deletion dialog box. Note that the dialog box lists the tables that will be deleted.

    When you click Yes in the Confirm Object Deletion dialog box, the table nodes are automatically removed from theDatabase Explorer.

    To run the SQL script on MyNewDatabase:

    1. Choose File > Open File from the IDE's main menu. In the file browser navigate to the location where youpreviously saved ifpwafcad.sql and click Open. The script automatically opens in the SQL Editor.

    2. Make sure your connection to MyNewDatabase is selected from the Connection drop-down box in the toolbar atthe top of the Editor.

    3. Click the Run SQL ( ) button in the SQL Editor's task bar. The script is executed against the selected database,and any feedback is generated in the Output window.

    4. To verify changes, right-click the MyNewDatabase connection node in the Runtime window and choose Refresh.The Refresh option updates the Database Explorer's UI component to the current status of the specifieddatabase. Note that the two new tables from the SQL script now display as a table nodes under MyNewDatabasein the Database Explorer.

    5. Choose View Data from the right-click menu of a selected table node to see the data contained in the new tables.In this manner, you can compare the tabular data with the data contained in the SQL script to see that they match

    Creating a Simple Web Application Using a MySQL Database

    This document describes how to create a simple web application that connects to a MySQL database server. It alsocovers some basic ideas and technologies in web development, such asJavaServer Pages(JSP),JavaServer PagesStandard Tag Library(JSTL), theJava Database Connectivity(JDBC) API, and two-tier, client-server architecture. Thistutorial is designed for beginners who have a basic understanding of web development and are looking to apply theirknowledge using a MySQL database.

    MySQLis a popular open source database management system commonly used in web applications due to its speed,flexibility and reliability. MySQL employs SQL, or Structured Query Language, for accessing and processing datacontained in databases.

    This tutorial is a continuation from theConnecting to a MySQL Databasetutorial and assumes that you have alreadycreated a MySQL database named MyNewDatabase, which you have registered a connection for in the NetBeans IDE.

    https://netbeans.org/project_downloads/samples/Samples/Java%20Web/ifpwafcad.sqlhttps://netbeans.org/project_downloads/samples/Samples/Java%20Web/ifpwafcad.sqlhttps://netbeans.org/project_downloads/samples/Samples/Java%20Web/ifpwafcad.sqlhttp://www.oracle.com/technetwork/java/overview-138580.htmlhttp://www.oracle.com/technetwork/java/overview-138580.htmlhttp://www.oracle.com/technetwork/java/overview-138580.htmlhttp://www.oracle.com/technetwork/java/index-jsp-135995.htmlhttp://www.oracle.com/technetwork/java/index-jsp-135995.htmlhttp://www.oracle.com/technetwork/java/index-jsp-135995.htmlhttp://www.oracle.com/technetwork/java/index-jsp-135995.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/overview/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/overview/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/overview/index.htmlhttp://www.mysql.com/http://www.mysql.com/https://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttp://www.mysql.com/http://docs.oracle.com/javase/tutorial/jdbc/overview/index.htmlhttp://www.oracle.com/technetwork/java/index-jsp-135995.htmlhttp://www.oracle.com/technetwork/java/index-jsp-135995.htmlhttp://www.oracle.com/technetwork/java/overview-138580.htmlhttps://netbeans.org/project_downloads/samples/Samples/Java%20Web/ifpwafcad.sql
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    25/46

    The table data used in that tutorial is contained inifpwafcad.sqland is also required for this tutorial. This SQL file createstwo tables, Subject and Counselor, then populates them with sample data. If needed, save this file to your computer, thenopen it in the NetBeans IDE and run it on the MySQL database named MyNewDatabase.

    To follow this tutorial, you need the following software and resources.

    Software or Resource Version Required

    NetBeans IDE 7.2, 7.3, 7.4, 8.0, Java EE bundle

    Java Development Kit (JDK) version 7 or 8

    MySQL database server 5.x

    MySQL Connector/J JDBC Driver version 5.x

    GlassFish Server Open Source Edition 3.x or 4.x

    Notes:

    The Java download bundle of the NetBeans IDE enables you to install the GlassFish server. You require theGlassFish server to work through this tutorial.

    TheMySQL Connector/J JDBC Driver,necessary for communication between Java platforms and the MySQLdatabase protocol, is included in the NetBeans IDE.

    If you need to compare your project with a working solution, you candownload the sample application.

    Planning the Structure

    Simple web applications can be designed using a two-tier architecture, in which a client communicates directly with aserver. In this tutorial, a Java web application communicates directly with a MySQL database using the Java DatabaseConnectivity API. Essentially, it is theMySQL Connector/J JDBC Driverthat enables communication between the Javacode understood by the application server (the GlassFish server), and any content in SQL, the language understood bythe database server (MySQL).

    The application you build in this tutorial involves the creation of two JSP pages. In each of these pages you use HTMLand CSS to implement a simple interface, and apply JSTL technology to perform the logic that directly queries thedatabase and inserts the retrieved data into the two pages. The two database tables, Subject and Counselor, arecontained in the MySQL database, MyNewDatabase, which you create by completing theConnecting to a MySQLDatabasetutorial. Consider the following two-tier scenario.

    The welcome page (index.jsp) presents the user with a simple HTML form. When a browser requests index.jsp, the JSTLcode within the page initiates a query on MyNewDatabase. It retrieves data from the Subject database table, and inserts itinto to the page before it is sent to the browser. When the user submits his or her selection in the welcome page's HTMLform, the submit initiates a request for the response page (response.jsp). Again, the JSTL code within the page initiates a

    https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252Fifpwafcad.sqlhttps://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252Fifpwafcad.sqlhttps://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252Fifpwafcad.sqlhttps://netbeans.org/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttp://dev.mysql.com/downloads/mysql/http://dev.mysql.com/downloads/mysql/http://dev.mysql.com/downloads/connector/j/http://dev.mysql.com/downloads/connector/j/http://dev.mysql.com/downloads/connector/j/https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252FIFPWAFCAD.ziphttps://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252FIFPWAFCAD.ziphttps://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252FIFPWAFCAD.ziphttp://dev.mysql.com/downloads/connector/j/http://dev.mysql.com/downloads/connector/j/http://dev.mysql.com/downloads/connector/j/https://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttp://dev.mysql.com/downloads/connector/j/https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252FIFPWAFCAD.ziphttp://dev.mysql.com/downloads/connector/j/http://dev.mysql.com/downloads/mysql/http://www.oracle.com/technetwork/java/javase/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttps://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252Fifpwafcad.sql
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    26/46

    query on MyNewDatabase. This time, it retrieves data from both the Subject and Counselor tables and inserts it into to thepage, allowing the user to view data based upon his or her selection when the page is returned to the browser.

    In order to implement the scenario described above, you develop a simple application for a fictitious organization namedIFPWAFCAD, The International Former Professional Wrestlers' Association for Counseling and Development.

    index.jsp

    response.jsp

    Creating a New Project

    Begin by creating a new Java web project in the IDE:

    1. Choose File > New Project (Ctrl-Shift-N;-Shift-N on Mac) from the main menu. Select the Java Web category,then select Web Application. Click Next.

    The New Project wizard allows you to create an empty web application in a standard IDE project. The standardproject uses an IDE-generated Ant build script to compile, deploy, and run the application.

    2. In Project Name, enter IFPWAFCAD. Also, specify the location for the project on your computer. (By default, theIDE places projects in a NetBeansProjects folder located in your home directory.) Click Next.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    27/46

    3. In the Server and Settings panel, specify the GlassFish server as server which will be used to run the application.

    Note. The GlassFish server displays in the Server drop-down field if you installed the Java version of theNetBeans IDE.Because the GlassFish server is included in the download, it is automatically registered with theIDE. If you want to use a different server for this project, click the Add button located next to the Server drop-downfield, and register a different server with the IDE. However, working with servers other than the GlassFish serveris beyond the scope of this tutorial.

    4. In the Java EE Version field, select Java EE 5.

    Java EE 6 and Java EE 7 web projects do not require the use of the web.xml deployment descriptor, and theNetBeans project template does not include the web.xml file in Java EE 6 and Java EE 7 projects. However, thistutorial demonstrates how to declare a data source in the deployment descriptor, and it does not rely on anyfeatures specific to Java EE 6 or Java EE 7, so you can set the project version to Java EE 5.

    Note.You could equally set the project version to Java EE 6 or Java EE 7 and then create a web.xml deploymentdescriptor. (From the New File wizard, select the Web category, then Standard Deployment Descriptor.)

    5. Click Finish. The IDE creates a project template for the entire application, and opens an empty JSP page(index.jsp) in the editor. The index.jsp file serves as the welcome page for the application.

    Preparing the Web Interface

    Begin by preparing the welcome (index.jsp) and response (response.jsp) pages. The welcome page implements an HTMLform that is used to capture user data. Both pages implement an HTML table to display data in a structured fashion. In thissection, you also create a stylesheet that enhances the appearance of both pages.

    Setting up the welcome page Creating the response page Creating a stylesheet

    Setting up the welcome page

    Confirm that index.jsp is open in the editor. If the file is not already open, double-click index.jsp under the Web Pagesnode in the IFPWAFCAD project in the Projects window.

    1. In the editor, change the text between the tags to: IFPWAFCAD Homepage.2. Change the text between the tags to: Welcome to IFPWAFCAD, the International Former Professional

    Wrestlers' Association for Counseling and Development!.3. Open the IDE's Palette by choosing Window > Palette (Ctrl-Shift-8;-Shift-8 on Mac) from the main menu. Hove

    your pointer over the Table icon from the HTML category and note that the default code snippet for the itemdisplays.

    https://netbeans.org/downloads/index.htmlhttps://netbeans.org/downloads/index.htmlhttps://netbeans.org/kb/docs/web/mysql-webapp.html#welcomePagehttps://netbeans.org/kb/docs/web/mysql-webapp.html#welcomePagehttps://netbeans.org/kb/docs/web/mysql-webapp.html#responsePagehttps://netbeans.org/kb/docs/web/mysql-webapp.html#responsePagehttps://netbeans.org/kb/docs/web/mysql-webapp.html#stylesheethttps://netbeans.org/kb/docs/web/mysql-webapp.html#stylesheethttps://netbeans.org/kb/docs/web/mysql-webapp.html#stylesheethttps://netbeans.org/kb/docs/web/mysql-webapp.html#responsePagehttps://netbeans.org/kb/docs/web/mysql-webapp.html#welcomePagehttps://netbeans.org/downloads/index.html
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    28/46

    You can configure the Palette to your liking - right-click in the Palette and choose Show Big Icons and Hide ItemNames to have it display as in the image above.

    4. Place your cursor at a point just after the tags. (This is where you want to implement the new HTML table.)Then, in the Palette, double-click the Table icon.

    5. In the Insert Table dialog that displays, specify the following values then click OK:

    o Rows: 2o

    Columns : 1o Border Size: 0

    The HTML table code is generated and added to your page.

    6. Add the following content to the table heading and the cell of the first table row (new content shown in bold):

    IFPWAFCAD offers expert counseling in a wide range of fields .

    To view the contact details of an IFPWAFCAD certified form erprofessional wrestler in your area, select a subject below:

    7. For the bottom row of the table, insert an HTML form. To do so, place your cursor between the second pair of

    tags, then double-click the HTML form ( ) icon in the Palette. In the Insert Form dialog, type inresponse.jsp in the Action text field, then click OK.

    8. Type in the following content between the tags (new content shown in bold):

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    29/46

    Select a subject:

    9. Press Enter to add an empty line after the content you just added and then double-click Drop-down List in the

    Palette to open the Insert Drop-down dialog box.10. Type subject_id for the Name text field in the Insert Drop-down dialog and click OK. Note that the code snippet forthe drop-down list is added to the form.

    The number of options for the drop-down is currently not important. Later in the tutorial you will add JSTL tagsthat dynamically generate options based on the data gathered from the Subject database table.

    11. Add a submit button item ( ) to a point just after the drop-down list you just added. You can either use thePalette to do this, or invoke the editor's code completion as illustrated in the previous step. In the Insert Buttondialog, enter submit for both the Label and Name text fields, then click OK.

    12. To format your code, right-click in the editor and choose Format (Alt-Shift-F; Ctrl-Shift-F on Mac). Your code isautomatically formatted, and should now look similar to the following:

    Welcome to IFPWAFCAD, the International Former

    Professional Wrestlers' Association for Counseling and Development!

    IFPWAFCAD offers expert counseling in a wide range of fields.

    To view the contact details of an IFPWAFCAD certified formerprofessional wrestler in your area, select a subject below:

    Select a subject:

    To view this page in a browser, right-click in the editor and choose Run File (Shift-F6; Fn-Shift-F6 on Mac). Whenyou do this, the JSP page is automatically compiled and deployed to your server. The IDE opens your defaultbrowser to display the page from its deployed location.

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    30/46

    Creating the response page

    In order to prepare the interface for response.jsp you must first create the file in your project. Note that most of the contentthat displays in this page is generated dynamically using JSP technology. Therefore, in the following steps you addplaceholderswhich you will later substitute for the JSP code.

    1. Right-click the IFPWAFCAD project node in the Projects window and choose New > JSP. The New JSP Filedialog opens.

    2. In the JSP File Name field, enter response. Note that Web Pages is currently selected for the Location field,meaning that the file will be created in the project's web directory. This is the same location as where the index.jspwelcome page resides.

    3. Accept any other default settings and click Finish. A template for the new response.jsp page is generated andopens in the editor. A new JSP node also displays under Web Pages in the Projects window.

    4. In the editor, change the title to: IFPWAFCAD - {placeholder}.5. Remove the Hello World! line between the tags, then copy and paste the following HTML table

    into the body of the page:

    {placeholder}

    Description: {placeholder}

    Counselor: {placeholder}


    member since: {placeholder}

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    31/46

    Contact Details: email:

    {placeholder}
    phone: {placeholder}

    To view this page in a browser, right-click in the editor and choose Run File (Shift-F6; Fn-Shift-F6 on Mac). Thepage compiles, is deployed to the GlassFish server, and opens in your default browser.

    Creating a stylesheet

    Create a simple stylesheet that enhances the display of the web interface. This tutorial assumes that you understand howstyle rules function, and how they affect corresponding HTML elements found in index.jsp and response.jsp.

    1. Open the New File wizard by pressing the New File ( ) button in the IDE's main toolbar. Select the Webcategory, then select Cascading Style Sheet and click Next.

    2. Type style for CSS File Name and click Finish. The IDE creates an empty CSS file and places it in the sameproject location as index.jsp and response.jsp. Note that a node for style.css now displays within the project in theProjects window, and the file opens in the editor.

    3. In the editor, add the following content to the style.css file:

    body {font-family: Verdana, Arial, sans-serif;font-size: smaller;padding: 50px;color: #555;

    }

    h1 {text-align: left;letter-spacing: 6px;font-size: 1.4em;

    color: #be7429;font-weight: normal;width: 450px;

    }

    table {width: 580px;padding: 10px;background-color: #c5e7e0;

    }

    th {text-align: left;

  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    32/46

    border-bottom: 1px solid;}

    td {padding: 10px;

    }

    a:link {color: #be7429;font-weight: normal;text-decoration: none;

    }

    a:link:hover {color: #be7429;font-weight: normal;text-decoration: underline;

    }

    4. Link the stylesheet to index.jsp and response.jsp. In both pages, add the following line between the tags:

    To quickly navigate between files that are open in the editor, press Ctrl-Tab, then select the file you are wanting.

    Preparing Communication between the Application and Database

    The most efficient way to implement communication between the server and database is to set up a database connectionpool. Creating a new connection for each client request can be very time-consuming, especially for applications thatcontinuously receive a large number of requests. To remedy this, numerous connections are created and maintained in aconnection pool. Any incoming requests that require access to the application's data layer use an already-createdconnection from the pool. Likewise, when a request is completed, the connection is not closed down, but returned to thepool.

    After preparing the data source and connection pool for the server, you then need to instruct the application to use thedata source. This is typically done by creating an entry in the application's web.xml deployment descriptor. Finally, youneed to ensure that the database driver (MySQL Connector/J JDBC Driver) is accessible to the server.

    Important: From this point forward, you need you ensure that you have a MySQL database instance namedMyNewDatabase set up that contains sample data provided inifpwafcad.sql.This SQL file creates two tables, Subject andCounselor, then populates them with sample data. If you have not already done this, or if you need help with this task, seeConnecting to a MySQL Databasebefore proceeding further.

    Also, your database needs to be password-protected to create a data source and work with the GlassFish server in thistutorial. If you are using the default MySQL root account with an empty password, you can set the password from acommand-line prompt.

    This tutorial uses nbuser as an example password. To set your password to nbuser, navigate to your MySQL installation'sbin directory in the command-line prompt and enter the following:

    shell> mysql -u rootmysql> UPDATE mysql.user SET Password = PASSWORD('nbuser')

    -> WHERE User = 'root';mysql> FLUSH PRIVILEGES;

    For more information, see the official MySQL Reference Manual:Securing the Initial MySQL Accounts.

    https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252Fifpwafcad.sqlhttps://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252Fifpwafcad.sqlhttps://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252Fifpwafcad.sqlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttp://dev.mysql.com/doc/refman/5.1/en/default-privileges.htmlhttp://dev.mysql.com/doc/refman/5.1/en/default-privileges.htmlhttp://dev.mysql.com/doc/refman/5.1/en/default-privileges.htmlhttp://dev.mysql.com/doc/refman/5.1/en/default-privileges.htmlhttps://netbeans.org/kb/docs/ide/mysql.htmlhttps://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252Fifpwafcad.sql
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    33/46

    1. Setting up a JDBC data source and connection pool2. Referencing the data source from the application3. Adding the database driver's JAR file to the server

    Setting up a JDBC data source and connection poo l

    The GlassFish Server Open Source Edition contains Database Connection Pooling (DBCP) libraries that provideconnection pooling functionality in a way that is transparent to you as a developer. To take advantage of this, you need toconfigure aJDBC(Java Database Connectivity) data sourcefor the server which your application can use for connectionpooling.

    For more information on JDBC technology, seeThe Java Tutorials: JDBC Basics.

    You could configure the data source directly within the GlassFish server Admin Console, or, as described below, you candeclare the resources that your application needs in a glassfish-resources.xml file. When the application is deployed, theserver reads in the resource declarations, and creates the necessary resources.

    The following steps demonstrate how to declare a connection pool, and a data source that relies on the connection pool.

    The NetBeans JDBC Resource wizard allows you to perform both actions.

    1. Open the New File wizard by pressing the New File ( ) button in the IDE's main toolbar. Select the GlassFishserver category, then select JDBC Resource and click Next.

    2. In step 2, General Attributes, choose the Create New JDBC Connection Pool option, then in the JNDI Name textfield, type injdbc/IFPWAFCAD.

    The JDBC data source relies onJNDI,the Java Naming and Directory Interface. The JNDI API provides a uniformway for applications to find and access data sources. For more information, seeThe JNDI Tutorial.3. Optionally, add a description for the data source. For example, type in: Accesses the database that provides data

    for the IFPWAFCAD application.4. Click Next, then click Next again to skip step 3, Additional Properties.5. In Step 4, type in IfpwafcadPoolfor JDBC Connection Pool Name. Make sure the Extract from Existing

    Connection option is selected, and choose jdbc:mysql://localhost:3306/MyNewDatabase from the drop-down list.Click Next.

    https://netbeans.org/kb/docs/web/mysql-webapp.html#setUpJDBChttps://netbeans.org/kb/docs/web/mysql-webapp.html#setUpJDBChttps://netbeans.org/kb/docs/web/mysql-webapp.html#referenceDataSourcehttps://netbeans.org/kb/docs/web/mysql-webapp.html#referenceDataSourcehttps://netbeans.org/kb/docs/web/mysql-webapp.html#addJarhttps://netbeans.org/kb/docs/web/mysql-webapp.html#addJarhttp://docs.oracle.com/javase/tutorial/jdbc/overview/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/overview/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/overview/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/basics/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/basics/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/basics/index.htmlhttp://www.oracle.com/technetwork/java/jndi/index.htmlhttp://www.oracle.com/technetwork/java/jndi/index.htmlhttp://www.oracle.com/technetwork/java/jndi/index.htmlhttp://docs.oracle.com/javase/jndi/tutorial/http://docs.oracle.com/javase/jndi/tutorial/http://docs.oracle.com/javase/jndi/tutorial/http://docs.oracle.com/javase/jndi/tutorial/http://www.oracle.com/technetwork/java/jndi/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/basics/index.htmlhttp://docs.oracle.com/javase/tutorial/jdbc/overview/index.htmlhttps://netbeans.org/kb/docs/web/mysql-webapp.html#addJarhttps://netbeans.org/kb/docs/web/mysql-webapp.html#referenceDataSourcehttps://netbeans.org/kb/docs/web/mysql-webapp.html#setUpJDBC
  • 7/25/2019 Connecting to Oracle Database From NetBeans IDE

    34/46

    Note: The wizard detects any database connections that have been set up in the IDE. Therefore, you need tohave already created a connection to the MyNewDatabase database at this point. You can verify whatconnections have been created by opening the Services window (Ctrl-5;-5 on Mac) and looking for connection

    nodes ( ) under the Databases category.6. In Step 5, select javax.sql.ConnectionPoolDataSource in the Resource Type drop-down list.

    Note that the IDE extracts information from the dat