SQLJ

SQLJ

SQLJ

SQLJ es un estándard ISO (ISO/IEC 9075-10) para embeber sentencias SQL en programas de Lenguaje de programación Java.

Al contrario que JDBC, SQLJ no es una API sino una extensión del lenguaje. Así, los programas SQLJ deben ejecutarse a través de un preprocesador (el traductor SQLJ) antes de que puedan ser compilados.

SQLJ tiene varias ventajas sobre JDBC:

  • Los programas SQLJ son más fáciles de escribir y de mantener. Además tienden a ser más cortos que los programas JDBC equivalentes.
  • Es más eficiente que JDBC dado que las sentencias SQL son parseadas y los caminos de acceso son optimizados en tiempo de compilación en lugar de en tiempo de ejecución.
  • Suministra mejor control de autorización: La Autorización puede ser concedida a los programas en lugar de a los usuarios.
  • Los problemas de rendimiento potenciales, tales como las consultas ineficientes debido a un mal camino de acceso, pueden ser identificados en tiempo de desarrollo.

Hay varias desventajas:

  • SQLJ requiere un paso de preprocesamiento.
  • Muchos IDEs no proporcionan soporte SQLJ.
  • No hay soporte de SQLJ para la mayoría de frameworks de persistencia comunes, tales como Hibernate.

Ejemplos

Los siguientes ejemplos comparan la sintaxis de SQLJ con la utilización de JDBC.

JDBC SQLJ
Consulta de varias filas
PreparedStatement stmt = conn.prepareStatement(
   "SELECT LASTNAME"
 + ", FIRSTNME"
 + ", SALARY"
 + " FROM DSN8710.EMP"
 + " WHERE SALARY BETWEEN ? AND ?");
stmt.setBigDecimal(1, min);
stmt.setBigDecimal(2, max);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
  lastname = rs.getString(1);
  firstname = rs.getString(2);
  salary = rs.getBigDecimal(3);
  // Imprimir fila...
}
rs.close();
stmt.close();
#sql private static iterator EmployeeIterator(String, String, BigDecimal);

...

EmployeeIterator iter;
#sql [ctx] iter = {
  SELECT LASTNAME
      , FIRSTNME
      , SALARY
    FROM DSN8710.EMP
   WHERE SALARY BETWEEN:min AND:max
};
while (true) {
  #sql {
    FETCH:iter
     INTO:lastname,:firstname,:salary
  };
  if (iter.endFetch()) break;
  // Imprimir fila...
}
iter.close();
Consulta de una fila
PreparedStatement stmt = conn.prepareStatement(
    "SELECT MAX(SALARY), AVG(SALARY)"
  + " FROM DSN8710.EMP");
rs = statement.executeQuery();
if (!rs.next()) {
  // Error -- no se encontraron filas
}
maxSalary = rs.getBigDecimal(1);
avgSalary = rs.getBigDecimal(2);
if (rs.next()) {
  // Error -- más de una fila encontrada
}
rs.close();
stmt.close();
#sql [ctx] {
  SELECT MAX(SALARY), AVG(SALARY)
    INTO:maxSalary,:avgSalary
    FROM DSN8710.EMP
};
INSERT
stmt = conn.prepareStatement(
   "INSERT INTO DSN8710.EMP " +
   "(EMPNO, FIRSTNME, MIDINIT, LASTNAME, HIREDATE, SALARY) "
 + "VALUES (?, ?, ?, ?, CURRENT DATE, ?)");
stmt.setString(1, empno);
stmt.setString(2, firstname);
stmt.setString(3, midinit);
stmt.setString(4, lastname);
stmt.setBigDecimal(5, salary);
stmt.executeUpdate();
stmt.close();
#sql [ctx] {
  INSERT INTO DSN8710.EMP
    (EMPNO,  FIRSTNME,   MIDINIT,  LASTNAME,  HIREDATE,     SALARY)
  VALUES
    (:empno,:firstname,:midinit,:lastname, CURRENT DATE,:salary)
};

Enlaces externos

Obtenido de "SQLJ"

Wikimedia foundation. 2010.

Игры ⚽ Поможем написать курсовую

Mira otros diccionarios:

  • SQLJ — SQLJ  подмножество стандарта SQL, направленное на объединение преимуществ синтаксиса языков SQL и Java ради удобства реализации бизнес логики и работы с данными. Данный стандарт разработан консорциумом, состоящим из компаний IBM, Micro Focus …   Википедия

  • Sqlj — подмножество стандарта Java ради удобства реализации бизнес логики и работы с данными. Данный стандарт разработан консорциумом, состоящим из компаний Microsoft, Informix, Oracle, Sun и Содержание 1 Предыстория 2 Пример кода 3 SQLJ и JDBC 4… …   Википедия

  • SQLJ — (Embedded SQL) ist ein eingebettetes SQL für Java. SQLJ ermöglicht eine direkte Einbettung von SQL Anweisungen in Java Code, wobei die statischen SQL Anweisungen zur Übersetzungszeit syntaktisch und semantisch überprüft werden können. SQLJ bietet …   Deutsch Wikipedia

  • SQLJ — is an ISO standard (ISO/IEC 9075 10) for embedding SQL statements in Java programs.Unlike JDBC, SQLJ is not an API but a language extension. Thus, SQLJ programs must be run through a preprocessor (the SQLJ translator) before they can be… …   Wikipedia

  • SQLJ — abbr. Structured Query Language Java (SQL, Java, DB, ANSI, NCITS) …   United dictionary of abbreviations and acronyms

  • SQL — This article is about the database language. For the airport with IATA code SQL, see San Carlos Airport. SQL Paradigm(s) Multi paradigm Appeared in 1974 Designed by Donald D. Chamberlin Raymond F. Boyce Developer …   Wikipedia

  • SQL/OLB — The SQL/OLB, or Object Language Bindings , extension to the SQL standard is defined by ISO/IEC 9075 10:2003. SQL/OLB defines the syntax and symantics of SQLJ, which is SQL embedded in Java. The standard also describes mechanisms to ensure binary… …   Wikipedia

  • Pro*C — [pɹoʊˈsiː]/Pro*C++ [ ˈplʌs ˈplʌs] ist ein Precompiler des Unternehmens Oracle für die Programmiersprache C und C++. Mittels des Precompilers ist es möglich, SQL Ausdrücke und normale C oder C++ Quellcode Elemente miteinander zu vermischen. Dies… …   Deutsch Wikipedia

  • SQL-92 — SQL (das Kürzel für Structured Query Language; offizielle Aussprache [ɛskjuːˈɛl], häufig auch [ˈsiːkwəl] →SEQUEL), ist eine Datenbanksprache zur Definition, Abfrage und Manipulation von Daten in relationalen Datenbanken. SQL ist von ANSI und ISO… …   Deutsch Wikipedia

  • SQL-99 — SQL (das Kürzel für Structured Query Language; offizielle Aussprache [ɛskjuːˈɛl], häufig auch [ˈsiːkwəl] →SEQUEL), ist eine Datenbanksprache zur Definition, Abfrage und Manipulation von Daten in relationalen Datenbanken. SQL ist von ANSI und ISO… …   Deutsch Wikipedia

Compartir el artículo y extractos

Link directo
Do a right-click on the link above
and select “Copy Link”