
SQLJ Part O, now known as SQL/OLB (Object-Language Bindings) Andrew Eisenberg Jim Melton Sybase, Burlington, MA 01803 Sybase, Sandy, UT 84093 andrew.eisenberg @ sybase.com [email protected] to be defined; although there are presently no Introductions proposals to extend this to other languages, several participants have expressed interest in supporting For about a year and a half, an informal and open other object-oriented languages, such as C++ or group of companies has been meeting to consider Smalltalk. how the Java" programming language and relational As this article is being written, the editor has databases might be used together. Initially called just submitted his resolution of comments for the JSQL and later SQLJ, the companies that have U.S. public review that just took place. It is expected participated in this group are Compaq (Tandem), that SQL/OLB will be formally approved by the end IBM, Informix, Micro Focus, Microsoft, Oracle, Sun, of 1998. When it is approved, it will be available for and Sybase. purchase from ANSI as ANSI X3.135.10:1998. The intent of this group when it was formed was to suggest and review one another's ideas, Javasoft's JDBC TM meeting fairly often, see where there was common understanding and agreement on syntax and JDBC, initially provided in JDK 1.1, defines a Java semantics, and to eventually provide a basis for one API for accessing relational DBMSs. It is mentioned or several formal standards. here because SQLJ Part 0 layers upon it. The work began with a proposal on how The JDBC API is a fairly rich one. It SQL statements might be embedded in Java, put provides classes and methods to: forward by Oracle. Later Sybase put forward • connect to the database proposals on how to use Java in the database to • get capability, syntax, and limit provide the implementation of stored routines and metadata from the database user-defined data types (UDTs). Once an initial draft • execute a query or DDL statement of a specification was put forward, the entire group • prepare a DML or call statement, with participated in reviewing it, spotting problems, and parameters to be supplied at the time the suggesting enhancements. These 3 parts are, then, statement executes roughly described as: • retrieve both data and metadata for result sets produced by statement Part 0 Embedded SQL in Java execution Part 1 Java Stored Routines JDBC provides for the dynamic execution of Part 2 Java Data Types SQL statements. Any syntax or semantic errors in the SQL statements will raise exceptions at the time the These three parts have all progressed application runs. rapidly. Since work on Part 0 was started before the A JDBC driver must support Entry SQL-92 other parts, it was the first to be submitted to a formal statements, with some extensions defined in the standards body. SQLJ Part 0 has been processed as JDBC specification. It may also support statements Database Language SQL -- Part 10, Object from other levels of SQL and statements that are Language Bindings (SQL/OLB), by NCITS H2, the vendor-extensions to the SQL standard. Database Language technical committee. The name A very straightforward program to retrieve for this part of the SQL standard implies a broad some data from a table might look like this: scope, with SQLJ Part 0 being the first such binding "Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. 94 SIGMOD Record, Vol. 27, No. 4, December 1998 try { SQL statements to be executed. The resulting Java String url source program will be compiled normally. = "jdbc:sybase:Tds:localhost:2638"; Connection con = At the time that the SQLJ translator runs, it DriverManager.getConnection can be told to connect to an exemplar database and (url, "DBA", "sql"); use the metadata it finds there to validate the SQL String stmt_source = statements. If the employee table did not exist, or "SELECT distinct city, state " the empid column did not exist, then the SQLJ + "FROM employee"; translator would notify the user of the error. Statement stmt = con.createStatement(); It is also possible that the vendor of the ResultSet rs SQLJ translator will provide off-line checking, which = stmt.executeQuery(stmt_source); could do the part of the checking of the statement that does not require metadata. If the "=" in the above while (rs.next()) { System.out.println statement were a "+" instead, then off-line checking (rs.getString("city") would be able to catch this mistake. + .... + rs.getString(2) At execution time, the application can ); } connect to the same database against which it was con.close (); checked, or to another database with the same } schema. catch (SQLException sqe) { The example above also shows that the System.out.println (sqe.getMessage()); } JDBC model for dealing with SQL exception conditions has been used. In other host language bindings, the SQLSTATE variable is used to inform SQLJ Part 0 Features the application of a SQL exception condition. In SQLJ Part 0, such an exception condition will cause SQLJ Part 0 allows SQL static statements to be the SQLJ statements to throw a Java exception, embedded in a Java program, in somewhat the same java. s ql. SQLExc ep t i on. way that SQL-92 allows SQL statements to be In the sections below, we will make this embedded in C, COBOL, and several other example more complicated to show additional SQLJ languages. Dynamic SQL statements are handled just Part 0 features. fine by JDBC, and so were not included in this effort. A simple SQLJ Program Connection Contexts A connection context object is used to associate the The following code fragment shows how SQLJ Part 0 execution of an SQL statement with a particular can be used to access a database: connection to a database. In the example above, an try { implicit connection context object was used. In the #sql { DELETE following example an explicit connection context FROM employee object will be used. WHERE emp_id = 17 }; } #sql context EmpContext; catch (SQLException sqe) { String url System.out.println = "jdbc:sybase:Tds:localhost:2638"; (sqe.getMessage()); } EmpContext empCtxt = new EmpContext(url, "dba", "sql", false); This example does not show some of the setup that is necessary for this example to be used. A #sql [empCtxt] { DELETE FROM employee JDBC driver must be registered with the JDBC WHERE emp_id = 17 Driver Manager, and a connection must be made to a }; database. "#sql { ... } ;"identifies an SQL The example begins with "#sql context executable statement. The curly braces ( ( } ) have •.." to declare of the connection context class (a been used to delimit the SQL statement and separate subclass of ConnectionContext), called EmpContext. it from the rest of the Java program. An SQLJ Later, an empCtxt object of this class is created. It translator will look for these embedded statements, appears in square brackets ( [ ] ) to indicate its use in and replace them with Java statements that cause the the execution of the DELETE statement. SIGMOD Record, Vol. 27, No. 4, December 1998 95 A connection context class is used by an ConnectionContext. getExecutionContext ( ) SQLJ translator to determine which database schema method. is to be used to check the validity of all of the Unlike a connection context, an execution statements that specify connection context objects of context should not be shared among threads in a that class. multithreaded application. This type of explicit context is more portable than the use of' an implicit context. The Host Variables and Expressions connection context class provides methods that can Ordinary static SQL allows for the use of host examine and change some of the properties of the variables in expressions, in addition to literals, connection. column references, SQL variables, and SQL An application can operate on multiple parameters. connections to the same database, or multiple SQLJ Part 0 allows the use of Java host connections to different databases, using explicit variables and host expressions, as seen in the connection context objects. Connection contexts may following example: be safely shared among threads in a multithreaded application. int id; In some environments, such as within a DBMS, an SQLJ application may be invoked with a #sql { SELECT emp_id connection context already provided for its use. The INTO :id FROM employee ConnectionContext. getDefaultContext ( ) WHERE emp_fname method can be used to determine if this is the case. LIKE :(argv[0] + '%') }; Execution Contexts System.out.println ("Employee id " + id); An execution context object allows some aspects of a statement's execution to be controlled, and it allows The Java host variable, "id" in this the retrieval of information about the execution after example, is used within an SQL statement to indicate it has completed. that a value must be placed into a Java variable. The pattern in the LIKE clause is a host expression that sql j. runt ime. Execut ionCont ext execCtxt concatenates an element of a String array with a = new sqlj .runtime.ExecutionContext ( ) ; String literal. SQLJ uses the same mapping between #sql [empCtxt, execCtxt ] Java data types and SQL data types that is defined by { DELETE JDBC. FROM emp i eye e The syntax for a host variable is as follows: WHERE emp_id = 17 }; <host expression> ::= : [ <parameter mode> ] <expression> System. out. print in ( "Deleted " <parameter mode> ::= IN I OUT I INOUT + execCtxt, getUpdateCount ( ) + " rows." ); <expression> ::= <variable> I ( <complex expression> ) In this example, an explicit connection context and execution context are associated with the Java variables and host expressions have a DELETE statement. The ExecutionContext object parameter mode that--if not explicitly specified--is empCtxt is used to access the number of rows that implicitly determined by its use.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-