Advertisement
Guest User

Untitled

a guest
Aug 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. public Contact() {
  2.  
  3. }
  4. public Contact (String name, String email, String address, int phone) {
  5. this.name=name;
  6. this.email=email;
  7. this.address=address;
  8. this.telephoneNumber=phone;
  9. }
  10. //Getters e Setters
  11. ...
  12. }
  13.  
  14.  
  15. public class ContactDAO {
  16. private Connection con;
  17.  
  18.  
  19. public Connection getConnection() {
  20. return con;
  21. }
  22.  
  23. public ContactDAO() {
  24. ConnectionFactory conF = new ConnectionFactory();
  25. con=conF.getBDConnection();
  26. }
  27.  
  28. public void addClient(Contact contact) {
  29. String sql= "INSERT INTO contatos" + "(nome,email,endereco,telefone)" +
  30. "values (?,?,?,?)";
  31.  
  32. try {
  33. PreparedStatement stmt= con.prepareStatement(sql);
  34.  
  35.  
  36. stmt.setString(1,contact.getName());
  37. stmt.setString(2,contact.getEmail());
  38. stmt.setString(3,contact.getAddress());
  39. stmt.setInt(4,contact.getTelephoneNumber());
  40.  
  41.  
  42. stmt.executeUpdate();
  43. stmt.close();
  44. } catch (SQLException sqle) {
  45. sqle.printStackTrace();
  46. } finally {
  47. try {
  48. con.close();
  49. } catch (SQLException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. }
  56.  
  57.  
  58. public class ConnectionFactory {
  59.  
  60.  
  61. public Connection getBDConnection() {
  62. Connection con = null;
  63. System.out.println("Testing access to BD MySQLn");
  64.  
  65. try {
  66. Class.forName("com.mysql.jdbc.Driver");
  67. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root", "");
  68. System.out.println("Connection successful!!!");
  69. } catch (ClassNotFoundException cnfe){
  70. cnfe.printStackTrace();
  71. System.out.println("ClassNotFoundException");
  72. } catch (SQLException sqle) {
  73. sqle.printStackTrace();
  74. System.out.println("SQLException");
  75. }
  76. return con;
  77. }
  78.  
  79. public void closeConnection(Connection con) {
  80. try {
  81. con.close();
  82. System.out.println("n Connection successfully close!!!");
  83. }
  84. catch (SQLException sqle) {
  85. System.out.println("SQLException");
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement