This is for a class assignment. I have to create a student class with three sub-classes(grad, undergrad, part-time). Within the student class I need to use set and get methods() to capture the data. The student object is then stored in the database. I can access the database. I was able to insert a record @ the cmd prompt using mysql. I can query the database in java and produce a reseultSet. I am unable to add data to the database. I know my get/set methods create the desired student object because I overrode the toString() method and was able to print the object info that was input at the prompts. Any help is greatly apprieciated.
Thanks,
Moni
Student Class
Graduate sub-class/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package registrar; import java.sql.*; import java.util.Scanner; /** * * @author Moni */ public abstract class Student { private String firstName; private String lastName; private int studentID; private double gradeAverage; String status; private String mentorName; private String level; private String thesisTitle; private String thesisAdvisor; private String company; //overloaded constructors public Student() { } public Student(String first, String last, int id, double gpa, String stat, String mentor) { firstName = first; lastName = last; studentID = id; gradeAverage = gpa; status = stat; mentorName = mentor; } public Student(String first, String last, int id, double gpa, String stat, String mentor, String lvl, String title, String advisor, String comp) { firstName = first; lastName = last; studentID = id; gradeAverage = gpa; status = stat; mentorName = mentor; level = lvl; thesisTitle = title; thesisAdvisor = advisor; company = comp; } //getters and setters public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public void setFirstName(String first) { firstName = first; } public void setLastName(String last) { lastName = last; } public int getStudentID() { return studentID; } public void setStudentID(int id) { studentID = id; } public String getStatus() { return status; } public void setStatus(String s) { status = s; } public double getGPA() { return gradeAverage; } public void setGPA(double gpa) { gradeAverage = gpa; } public String getMentor() { return mentorName; } public void setMentor(String mentor) { mentorName = mentor; } public String getLevel() { return level; } public void setLevel(String lvl) { level = lvl; } public String getTitle() { return thesisTitle; } public void setTitle(String title) { thesisTitle = title; } public String getAdvisor() { return thesisAdvisor; } public void setAdvisor(String advisor) { thesisAdvisor = advisor; } public String getCompany() { return company; } public void setCompany(String comp) { company = comp; } //toString override @Override public String toString() { return "student[Registration= " + studentID + firstName + "\t" + lastName + "\t" + gradeAverage + "\t" + status + "\t" + mentorName + "\t" + level + "\t" + thesisTitle + "\t" + thesisAdvisor + "\t" + company + "]"; } //abstract methods public abstract double calculateTuition(); public abstract String add(); public abstract boolean update(); public abstract boolean delete(); public static ResultSet getQuery() throws SQLException { Statement stmt = null; Connection conn = SimpleDataSource.getConnection(); try { stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String select = "SELECT * from student"; ResultSet rs = stmt.executeQuery(select); System.out.println("Student Records:"); while(rs.next()) { System.out.println("Student ID#:\t" + rs.getString("studentID")); System.out.println("First Name:\t" + rs.getString("firstName")); System.out.println("Last Name:\t" + rs.getString("lastName")); System.out.println("Grade Avg:\t" + rs.getString("gpa")); System.out.println("Status:\t" + rs.getString("status")); System.out.println("Mentor:\t" + rs.getString("mentor")); System.out.println("Level:\t" + rs.getString("level")); System.out.println("Title:\t" + rs.getString("thesisTitle")); System.out.println("Advisor:\t" + rs.getString("thesisAdvisor")); System.out.println("Company:\t" + rs.getString("company")); } stmt.close(); conn.close(); } catch (SQLException e) { System.out.println(e.getMessage()); } return null; } }
Registration (main)/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package registrar; import java.io.File; import java.io.IOException; import java.sql.*; import java.util.ArrayList; import java.util.Scanner; /** * * @author Moni */ public class Graduate extends Student { double rate; double creditHours = 16; public void setGradRate(double rate) { if (status.equalsIgnoreCase("non-resident")) { rate = 350; } else { rate = 300; } } @Override public double calculateTuition() { this.setGradRate(rate); double tution = rate * creditHours; return tution; } @Override public String add() { Connection conn = null; Statement stmt1 = null; try { Scanner in = new Scanner(System.in); conn = SimpleDataSource.getConnection(); stmt1 = conn.createStatement(); System.out.print("Enter the Student ID #: "); int id = in.nextInt(); System.out.print("Enter First Name: "); String first = in.next(); System.out.print("Enter Last Name: "); String last = in.next(); System.out.print("Enter Student Status: "); String stat = in.next(); System.out.print("Enter Mentor's Name: "); String mentor = in.next(); double gpa = 3.5; System.out.print("Enter the Student's Level: "); String lvl = in.next(); System.out.print("Enter the Thesis Title: "); String title = in.next(); System.out.print("Enter the Thesis Advisor's Name: "); String advisor = in.next(); System.out.print("Enter the Company's Name: "); String comp = in.next(); //set variables this.setStudentID(id); this.setFirstName(first); this.setLastName(last); this.setGPA(gpa); this.setStatus(stat); this.setMentor(mentor); this.setLevel(lvl); this.setTitle(title); this.setAdvisor(advisor); this.setCompany(comp); stmt1.execute("INSERT into student" + "(studentID, firstName, lastName, gpa, status, mentor," + "level, thesisTitle, thesisAdvisor, company)" + "VALUES ('" + getStudentID() + "','" + getFirstName() + "','" + getLastName() + "','" + getGPA() + "','" + getStatus() + "','" + getMentor() + "','" + getLevel() + "'," + getTitle() + "','" + getAdvisor() + "','" + getCompany() + "')"); // try // { // stmt1.execute(addStudent); //System.out.println("1 record added"); // } // catch(SQLException s) // { // // System.exit(0); // } stmt1.close(); conn.close(); } catch(Exception e) { System.out.println("SQL statement is not executed!"); } return null; } @Override public boolean update() { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean delete() { throw new UnsupportedOperationException("Not supported yet."); } }
/* * This is the main program that manipulates and queries the student * database table. * APA References: * [url=http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html]Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)[/url] * retrieved March 5, 2011a * */ package registrar; import java.io.File; import java.io.IOException; import java.sql.*; import java.util.Scanner; /** * * @author Moni */ public class Registration { /** * Finds and returns a list of students registered */ public static void main (String[] args) throws IOException, SQLException, ClassNotFoundException { //SimpleDataSource.init(args[0]); Scanner in = new Scanner(System.in); System.out.print("Welcome To Student Records. Select Action:" + "[A]ADD, [D]DELETE, [E]EDIT: "); String action = in.next(); if(action.equalsIgnoreCase("A")) { System.out.print("Enter Student Type: " + "[G]Grad/[P]Part-time/[U]Undergrad: "); String type = in.next(); //create graduate object to add if(type.equalsIgnoreCase("g")) { Graduate grad = new Graduate(); // add to the database grad.add(); //String gradString = grad.toString(); //System.out.println(gradString); //System.out.println(grad.getFirstName()); } //create undergrad object to add else if(type.equalsIgnoreCase("u")) { } } ResultSet query = Student.getQuery(); System.out.print(query); } }