Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

More Books

Students also viewed these Databases questions

Question

Define the goals of persuasive speaking

Answered: 1 week ago