import java.sql.*; import simpledb.remote.*; import java.util.*; import java.io.*; public class CalculateGPA { public static void main(String[] args) { Connection conn = null; try { Driver d = new SimpleDriver(); conn = d.connect("jdbc:simpledb://localhost", null); Reader rdr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(rdr); System.out.print("Enter student name: "); String sname = br.readLine().trim(); Statement stmt = conn.createStatement(); String qry = "select sid from STUDENT where sname='" + sname + "'"; ResultSet rs = stmt.executeQuery(qry); if (!rs.next()) { System.out.println("No such student"); return; } int sid = rs.getInt("sid"); rs.close(); String qry2 = "select Points from ENROLL, GRADEPOINTS where Grade=LetterGrade and studentid=" + sid; ResultSet rs2 = stmt.executeQuery(qry2); int numcourses = 0; float totalpoints = 0; while (rs2.next()) { totalpoints += rs2.getInt("Points"); numcourses++; } rs2.close(); conn.close(); if (numcourses == 0) System.out.println("No courses taken"); else System.out.println("GPA = " + totalpoints/(10 * numcourses)); } catch(Exception e) { e.printStackTrace(); } finally { try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }