In JBuilder, data is extracted from a server into a data set. This action is called "providing". Once the data is provided, you can view and work with the data locally in data-aware controls. You can store your data to local memory (MemoryStore) or to a local single-file database with a hierarchical directory structure (DataStore). When you want to save the changes back to your database, you must resolve the data. This process is discussed in more detail in Understanding JBuilder's DataExpress architecture.
With a stored procedure, one or more SQL statements are encapsulated in a single location on your server and can be run as a batch. In the Client/Server version of JBuilder, ProcedureDataSet components enable you to access, or provide, data from your database with existing stored procedures, invoking them with either JDBC procedure escape sequences or server-specific syntax for procedure calls. To run a stored procedure against a SQL table, you need a Database component, a ProcedureDataSet component, and a ProcedureDescriptor. You can provide this information programmatically, or by using JBuilder design tools.
When providing data from JDBC data sources, the ProcedureDataSet has built-in functionality to fetch data from a stored procedure that returns a cursor to a result set. The following properties of the ProcedureDescriptor object affect the execution of stored procedures:
Property | Purpose |
---|---|
database | Specifies what Database connection object to run the procedure against. |
procedure | A Java String representation of a stored procedure escape sequence or SQL statement that causes a stored procedure to be executed. |
parameters | An optional ReadWriteRow from which to fill in parameters. These values can be acquired from any DataSet or ReadWriteRow. |
executeOnOpen | Causes the ProcedureDataSet to execute the procedure when it is first opened. This is useful for presenting live data at design time. You may also want this enabled at run time. The default value is true. |
loadOption | An optional integer value that defines the method of loading data into the data set. Options are:
|
A ProcedureDataSet can be used to run stored procedures with and without parameters. A stored procedure with parameters can acquire the values for its parameters from any DataSet or ParameterRow. The section Example: using parameters with Oracle PL/SQL stored procedures provides an example.
Use SQL Explorer to browse and edit database server-specific schema objects, including tables, fields, stored procedure definitions, triggers, and indexes. For more information on SQL Explorer, select Tools|SQL Explorer and refer to its online help.
The following topics related to stored procedure components are covered:
This tutorial shows how to provide data to an application using JBuilder's UI Designer and a ProcedureDataSet component. This example also demonstrates how to attach the resulting data set to a GridControl and a NavigatorControl for data viewing and editing.
The finished example for this tutorial may be available as a completed project in the samples\borland\samples\tutorial\dataset\StorProc directory of your JBuilder installation under the file name StorProc.jpr or check www.borland.com/techpubs/jbuilder/ for the latest updates. Other sample applications referencing stored procedures on a variety of servers are available in the samples\borland\samples\tutorial\dataset\StoredProcedure directory.
These steps run a stored procedure that creates a table and insert, update, and delete procedures on the local InterBase server (in the directory set up in Installing Local InterBase Server). This procedure is written in the InterBase language. These procedures will be used both in this section and in the Tutorial: saving changes with a NavigatorControl and Tutorial: saving changes with a ProcedureResolver.
To create this application and populate a data set from the stored procedure,
Property name | Value |
Connection URL | jdbc:odbc:DataSet Tutorial |
Username | SYSDBA |
Password | masterkey |
The code generated by the designer for this step is:
database1.setConnection(new borland.sql.dataset.ConnectionDescriptor
("jdbc:odbc:DataSet Tutorial", "SYSDBA", "masterkey", false,
"sun.jdbc.odbc.JdbcOdbcDriver"));
The connection dialog includes a Test Connection button. Click this button to check that the connection properties have been correctly set. Results of the connection attempt are displayed in the gray area below the button. When the text indicates Success, click OK to close the dialog.
Property name | Value |
Database | database1 |
Stored Procedure Escape or SQL Statement | SELECT * FROM GET_COUNTRIES |
Place SQL Text In Resource Bundle | uncheck |
Several procedures were created when createprocedures.java was run. The procedure GET_COUNTRIES is the only one that will return a result set. The SELECT statement is how a procedure is called in the InterBase language. The other procedures will be used for resolving data in the topic Tutorial: saving changes with a ProcedureResolver.
The code generated by this step is:
procedureDataSet1.setProcedure(new borland.sql.dataset.ProcedureDescriptor(database1, "select * from GET_COUNTRIES", null, true, Load.ALL));
Tip: You can use the Browse Procedures button in future projects to learn what stored procedures are available. See Discussion of stored procedure escape sequences, SQL statements, and server-specific procedure calls for more information.
Click Test Procedure to ensure that the procedure is runnable. When the gray area beneath the button indicates Success, click OK to close the dialog.
To view the data in your application,
You must add resolving capability to your application in order to edit, insert, and delete data in the running application. See:
When entering information in the Stored Procedure Escape or SQL Statement field in the procedure property editor, or in code, you have three options for the type of statement to enter. These are
To browse the database for an existing procedure, click Browse Procedures in the procedure property editor. A list of available procedure names for the database you are connected to is displayed. If the server is InterBase and you select a procedure that does not return data, you receive a notice to that effect. If you select a procedure that does return data, JBuilder attempts to generate the correct escape syntax for that procedure call. However, you may need to edit the automatically-generated statement to correspond correctly to your server's syntax.
If the procedure is expecting parameters, you have to match these with the column names of the parameters.
To enter a JDBC procedure escape sequence, use the following formatting:
When a server allows a separate syntax for procedure calls, you can enter that syntax instead of an existing stored procedure or JDBC procedure escape sequence. For example, server-specific syntax may look like this:
In both of the last two examples, the parameter markers, or question marks, may be replaced with named parameters of the form :ParameterName. For an example using named parameters, see Example: using parameters with Oracle PL/SQL stored procedures. For an example using InterBase stored procedures, see Example: using InterBase stored procedures.
package ProcSetUp; import borland.jbcl.dataset.*; import borland.sql.dataset.*; import java.sql.*; public class CreateProcedures { public static void main(String[] args) throws DataSetException { Database database1 = new Database(); database1.setConnection(new ConnectionDescriptor("jdbc:odbc:dataset tutorial", "sysdba", "masterkey", false, "sun.jdbc.odbc.JdbcOdbcDriver")); try { database1.executeStatement("DROP PROCEDURE GET_COUNTRIES"); } catch (Exception ex) {}; try { database1.executeStatement("DROP PROCEDURE UPDATE_COUNTRY"); } catch (Exception ex) {}; try { database1.executeStatement("DROP PROCEDURE INSERT_COUNTRY"); } catch (Exception ex) {}; try { database1.executeStatement("DROP PROCEDURE DELETE_COUNTRY"); } catch (Exception ex) {}; database1.executeStatement(getCountriesProc); database1.executeStatement(updateProc); database1.executeStatement(deleteProc); database1.executeStatement(insertProc); database1.closeConnection(); } static final String getCountriesProc = "CREATE PROCEDURE GET_COUNTRIES RETURNS ( \r\n"+ " COUNTRY VARCHAR(15), \r\n"+ " CURRENCY VARCHAR(10) ) AS \r\n"+ "BEGIN \r\n"+ " FOR SELECT c.country, c.currency \r\n"+ " FROM country c \r\n"+ " INTO :COUNTRY,:CURRENCY \r\n"+ " DO \r\n"+ " BEGIN \r\n"+ " SUSPEND; \r\n"+ " END \r\n"+ "END;"; static final String updateProc = "CREATE PROCEDURE UPDATE_COUNTRY( \r\n"+ " OLD_COUNTRY VARCHAR(15), \r\n"+ " NEW_COUNTRY VARCHAR(15), \r\n"+ " NEW_CURRENCY VARCHAR(20) ) AS \r\n"+ "BEGIN \r\n"+ " UPDATE country \r\n"+ " SET country = :NEW_COUNTRY \r\n"+ " WHERE country = :OLD_COUNTRY; \r\n"+ "END;"; static final String insertProc = "CREATE PROCEDURE INSERT_COUNTRY( \r\n"+ " NEW_COUNTRY VARCHAR(15), \r\n"+ " NEW_CURRENCY VARCHAR(20) ) AS \r\n"+ "BEGIN \r\n"+ " INSERT INTO country(country,currency) \r\n"+ " VALUES (:NEW_COUNTRY,:NEW_CURRENCY); \r\n"+ "END;"; static final String deleteProc = "CREATE PROCEDURE DELETE_COUNTRY( \r\n"+ " OLD_COUNTRY VARCHAR(15) ) AS \r\n"+ "BEGIN \r\n"+ " DELETE FROM country \r\n"+ " WHERE country = :OLD_COUNTRY; \r\n"+ "END;"; }
This is a very simple procedure. For a look at more complicated InterBase stored procedures, use Tools|SQL Explorer to browse procedures on this server. An interesting example is the stored procedure ORG_CHART, which returns a result set that combines data from several tables. ORG_CHART is written in InterBase's procedure and trigger language, which includes SQL data manipulation statements plus control structures and exception handling.
In InterBase, the SELECT procedures may be used to generate a DataSet. In the InterBase sample database, employee.gdb, the stored procedure ORG_CHART is such a procedure. To call this procedure from JBuilder, enter the following syntax in the Stored Procedure Escape or SQL Statement field in the procedure property editor, or in code:
select * from ORG_CHART
For a look at more complicated InterBase stored procedures, use SQL Explorer to browse procedures on this server. ORG_CHART is an interesting example. It returns a result set that combines data from several tables. ORG_CHART is written in InterBase's procedure and trigger language, which includes SQL data manipulation statements plus control structures and exception handling.
The output parameters of ORG_CHART turn into columns of the produced DataSet.
See the InterBase Server documentation for more information on writing InterBase stored procedures or see Creating tables and procedures for the tutorial manually for an example of a stored procedure written in InterBase.
Currently, a ProcedureDataSet can only be populated with Oracle PL/SQL stored procedures if you are using Oracle's type-2 or type-4 JDBC drivers, or Borland's DataGateway. The stored procedure that is called must be a function with a return type of CURSOR REF.
Follow this general outline for using Oracle stored procedures in JBuilder:
create or replace function MyFct1(INP VARCHAR2) RETURN rcMyTable1 as
type rcMyTable1 is ref cursor return MyTable1%ROWTYPE;
rc rcMyTable;
begin
open rc for select * from MyTable1;
return rc;
end;
ParameterRow row = new ParameterRow();
row.addColumn( "INP", Variant.STRING, ParameterType.IN);
row.setString("INP", "Input Value");
String proc = "{?=call MyFct1(?)}";
{?=call MyPackage1.MyFct1(?)}
See your Oracle server documentation for information on the Oracle PL/SQL language.
Stored procedures created on Sybase servers are created in a "chained" transaction mode. In order to call Sybase stored procedures as part of a ProcedureResolver, the procedures must be modified to run in an unchained transaction mode. To do this, use the Sybase stored system procedure sp_procxmode to change the transaction mode to either "anymode" or "unchained". For more details, see the Sybase documentation.
By default, all data sets store row data in memory (MemoryStore). To store data in a single file with a hierarchical directory structure, use a DataStore instead. Storing data in a DataStore provides persistent storage and caching for JBuilder DataSets, Java Objects, and arbitrary files. The advantages to using DataStore are that the implementation is pure Java, portable, there is a smaller footprint, and provides for better performance.