Chapter 10: Introduction to SQL Programming Techniques
CHAPTER 10: INTRODUCTION TO SQL PROGRAMMING TECHNIQUES
Answers to selected Exercises
10.7
loop=1;
while(loop)
{
prompt(“Please enter your name: “, name);
EXEC SQL DECLARE REPORT CURSOR FOR
select G.Grade, C.Credit_hours
from STUDENT S, GRADE_REPORT G, SECTION SEC, COURSE C
sum=0;
avg=0;
while(SQLCODE==0)
{
switch (grade)
default: println(“This grade %s will not be calculated.”, grade);
break;
}
EXEC SQL FETCH from REPORT into :grade, :credit;
}
avg = sum/count;
printf(“Student named %s has a grade point average %f.”, name, avg);
}
EXEC SQL CLOSE REPORT;
10.8
loop=1;
while(loop)
{
name = readEntry(“Please enter your name: “);
#sql iterator REPORT(char grade, double credit);
Chapter 10: Introduction to SQL Programming Techniques
G.Section_identifier=SEC.Section_identifier AND
SEC.Course_number=C.Course_number AND S.Name=:name
};
count=0;
sum=0;
avg=0;
while(r.next())
{
switch (r.grade)
{
case A: sum=sum+(4*r.credit); count=count+1; break;
calculated.”); break;
}
};
avg = sum/count;
10.9
EXEC SQL DECLARE BOOKLIST CURSOR FOR
select B.Title, BR.Name
from BOOK B, BOOK_LOANS BL, BORROWER BR
where B.Book_id=BL.Book_id AND BL.Card_no=BR.Card_no AND
BL.Due_date={fn CURDATE()}-1;
EXEC SQL OPEN BOOKLIST;
10.10
#sql iterator BOOKLIST(String title, String borrowername);
BOOKLIST bl = null;
#sql bl =
{
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
10.12
10.12.1)
import java.io.*;
import java.sql.*;
class calGPA
{
public static void main (String args[]) throws SQLException, IOException
System.out.println(“Driver could not be loaded.”);
}
String dbacct, passwrd, name;
Char grade;
int credit;
dbacct = readEntry(“Enter database account: “);
passwrd = readEntry(“Enter password: “);
Connection conn =
DriverManager.getConnection(“jdbc:oracle:oci8:”+dbacct+“/”+passwrd);
String stmt1 =
select G.Grade, C.Credit_hours
from STUDENT S, GRADE_REPORT G, SECTION SEC, COURSE C
where G.Student_number=S.Student_number AND
G.Section_identifier=SEC.Section_identifier AND
SEC.Course_number=C.Course_number AND S.Name=?“;
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
}
}
10.12.2)
import java.io.*;
import java.sql.*;
class listBorrowers
{
public static void main (String args[]) throws SQLException, IOException
{
try
{
}
}