Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MBContact {
- private Contact contact;
- private ArrayList<Contact> contacts;
- static Connection connection = null;
- static Statement stm = null;
- static ResultSet rs = null;
- public MBContact() {
- try {
- if (connection == null) {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/site", "root", "");
- }
- } catch (Exception e) {
- System.err.println("Error SQL: " + e);
- connection = null;
- }
- }
- public Contact getContact() {
- return contact;
- }
- public void setContact(Contact contact) {
- this.contact = contact;
- }
- public ArrayList<Contact> getContacts() throws SQLException {
- contacts = new ArrayList<Contact>();
- stm = connection.createStatement();
- rs = stm.executeQuery("SELECT * FROM si_contact");
- while (rs.next()) {
- contact = new Contact();
- contact.setId(rs.getInt("id"));
- contact.setName(rs.getString("name"));
- contact.setPhone(rs.getString("phone"));
- contact.setEmail(rs.getString("email"));
- contacts.add(contact);
- }
- rs.close();
- stm.close();
- return contacts;
- }
- public String insert() throws SQLException {
- contact = new Contact();
- stm = connection.createStatement();
- stm.execute("INSERT into si_contact (name, phone, email) VALUES ('"
- + contact.getName() + "','" + contact.getPhone() + "','"
- + contact.getEmail() + "')");
- return "sucesso";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement