Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that uses an ArrayList of parameter type Contact to store a

ID: 3715090 • Letter: W

Question

Write a Java program that uses an ArrayList of parameter type Contact to store a database of contacts.

The Contact class should store the contact’s first and last name, phone number, and email address.

Add appropriate constructor(s) and/or accessors/mutators.

Your database program should present a menu that allows the user to add a contact, display all contacts, search for a specific contact and give the user the option to delete it.

The searches should find any contact where any instance variable contains a target search string. For example, if “Sam” is the search target, then any contact where the first name, last name, phone number, or email address contains “Sam” should be returned for display or deletion. Use “for-each” loop to iterate through the ArrayList.

Explanation / Answer

import java.io.*;

import java.sql.*;

public class jdbcprg {

class Contact {

private String firstname;

private String lastname;

private String phone;

private String email;

public Contact(String firstname, String lastname, String phone, String email) {

super();

this.firstname = firstname;

this.lastname = lastname;

this.phone = phone;

this.email = email;

}

public String getFirstname() {

return firstname;

}

public void setFirstname(String firstname) {

this.firstname = firstname;

}

public String getLastname() {

return lastname;

}

public void setLastname(String lastname) {

this.lastname = lastname;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

}

public static void main(String[] args) {

Connection con;

Statement st;

BufferedReader bin;

ResultSet rs;

ResultSetMetaData rm;

String LastName, Firstname, Email,Phone;

int ch, nof;

try

{

Class.forName("oracle.jdbc.driver.OracleDriver");

con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","HR","HR");

st = con.createStatement();

bin = new BufferedReader(new InputStreamReader(System.in));

while(true){

System.out.println("Choose Option");

System.out.println("1. Select");

System.out.println("2. Insert");

System.out.println("3. Delete");

System.out.println("0. Exit");

ch = Integer.parseInt(bin.readLine());

if (ch==1)

{

rs = st.executeQuery("select * from Contact");

rm = rs.getMetaData();

nof = rm.getColumnCount();

System.out.println();

for(int i=1; i<=nof; i++)

{

System.out.print(rm.getColumnName(i)+" ");

}

System.out.println();

System.out.println();

while(rs.next())

{

for(int i=1; i<=nof; i++)

{

System.out.print(rs.getString(i) +" ");

}

System.out.println();

}

System.out.println();

}

else if(ch==2)

{

System.out.println("Enter FirstName");

Firstname = bin.readLine();

System.out.println("Enter LastName");

LastName = bin.readLine();

System.out.println("Enter Phone Number");

Phone = bin.readLine();

System.out.println("Enter Email address");

Email = bin.readLine();

  

  

st.execute("insert into Contact values("+Firstname+",'"+LastName+"',"+Phone+"',"+Email+")");

System.out.println("1 Record inserted");

System.out.println("Continue ? [y/n]");

  

}

else if(ch==3)

{

System.out.println("Enter Name to Delete :");

String eno = bin.readLine();

st.execute("delete from Contact where FirstName like "+eno);

System.out.println("Record Deleted");

}

else if(ch==0)

{

bin.close();

con.close();

System.exit(0);

}

}

}

catch(Exception e)

{

System.out.println("Error :"+e.getMessage());

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote