1.) Create a class called Student. This class will not be tied to the Database,
ID: 3808542 • Letter: 1
Question
1.) Create a class called Student. This class will not be tied to the Database, yet. The Student class should have the same properties as the Database table: SID, FirstName, LastName, etc. Include all the set and get functions as needed. Include 2 constructors, an empty constructor, and one that takes all properties. Include a display() method that prints out all properties. Use a main to instantiate and test out this Student class. (Hint: No database calls in this class, yet.) Testing Code in Main() method Student s1 = new Student(4,”Frank”, “Jones”, “123 Main”, “Atlanta”, “GA”, 30133, “fj@yahoo.com”, 3.2); s1.display(); 2.) Modify 1.) from above. Add a new Method called selectDB() that takes only one argument, SID. When a user calls this method, it should SELECT from the database that Student and get all the Student’s data from the database, and put this data into the appropriate properties. (Hint: You will use an SQL Select statement here.) Test out this method in the main. Testing Code in Main() method Student s1 = new Student(); s1.selectDB(4); //accessing DB s1.display(); //displays all data from DB for student with id=4 3.) Modify 2.) from above. This time we are add another method called insertDB(). This method will take all 9 data elements as arguments. This method will then Insert this information into the Student table, as well as fill up the object with the data provided. (Hint: You will use an SQL Insert statement here.) Testing Code in Main() method Student s2=new Student(); s2.insertDB(33, “Frank”, “Mayes”, “123 Main street”, “Atlanta”, “GA”, 30100,”fmayes@yahoo.com”,3.3f); s2.display(); 4.) Lastly add 2 functions to Update and Delete student information. Testing Code in Main() method Student s3 = new Student(); s3.selectDB(6); s3.deleteDB(); And Student s4 = new Student (); s4.selectDB(7); s4.setZipcode(30106); s4.updateDB();
Explanation / Answer
This is Student class:
package com.studentOperationWithDB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Student {
int Id;
String FirstName;
String LastName;
String StreetAddress;
String City;
String Country;
int Pin;
String Email;
double GPA;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String firstName, String lastName, String streetAddress,
String city, String country, int pin, String email, double gPA) {
super();
this.Id = id;
this.FirstName = firstName;
this.LastName = lastName;
this.StreetAddress = streetAddress;
this.City = city;
this.Country = country;
this.Pin = pin;
this.Email = email;
this.GPA = gPA;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getStreetAddress() {
return StreetAddress;
}
public void setStreetAddress(String streetAddress) {
StreetAddress = streetAddress;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
public int getPin() {
return Pin;
}
public void setPin(int pin) {
Pin = pin;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
public int insertDB(int id, String firstName, String lastName, String streetAddress,
String city, String country, int pin, String email, double gPA){
int x=0;
try{
DbUtility db=new DbUtility();
Connection con=db.getConnection();
System.out.println(2);
PreparedStatement ps=con.prepareStatement("insert into student111 values(?,?,?,?,?,?,?,?,?)");
ps.setInt(1,id );
ps.setString(2, firstName);
ps.setString(3, lastName);
ps.setString(4, streetAddress);
ps.setString(5, city);
ps.setString(6, country);
ps.setInt(7, pin);
ps.setString(8,email);
ps.setDouble(9, gPA);
System.out.println(3);
x=ps.executeUpdate();
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
return x;
}
public void selectDB(int Id){
try{
Connection con=DbUtility.getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from student111 where id="+"Id");
while(rs.next()){
System.out.println(1);
System.out.println("i m in showdata ");
System.out.println(2);
setId(rs.getInt(1));
setFirstName(rs.getString(2));
setLastName(rs.getString(3));
setStreetAddress(rs.getString(4));
setCity(rs.getString(5));
setCountry(rs.getString(6));
setPin(rs.getInt(7));
setEmail(rs.getString(8));
setGPA(rs.getDouble(9));
}
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
}
public void display(){
System.out.println("Student FirstName: "+getFirstName());
System.out.println("Student LastName: "+getLastName());
System.out.println("Student StreetAddress: "+getStreetAddress());
System.out.println("Student city: "+getCity());
System.out.println("Student Country: "+getCountry());
System.out.println("Student CountyCode: "+getPin());
System.out.println("Student Email: "+getEmail());
System.out.println("Student GPA: "+getGPA());
}
public int updateDB(int id, String firstName, String lastName, String streetAddress,
String city, String country, int pin, String email, double gPA){
int x=0;
try{
DbUtility db=new DbUtility();
Connection con=db.getConnection();
System.out.println(2);
PreparedStatement ps=con.prepareStatement("update student111 set id=?,firstName=?,lastName=?,streetAddress=?,city=?,country=?,pin=?,email=?,gPA=? where id="+"Id");
ps.setInt(1,id );
ps.setString(2, firstName);
ps.setString(3, lastName);
ps.setString(4, streetAddress);
ps.setString(5, city);
ps.setString(6, country);
ps.setInt(7, pin);
ps.setString(8,email);
ps.setDouble(9, gPA);
System.out.println(3);
x=ps.executeUpdate();
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
return x;
}
public int deleteDB(int Id){
int x=0;
try{
DbUtility db=new DbUtility();
Connection con=db.getConnection();
System.out.println(2);
Statement st=con.createStatement();
x=st.executeUpdate("delete from student111 where id="+"Id");
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
return x;
}
}
This is Driver class:
package com.studentOperationWithDB;
public class Test {
//First create table in database using below query
/*CREATE TABLE `test`.`student111` (
`id` INTEGER NOT NULL,
`firstName` VARCHAR(45) NOT NULL,
`lastName` VARCHAR(45) NOT NULL,
`streetAddress` VARCHAR(45) NOT NULL,
`city` VARCHAR(45) NOT NULL,
`country` VARCHAR(45) NOT NULL,
`pin` INTEGER NOT NULL,
`email` VARCHAR(45) NOT NULL,
`gPA` DOUBLE NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;*/
public static void main(String[] args) {
Student s1 = new Student();
//int xx=s1.insertDB(4,"Frank", "Jones", "123 Main", "Atlanta", "GA", 30133, "fj@yahoo.com", 3.2);
//System.out.println("Insert operation success if 1: "+xx);
s1.selectDB(4);
s1.display();
//int yy=s1.deleteDB(4);
//System.out.println("Delete operation success if 1: "+yy);
int zz=s1.updateDB(4,"Mary", "Marlo", "111Main", "Atlanta", "GA", 30133, "fj@yahoo.com", 3.2);
System.out.println("Insert operation success if 1: "+zz);
s1.selectDB(4);
s1.display();
}
}
Output:
i m dbutil
connected
i m in showdata
Student FirstName: firstName
Student LastName: lastName
Student StreetAddress: 123 Main
Student city: Atlanta
Student Country: GA
Student CountyCode: 30133
Student Email: fj@yahoo.com
Student GPA: 3.2
i m dbutil
connected
Insert operation success if 1: 1
i m dbutil
connected
i m in showdata
Student FirstName: Mary
Student LastName: Marlo
Student StreetAddress: 111Main
Student city: Atlanta
Student Country: GA
Student CountyCode: 30133
Student Email: fj@yahoo.com
Student GPA: 3.2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.