Hi, My question is from the textbook:Java how to program eighth edition by Paul
ID: 3682270 • Letter: H
Question
Hi,
My question is from the textbook:Java how to program eighth edition by Paul Deitel and Harvey Deitel Solution
28.8 (AddressBook Modification: Delete an Existing Entry) Modify the in Exercise 28.7 to provide a JButton that allows the user to call a method named deletePerson in PersonQueries class to delete the current entry in the AddressBook database.
The code can be find here:http://www.chegg.com/homework-help/address-book-modification-delete-existing-entry-modify-progr-chapter-24-problem-8e-solution-9780133813425-exc
Thank you so much!
Explanation / Answer
import java.util.*; import java.sql.*; publicclassPersonDAO { /* Person Table needs to be created in the Oracle Database. * create table Person ( id Integer, name Varchar(30), address Varchar(30), phone Integer, email Varchar(50) );*/ privateArrayList personsList; privateString userid ="scott"; privateString password ="tiger"; staticString url ="jdbc:odbc:bob"; privateConnection con;// constructor publicPersonDAO(){ personsList =newArrayList(); getConnection();//Create Connection to the Oracle Database}publicConnection getConnection(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(java.lang.ClassNotFoundException e){ System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try{ con =DriverManager.getConnection(url, userid, password); } catch(SQLException ex) { System.err.println("SQLException: "+ ex.getMessage()); } return con; } publicArrayList searchPerson(String name) { try{ String sql ="SELECT * FROM Person WHERE name like '%"+ name +"%'"; // Create a prepared statement Statement s = con.createStatement(); ResultSet rs = s.executeQuery(sql); String pname =""; String address =""; String email =""; int id, phone; while(rs.next()) { id = rs.getInt("id"); pname = rs.getString("name"); address = rs.getString("address"); phone = rs.getInt("phone"); email = rs.getString("email");//Create a PersonInfo objectPersonInfo person =newPersonInfo(id, pname, address, phone, email);//Add the person object to array list personsList.add(person); } } catch(Exception e) { System.out.println(e); } return personsList; } publicvoid savePerson(PersonInfo person) { try { String sql ="INSERT INTO Person(name, address, "+"phone, email) VALUES (?,?,?,?) "; // Create a Preparedstatement PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, person.getName()); ps.setString(2, person.getAddress()); ps.setInt(3, person.getPhone()); ps.setString(4, person.getEmail()); ps.executeUpdate(); } catch(Exception e) { System.out.println(e); } } publicvoid updatePerson(PersonInfo person) { try { String sql ="UPDATE Person SET name = ?, address=? , "+"phone=? , email=? where id=?"; // Create a Prepared statement PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, person.getName()); ps.setString(2, person.getAddress()); ps.setInt(3, person.getPhone()); ps.setString(4, person.getEmail()); ps.setInt(5, person.getId()); ps.executeUpdate(); } catch(Exception e) { System.out.println(e); } } publicint removePerson(String name) { int no=0; try{ String sql ="DELETE FROM Person WHERE name = ?"; // Create a Prepared statement PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, name); no= ps.executeUpdate(); } catch(Exception e) { System.out.println(e); }return no; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.