Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. public class MBContact {
  2.  
  3.     private Contact contact;
  4.     private ArrayList<Contact> contacts;
  5.  
  6.     static Connection connection = null;
  7.     static Statement stm = null;
  8.     static ResultSet rs = null;
  9.  
  10.     public MBContact() {
  11.         try {
  12.             if (connection == null) {
  13.                 Class.forName("com.mysql.jdbc.Driver");
  14.                 connection = DriverManager.getConnection(
  15.                         "jdbc:mysql://localhost:3306/site", "root", "");
  16.             }
  17.         } catch (Exception e) {
  18.             System.err.println("Error SQL: " + e);
  19.             connection = null;
  20.         }
  21.     }
  22.  
  23.     public Contact getContact() {
  24.         return contact;
  25.     }
  26.  
  27.     public void setContact(Contact contact) {
  28.         this.contact = contact;
  29.     }
  30.  
  31.     public ArrayList<Contact> getContacts() throws SQLException {
  32.         contacts = new ArrayList<Contact>();
  33.         stm = connection.createStatement();
  34.         rs = stm.executeQuery("SELECT * FROM si_contact");
  35.         while (rs.next()) {
  36.             contact = new Contact();
  37.             contact.setId(rs.getInt("id"));
  38.             contact.setName(rs.getString("name"));
  39.             contact.setPhone(rs.getString("phone"));
  40.             contact.setEmail(rs.getString("email"));
  41.             contacts.add(contact);
  42.         }
  43.         rs.close();
  44.         stm.close();
  45.         return contacts;
  46.     }
  47.  
  48.     public String insert() throws SQLException {
  49.         contact = new Contact();
  50.         stm = connection.createStatement();
  51.         stm.execute("INSERT into si_contact (name, phone, email) VALUES ('"
  52.                 + contact.getName() + "','" + contact.getPhone() + "','"
  53.                 + contact.getEmail() + "')");
  54.         return "sucesso";
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement