1.7 Oracle JDeveloper Oracle JDeveloper is an integrated development environment (IDE) for developing Java programs. It uses a graphical user interface that enables you to write, compile, and debug Java applications without having to use command-line utilities. While JDeveloper is not actually required to develop SQLJ programs, it is a very useful and powerful tool. This section describes the basic features of JDeveloper and illustrates how to use JDeveloper to translate, compile, run, and debug the HelloWorld.sqlj program. In addition to developing standard Java programs, JDeveloper may be used to develop many other types of Java-based programs, including: SQLJ statements can be included in any of the Java program types listed here. 1.7.1 Basic Features of JDeveloper This section gives you a quick tour of the basic features of JDeveloper. The JDeveloper integrated development environment is shown in Figure 1-3. Figure 1-3. The JDeveloper integrated development environment JDeveloper organizes the working environment into a workspace. A workspace is saved in a file with the extension .jws and can contain one or more projects, which are saved in files with the extension .jpr. In Figure 1-3, the workspace file sqlj_workspace.jws is open and contains the project file sqlj_project.jpr. A project can contain Java source files, and the figure shows that file FundamentalExample1.sqlj is open for editing. The menu bar at the top allows you to access all the functions within JDeveloper. These functions are organized into the following menus: File, Edit, Search, View, Project, Run, Wizards, Tools, Windows, and Help. Some functions can also be accessed using the toolbar, or from the context-sensitive menu obtained by clicking the right mouse button in various areas in the workspace. The Project menu contains the options used to build the executable from the source files. The Run menu contains menu for running and debugging programs. A toolbar is displayed in the upper-left corner of the IDE and contains buttons that may be used to open and close projects, save files, undo and redo changes, and cut and paste text. The component palette is displayed in the upper-right corner and may be used to add graphical elements to Java applets. JDeveloper uses a number of panes to display and manipulate programs. The Navigation pane, located in the upper left, displays the files contained in the project. The Structure pane, located below the Navigation pane, displays class names and the names of the methods and variables within each class. The Content pane, located to the right of the Navigation pane, is used to display and edit the contents of files. The Message View pane at the bottom of the screen displays the output from the compiler, and the output from Java programs when they are run. 1.7.2 HelloWorld in JDeveloper In this section, I show you how to use JDeveloper to compile, run, and debug the HelloWorld.sqlj program. The first thing you do is create a new workspace: go to the File menu and select New Workspace. You should see a blank workspace such as the one shown in Figure 1-4. The new workspace will have the default filename of Untitled1.jws. Figure 1-4. A new workspace in JDeveloper Next, create a new project by going to the File menu and selecting New Empty Project. Figure 1-5 shows a new project with the default filename MyProject1.jpr. Figure 1-5. A new empty project Save the new workspace by clicking on Untitled1.jws, selecting the Save As function from the File menu, and saving with the filename MyWorkspace.jws. Then, click on MyProject1.jpr, select the Save As function from the File menu, and save the new project with the filename MyProject.jpr. Next, create a new SQLJ program: from the File menu, select New. This displays a dialog, shown in Figure 1-6, that allows you to select the type of program to create. Figure 1-6. The Program Object Type dialog box Click SQLJ File to select it and click OK to continue. The SQLJ Wizard dialog box will be displayed, as shown in Figure 1-7. Figure 1-7. The SQLJ Wizard dialog box In the SQLJ Wizard dialog, enter "HelloWorld" as the class name and delete the text in the Package field; there's no need to set a package name for this class. Click OK to continue, and a file named HelloWorld.sqlj is then added to your project. Double-click on the HelloWorld.sqlj file in the Navigation pane to open the file in the Content pane. You will see that JDeveloper has created a skeleton of code for the program, as shown in Figure 1-8. Figure 1-8. The code generated by JDeveloper For the "Hello World" program, you won't be using any of the skeleton code generated by JDeveloper. Instead, cut and paste the code from the HelloWorld.sqlj file shown earlier in this chapter into the code window, making sure to replace all the original code. Your screen should now resemble Figure 1-9. Figure 1-9. The "Hello World" program in JDeveloper | You'll notice that the HelloWorld.sqlj file does not import the same packages as those in the skeleton code. HelloWorld.sqlj imports oracle.sqlj.runtime.Oracle, which includes the Oracle.connect( ) method, and also imports some of the java.sql packages. The skeleton code imports sqlj.runtime.* and sqlj.runtime.ref.*, which support features not used by HelloWorld.sqlj. |
| Before compiling the HelloWorld.sqlj program, you should save all the files by selecting Save All from the File menu. Next, build the project. From the Project menu, select Make Project "MyProject.jpr". This translates the file HelloWorld.sqlj and compiles the resulting HelloWorld.java file. Once this is complete, run the program by selecting Run "HelloWorld" from the Run menu. JDeveloper runs the program and displays the output from the program in the Message View pane, as shown in Figure 1-10. Figure 1-10. The Message View pane showing the output from HelloWorld.sqlj 1.7.3 Debugging JDeveloper may be used to debug Java programs. In this section, I'll show you how to set a breakpoint in the HelloWorld.sqlj program and inspect the value of the current_date variable as the program runs. The breakpoint will stop the program right before the SELECT statement is executed. To set the breakpoint, right-click on the following line in the source code: #sql { SELECT sysdate INTO :current_date FROM dual }; Select Toggle Breakpoint from the resulting context menu. Once the breakpoint is set, the line on which it is set is highlighted and marked with a dot at the start of the line. This is shown in Figure 1-11. Figure 1-11. A program line on which a breakpoint has been set To begin a debugging session, you run the program in debug mode by selecting Debug "HelloWorld" from the Run menu. Once execution reaches the line containing the breakpoint, control returns to the JDeveloper debugger, as shown in Figure 1-12. Notice the "Source breakpoint" message in the Message View pane at the bottom of the window. Figure 1-12. The JDeveloper debugger From the debugger, you can step over the current line, step out of the current method, run to the end of the method, or run to the end of the program using the functions in the Run menu. You can also display the values stored in program variables by selecting Inspect from the Run menu. Selecting Inspect causes JDeveloper to display the Inspect dialog box shown in Figure 1-13. Figure 1-13. The Inspect dialog box In Figure 1-13, current_date has been entered as the symbol to inspect. To display the value stored in current_date, click OK. A window appears containing the value stored in current_date, as shown in Figure 1-14. Figure 1-14. The current_date value inspected The value stored in current_date is unknown right now because the SELECT statement has not been executed yet. To execute the SELECT statement, select Step Over from the Run menu. This causes the SQLJ debugger to execute the entire SQLJ statement. Then inspect the current_date variable again; this time current_date should contain an actual date, such as the one shown in Figure 1-15. Figure 1-15. The current_date value with an actual date Select Run from the Run menu to continue the program execution. The date is displayed in the Message View pane, as shown in Figure 1-16. Figure 1-16. Completed execution of the program JDeveloper is a great tool for creating Java programs, but it is not mandatory for developing SQLJ programs; you can always use the command-line utilities to do the same tasks that JDeveloper does. However, JDeveloper should improve your productivity because it provides a graphical integrated development environment. This section has provided a basic introduction to JDeveloper, and should be enough to get you started if you have access to this tool. |