





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















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();
catch (java.lang.Exception ex) { System.out.println("Connect or execute query exception: " + ex); ex.printStackTrace(); } } }
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;
if (numParms != 3) { fprintf(stderr, "Usage %s
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; }
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 $<
INSERT INTO Sailors VALUES (:c_sname, :c_rating, c_age);
[CONTINUE | DO c-statement | GOTO stmt];
EXEC SQL WHENEVER SQLERROR DO My_Exit(); EXEC SQL WHENEVER NOT FOUND DO BREAK;
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 */