Docsity

SQL from Applications: Embedded SQL, JDBC and Stored Procedures in C, Java and PostgreSQL, Lecture notes of Database Management Systems (DBMS)

Partial preview of the text

Download SQL from Applications: Embedded SQL, JDBC and Stored Procedures in C, Java and PostgreSQL and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

SQL from Applications

Overview

✥ Embedded SQL

✥ JDBC

✥ Stored Procedures

6–2 SQL from Applications (1.1.0)

Accesing data from an application

✥ Most of the time, an application will be the interface between the

database and the user

✥ Almost any programming language can be used to do it

✦ C

✦ Perl

✦ Java

✦ PHP

✦ ASP

✦ Tcl

✦ Python

✦ You name it!

import java.sql.*;

class sailors { static public void main(String[] args) { try { // Prepare driver Class.forName("org.postgresql.Driver"); // connect to the database Connection connection = DriverManager.getConnection ("jdbc:postgresql:sail3", "dmg", "password");

String sqlCommand = "select sname from Sailors order by sname"; Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sqlCommand); String sName;

while (result.next()) { sName = result.getString(1); System.out.println(sName); } connection.close();

Example

catch (java.lang.Exception ex) { System.out.println("Connect or execute query exception: " + ex); ex.printStackTrace(); } } }

Example

Example

  • embedded C sample program */ #include <stdio.h>

void My_Exit(void);

EXEC SQL INCLUDE sqlca;

/* Whenever there is an error call error handler */ EXEC SQL WHENEVER SQLERROR DO My_Exit();

int main(int numParms, char* parms[]) {

EXEC SQL BEGIN DECLARE SECTION; char c_sname = NULL; / holds value returned by query / char c_query_string[256]; / holds constructed SQL query */ char c_dbName[40], c_userName[20], c_password[20]; int c_rating, c_maxRating; EXEC SQL END DECLARE SECTION;

int rating;

6–7 SQL from Applications (1.1.0)

if (numParms != 3) { fprintf(stderr, "Usage %s \n", parms[0]); exit(1); } strcpy(c_dbName, "[email protected]"); strcpy(c_userName, parms[1]);

6–7 SQL from Applications (1.1.0)

printf("%s %-3d (max %d)\n", c_sname, c_rating, c_maxRating); } EXEC SQL CLOSE cursorSailor; /* CLOSE the cursor / EXEC SQL COMMIT; EXEC SQL DISCONNECT; / disconnect from the database */ return 0; }

6–8 SQL from Applications (1.1.0)

  • Error handler: print error and die */ void My_Exit(void) { fprintf(stderr, "Error in SQL operation: %s\n", sqlca.sqlerrm.sqlerrmc); exit(1); }

6–12 SQL from Applications (1.1.0)

C SOURCE

C COMPILER

SQL INCLUDES

OBJECT CODE

SQL & C

LIBRARIES

LINKER

EXECUTABLE

PSQLLIB=/public/lib PSQLINC=/public/include default: sailors sailors: sailors.c gcc -g -I ${PSQLINC} -o sailors sailors.c -L ${PSQLLIB} -lecpg

sailors.c: sailors.csql ecpg $<

Embedding SQL statements

✥ All SQL statements should be clearly delimited. In C you use

EXEC SQL

✥ An SQL statement can be used as a regular statement in C

✥ Any time a SQL statement uses a host variable it should be

prefixed with :

✥ A semicolon ; ends the statement

✥ Example:

EXEC SQL

INSERT INTO Sailors VALUES (:c_sname, :c_rating, c_age);

6–13 SQL from Applications (1.1.0)

Error Handling

✥ To simplify error handling, use the WHENEVER

EXEC SQL WHENEVER [NOT FOUND | SQLERROR ]

[CONTINUE | DO c-statement | GOTO stmt];

✥ NOT FOUND: When the last record of a set has been read, do...

✥ SQLERROR: Whenever there is an error, do...

✥ Examples:

EXEC SQL WHENEVER SQLERROR DO My_Exit(); EXEC SQL WHENEVER NOT FOUND DO BREAK;

6–17 SQL from Applications (1.1.0)

✥ Example:

rating = 3; sprintf(c_query_string, /* create an SQL query string */ "SELECT sname, rating
FROM sailors WHERE rating >= %d
ORDER BY rating", rating);

/* Prepare query */ EXEC SQL PREPARE s_sailorName FROM :c_query_string;

EXEC SQL DECLARE cursorSailor CURSOR FOR s_sailorName; EXEC SQL OPEN cursorSailor; /* send the query / EXEC SQL WHENEVER NOT FOUND DO BREAK; / Break out of the loop where no more rows / while (1) { EXEC SQL FETCH IN cursorSailor INTO :c_sname, :c_rating; printf("%s %-3d (max %d)\n", c_sname, c_rating, c_maxRating); } EXEC SQL CLOSE cursorSailor; / CLOSE the cursor */

Cursors...

Cursors...