else {
printf(“pc model: %s\n”, pc_model);
}
/* get Printer within the budget */
rest_budget = total_budget pc_price;
color = “true”;
if (!rs.next()) {
color = “false”;
execStat.setInt(1, rest_budget);
execStat.setString(2, color);
ResultSet rs = execStat.executeQuery();
}
e)
import java.sql.*;
char pmodel[4], pmaker, ptype[6];
float pspeed;
Connection myCon =
DriverManager.getConnection(<URL>, <username>, <password>);
if (count > 0) {
Printf(“Warnning: The PC model already exists\n”);
}
else {
PreparedStatement execStat2 = myCon.prepareStatement(
“INSERT INTO Product VALUES(?, ?, ?)”);
execStat2.setString(1, pmaker);
PreparedStatement execStat3 = myCon.prepareStatement(
“INSERT INTO PC VALUES(?, ?, ?, ?, ?)”);
execStat3.setString(1, pmodel);
}
9.6.2
a)
import java.sql.*;
char cclass[20], maxFirepowerClass[20];
PreparedStatement execStat = myCon.repareStatement(
“SELECT class, numGuns, bore FROM Classes”);
ResultSet classrs = execStat.executeQuery();
if(!classrs.next())
/* print message and exit */ ;
cclass = classrs.getString(1);
cnumGuns = classrs.getString(2);
cbore = classrs.getString(3);
firepower = cnumGuns * (power (cbore, 3));
b)
import java.sql.*;
char ibattle[20], ocountry[20];
int colInfo;
Class.forName(“<drive name>”);
/* print ocountry */
}
c)
import java.sql.*;
/* ask user for a class and other info for Classes table */
PreparedStatement execStat = myCon.prepareStatement(
“INSERT INTO Classes VALUES (?, ?, ?, ?, ?, ?)”);
execStat.setString(1, iclass);
execStat.setString(2, itype);
execStat.executeUpdate();
/* ask user for a ship and launched */
PreparedStatement execStat2 = myCon.prepareStatement(
“INSERT INTO Ships VALUES (?, ?, ?)”);
d)
import java.sql.*;
char bname[20], bdate[8], newbdate[8];
char sname[20], lyear[4], newlyear[4];
PreparedStatement execStat = myCon.prepareStatement(
“Select b.name, b.date, s.name, s.launched ” +
ResultSet rs = execStat.executeQuery();
while(rs.next()) {
bname = rs.getString(1);
/* prompt user and ask if a change is needed */
if(change_battle)
{
/* get a new battle date to newbdate */
PreparedStatement execStat2 = myCon.prepareStatement(
“UPDATE Battles SET date = ? WHERE name = ?”);
}
9.7.1
a)
include(DB.php);
while($tuple = $pcs->fetchRow()) {
$tempModel = $tuple[0];
$tempPrice = $tuple[1];
$tempSpeed = $tuple[2];
}
/* Now, modelOfClosest is the model whose price is closest to
target. We must get its manufacturer with a single-row select
*/
b)
include(DB.php);
$myCon = DB::connect(<vendor>:://<username>:<password>
<hostname>/<databasename>);
$products = $myCon->execute($prepQuery, $args);
while($tuple = $products->fetchRow()) {
$model = $tuple[0];
}
c)
include(DB.php);
$myCon = DB::connect(<vendor>:://<username>:<password>
<hostname>/<databasename>);
while ($tuple = $pcs->fetchRow()) {
$model = $tuple[0];
$speed = $tuple[1];
$ram = $tuple[2];
$hd = $tuple[3];
$price = $tuple[4];
/* print fetched info */
}
while ($tuple = $laptops->fetchRow()) {
$model = $tuple[0];
$speed = $tuple[1];
/* get Printers made by the manufacturer */
$prepQuery3 = $myCon->prepare(
while ($tuple = $printers->fetchRow()) {
$model = $tuple[0];
$color = $tuple[1];
$type = $tuple[2];
$price = $tuple[3];
/* print fetched info */
}
d)
include(DB.php);
$pc_price = $tuple[1];
/* print fetched info */
}
else
/* print no pc found within the budget message */
if ($tuple = $pcs>fetchRow()) {
$printer_model = $tuple[0];
$printer_price = $tuple[1];
/* print fetched info */
}
else {
e)
include(DB.php);
$myCon = DB::connect(<vendor>:://<username>:<password>
<hostname>/<databasename>);
“SELECT COUNT(*) FROM PC WHERE model = ?”);
$rs = $myCon.execute($prepQuery1, $pmodel);
$tuple = $rs->fetchRow();
$count = $tuple[0];
if ($count > 0) {
Printf(“Warnning: The PC model already exists\n”);
}
9.7.2
a)
include(DB.php);
$myCon = DB::connect(<vendor>:://<username>:<password>
<hostname>/<databasename>);
$classrs = $myCon>query(
“SELECT class, numGuns, bore FROM Classes”);
$tuple = $classrs>fetchRow();
b)
include(DB.php);
$myCon = DB::connect(<vendor>:://<username>:<password>
<hostname>/<databasename>);
}
c)
include(DB.php);
$myCon = DB::connect(<vendor>:://<username>:<password>
<hostname>/<databasename>);
/* ask user for a class and other info for Classes table */
}
d)
include(DB.php);
$myCon = DB::connect(<vendor>:://<username>:<password>
<hostname>/<databasename>);
/* prompt user and ask if a change is needed */
if(change_battle)
{
/* get a new battle date to newbdate in $args2*/
$prepStmt2 = $myCon->prepare(
“UPDATE Battles SET date = ? WHERE name = ?”);
$result = $myCon->execute($prepStmt2, $args2);
}