Using Java and the database provided: Modify 2.) from above. This time we are ad
ID: 3704879 • Letter: U
Question
Using Java and the database provided:
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();
-----------------------------------------------------------------------------------------------------------------------------------------
public void selectDB(int SidInput){
Student s =[SELECT SID, firstName, lastName, street, city, state, zip, EMail, GPA FROM Student WHERE SID =: SidInput];
}
Students ID FirstName LastName Street City State Zip EMail GPA 1 Larry Jones 200 Larue St. Denver CO 89721 larry@yahoo.com 3.2 2 Debbie Gibson 101 Cambell St. Chicago IL 61721 debra@hotmail.com 2.9 3 James Henry 9019 Par Ln. Atlanta GA 30981 jh@yahoo.com 3.5 4 Tony Danza 100 Main St San Diego CA 90890 tony@yahoo.com 3.4 5 Marie Baker 313 Mockingbid Lane Seattle WA 98711 mbaker@hotmail.com 2.2 6 Mary Weathers 10 King St Denver CO 65334 maryw@bellsouth.net 2.1 7 Biily Wagner 777 Blake St Chicago IL 61900 billyw@comcast.net 2.9 8 Tim Allen 200 South St Detroit MI 54123 9 Gary Stevens 112 Plymouth Ln Ann Arbor MI 54123 garys@hotmail.com 3.2 10 Betsy Cook 101 Freeport St Atlanta GA 31010 bcook@yahoo.com 3.3 11 Susan Jones 200 West Ave Marietta GA 30060 sujones@hotmail.com 2.2 12 Frank Peters 3845 Beckford Ave San Diego CA 95123 frankperters@comcast.net 3.9 13 Terri March 9516 Hale Dr St Louis MO 63321 tmarch@bellsouth.net 3.1 14 Jack Hines 2222 Morningside Dr Marietta GA 30090 jhines@yahoo.com 2.8 15 Phil Gecko 45 East St Montgomery AL 41231 pgecko@yahoo.com 2.7 16 Tony Peters 200 Central Ave Miami FL 75213Explanation / Answer
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();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.