SQLBindCol(execStat, 1, SQL_CHAR, model, sizeof(model), &colInfo);
SQLBindCol(execStat, 2, SQL_FLOAT, speed,
sizeof(speed), &colInfo);
while(SQLFetch(execStat) != SQL_NO_DATA) {
if( FOUND )
/* print fetched info */
}
/* get Printers made by the manufacturer */
d)
#include sqlcli.h
SQLHENV myEnv;
SQLHDBC mycon;
SQLHSTMT execStat;
if(errCode2) {
printf(”Error for SQL_ HANDLE_DBC.\n”);
exit(1);
}
}
SQLPrepare(execStat,
SELECT model, price FROM PC
WHERE speed >= ? AND price <= ?
ORDER BY price”,
SQL_NTS);
/* ask user for budget & the minimum speed of pc */
SQLFetch(execStat);
if (NOT_FOUND) {
printf(“no pc found within the budget\n”);
}
else {
printf(“pc model: %s\n”, pc_model);
}
SQLFetch(execStat);
if(NOT_FOUND) {
color = “false”;
SQLBindParameter(execStat, 1, SQL_INTEGER, …, rest_budget, …);
SQLBindParameter(execStat, 2, SQL_CHAR, …, color, …);
}
else
printf(“printer model: %s\n”, printer_model);
}
e)
#include sqlcli.h
SQLHENV myEnv;
SQLHDBC mycon;
errCode1 = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &myEnv);
if(errCode1) {
printf(”Error for SQL_HANDLE_ENV.\n”);
exit(1);
}
errCode2 = SQLAllocHandle(SQL_HANDLE_DBC, myEnv, &myCon);
if(errCode3) {
printf(”Error for SQL_ HANDLE_STMT.\n”);
exit(1);
}
/* ask user for manufacturer, model, speed, RAM, hard-disk, */
/* & price of a new PC */
SQLPrepare(execStat, “INSERT INTO Product VALUES(?, ?, ?)”,
SQL_NTS);
SQLBindParameter(execStat, 1, SQL_CHAR, …, pmaker, …);
SQLBindParameter(execStat, 2, SQL_CHAR, …, pmodel, …);
SQLBindParameter(execStat, 3, SQL_CHAR, …, ptype, …);
SQLExecute(execStat);
SQLPrepare(execStat, “INSERT INTO PC VALUES(?, ?, ?, ?, ?)”,
}
9.5.2
a)
#include sqlcli.h
SQLHENV myEnv;
SQLINTEGER colInfo;
}
errCode3 = SQLAllocHandle(SQL_HANDLE_STMT, myCon, &execStat);
if(errCode3) {
printf(”Error for SQL_ HANDLE_STMT.\n”);
exit(1);
}
SQLExecDirect(execStat,
“SELECT class, numGuns, bore FROM Classes”, SQL_NTS);
while(1) {
SQLFetch(execStat);
if(NOT_FOUND) break;
b)
#include sqlcli.h
SQLHENV myEnv;
errCode2 = SQLAllocHandle(SQL_HANDLE_DBC, myEnv, &myCon);
if(errCode2) {
printf(”Error for SQL_ HANDLE_DBC.\n”);
exit(1);
}
errCode3 = SQLAllocHandle(SQL_HANDLE_STMT, myCon, &execStat);
SQLBindParameter(execStat, 1, SQL_CHAR, …, ibattle, …);
SQLExecute(execStat);
SQLBindCol(execStat, 1, SQL_CHAR, ocountry, sizeof(ocountry),
&colInfo);
SQLRETURN errCode1, errCode2, errCode3;
SQLCHAR iclass[20], itype[3], icountry[20], iship[20];
SQLINTEGER inumGuns, ibore, idisplacement, ilaunched;
SQLINTEGER colInfo;
errCode3 = SQLAllocHandle(SQL_HANDLE_STMT, myCon, &execStat);
if(errCode3) {
printf(”Error for SQL_ HANDLE_STMT.\n”);
exit(1);
}
SQLExecute(execStat);
/* ask user for a ship and launched */
SQLPrepare(execStat, “INSERT INTO Ships VALUES (?, ?, ?)”, SQL_NTS);
WHILE(there_is_input)
{
SQLBindParameter(execStat, 1, SQL_CHAR, …, iship, …);
d)
#include sqlcli.h
SQLHENV myEnv;
SQLHDBC mycon;
errCode2 = SQLAllocHandle(SQL_HANDLE_DBC, myEnv, &myCon);
if(errCode2) {
printf(”Error for SQL_ HANDLE_DBC.\n”);
exit(1);
}
}
SQLExecDirect(execStat,
“Select b.name, b.date, s.name, s.launched ” ||
“FROM Battles b, Outcomes o, Ships s ” ||
while(SQLFetch(execStat) != SQL_NO_DATA) {
/* prompt user and ask if a change is needed */
if(change_battle)
{
/* get a new battle date to newbdate */
SQLPrepare(execStat, “UPDATE Battles SET date = ? WHERE name = ?”,
}
if(change_ship)
{
/* get a new launched year to newlyear */
SQLPrepare(execStat, “UPDATE Ships SET launched = ? WHERE name= ?”,
}
}
9.6.1
a)
import java.sql.*;
char manf, tempModel[4];
float tempSpeed;
int tempPrice;
PreparedStatement execStat = myCon.prepareStatement(
“SELECT model, price, speed FROM PC”);
ResultSet pcs = execStat.executeQuery();
While(pcs.next()) {
}
/* Now, modelOfClosest is the model whose price is closest to
target. We must get its manufacturer with a single-row select
*/
if (priceOfClosest == NULL ) /* no data fetched */
/* print error message and exit */
b)
import java.sql.*;
char model[4], maker;
/* ask user for minimum speed, ram, hd size, and screen size */
PreparedStatement execStat = myCon.prepareStatement(
“SELECT model, speed, ram, hd, screen, price, maker ” +
“FROM Laptop l, Product p ” +
“WHERE speed >= ? AND ” +
“ram >= ? AND ” +
While(products.next()) {
model = getString(1);
speed = getFloat(2);
ram = getInt(3);
hd = getInt(4);
c)
import java.sql.*;
char maker, model[4], type[10], color[6];
float speed;
int ram, hd, screen, price;
Class.forName(“<drive name>”);
Connection myCon =
DriverManager.getConnection(<URL>, <username>, <password>);
while (pcs.next()) {
model = pcs.getString(1);
speed = pcs.getFloat(2);
ram = pcs.getInt(3);
hd = pcs.getInt(4);
price = pcs.getInt(5);
/* print fetched info */
while (laptops.next()) {
model = laptops.getString(1);
speed = laptops.getFloat(2);
ram = laptops.getInt(3);
/* get Printers made by the manufacturer */
ResultSet printers = execStat3.executeQuery();
while (printers.next()) {
model = printers.getString(1);
color = printers.getString(2);
}
d)
import java.sql.*;
int total_budget, rest_budget, pc_price, printer_price;
char pc_model[4], printer_model[4], color[6];
float min_speed;
/* get the cheapest PC of the minimum speed */
PreparedStatement execStat = myCon.prepareStatement(
SELECT model, price FROM PC
WHERE speed >= ? AND price <= ?
ORDER BY price”);
execStat.setFloat(1, min_speed);