Advertisement
Guest User

Untitled

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