Question: Using Java: 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

Using Java:

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();

-------------------------------------------

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

public class Student {

private int SID;

private String FirstName, LastName, Street, City, State;

private int Zip;

private String EMail;

private double GPA;

public Student() {

}

public Student(int SID, String firstName, String lastName, String street, String city, String state, int zip, String EMail, double GPA) {

this.SID = SID;

FirstName = firstName;

LastName = lastName;

Street = street;

City = city;

State = state;

Zip = zip;

this.EMail = EMail;

this.GPA = GPA;

}

public void display() {

System.out.println("ID: " + SID);

System.out.println("Name: " + FirstName + " " + LastName);

System.out.println("Street: " + Street);

System.out.println("City: " + City);

System.out.println("State: " + State);

System.out.println("Zip: " + Zip);

System.out.println("Email: " + EMail);

System.out.println("GPA: " + GPA);

}

public static void main(String[] args) {

Student s1 = new Student(4,"Frank", "Jones", "123 Main", "Atlanta", "GA", 30133, "fj@yahoo.com", 3.2);

s1.display();

}

public void insertDB(int SID, String FirstName, String LastName, String Street, String City, String State, int Zip, String EMail, double GPA){

// fillin the object

this.SID = SID;

this.FirstName = FirstName;

this.LastName = LastName ;

this.Street = Street;

this.City= City;

this.State = State;

this.Zip = Zip;

this.EMail = EMail;

this.GPA = GPA;

// forming the query with the given values

StringBuffer query = new StringBuffer("insert into student values(");

query.append(SID);

query.append(",");

query.append("'"+FirstName+"'");

query.append(",");

query.append("'"+LastName+"'");

query.append(",");

query.append("'"+Street+"'");

query.append(",");

query.append("'"+City+"'");

query.append(",");

query.append("'"+State+"'");

query.append(",");

query.append(Zip);

query.append(",");

query.append("'"+EMail+"'");

query.append(",");

query.append(GPA+")");

Connection con = null;

try {

// change the URL according to the connection

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sonoo","user","password");

} catch (SQLException e) {

System.out.println("Problem in Connecting Db");

e.printStackTrace();

}

Statement st = null;

try {

st = con.createStatement();

} catch (SQLException e) {

System.out.println("Problem in creating statement");

e.printStackTrace();

}

try {

st.executeUpdate(new String(query));

} catch (SQLException e) {

System.out.println("Problem while inserting data");

e.printStackTrace();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!