Guest User

Untitled

a guest
Apr 10th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. package pacote;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import javax.servlet.RequestDispatcher;
  7. import javax.servlet.ServletConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. @WebServlet("/www")
  14. public class AdicionaContatoServlet extends HttpServlet {
  15. public void init(ServletConfig config) throws ServletException{
  16. super.init(config);
  17. log("INICIANDO SERVLET");
  18. }
  19. public void destroy() {
  20. super.destroy();
  21. log("DESTRUINDO SERVLET");
  22. }
  23. protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. PrintWriter out = response.getWriter();
  25. String nome = request.getParameter("nome");
  26. String email = request.getParameter("email");
  27. String endereco = request.getParameter("endereco");
  28. ContatoDao dao = new ContatoDao();
  29. Contato contato = new Contato();
  30. contato.setNome(nome);
  31. contato.setEmail(email);
  32. contato.setEndereco(endereco);
  33. dao.addContato(contato);
  34. //RequestDispatcher rd = request.getRequestDispatcher("/PrimeiroJsp.jsp");
  35. //rd.forward(request, response);
  36. }
  37. }
  38.  
  39. package pacote;
  40.  
  41.  
  42. import java.sql.Connection;
  43. import java.sql.DriverManager;
  44. import java.sql.SQLException;
  45.  
  46.  
  47. public class ConnectionFactory {
  48. static String url = "jdbc:oracle:thin:@localhost:1521:XE";
  49. static String user = "system";
  50. static String password = "1234";
  51. public Connection getConnection() {
  52. try {
  53. return DriverManager.getConnection(url, user, password);
  54. }
  55. catch(SQLException e) {
  56. throw new RuntimeException(e);
  57. }
  58. }
  59.  
  60. public void closeConnection(Connection connection) {
  61. try {
  62. connection.close();
  63. }
  64. catch(SQLException e) {
  65. throw new RuntimeException(e);
  66. }
  67. }
  68. }
  69.  
  70. package pacote;
  71.  
  72.  
  73. import pacote.Contato;
  74. import pacote.ConnectionFactory;
  75. import java.sql.Connection;
  76. import java.sql.PreparedStatement;
  77. import java.sql.ResultSet;
  78. import java.sql.SQLException;
  79. import java.util.ArrayList;
  80. import java.util.List;
  81.  
  82.  
  83. public class ContatoDao {
  84. Connection connection = null;
  85. ConnectionFactory cf = new ConnectionFactory();
  86.  
  87. public void addContato(Contato contato) {
  88. connection = cf.getConnection();
  89. String sql = "INSERT INTO CONTATO VALUES (?,?,?)";
  90. try {
  91. PreparedStatement stmt = connection.prepareStatement(sql);
  92. stmt.setString(1, contato.getNome());
  93. stmt.setString(2, contato.getEmail());
  94. stmt.setString(3, contato.getEndereco());
  95. stmt.execute();
  96. stmt.close();
  97. }
  98. catch(SQLException e) {
  99. throw new RuntimeException(e);
  100. }
  101. finally {
  102. cf.closeConnection(connection);
  103. }
  104. }
  105.  
  106. public void delContato(Contato contato) {
  107. try {
  108. connection = new ConnectionFactory().getConnection();
  109. String sql = "DELETE FROM CONTATO WHERE NOME = ?";
  110. PreparedStatement stmt = connection.prepareStatement(sql);
  111. stmt.setString(1, contato.getNome());
  112. }
  113. catch(SQLException e) {
  114. throw new RuntimeException(e);
  115. }
  116. finally {
  117. cf.closeConnection(connection);
  118. }
  119. }
  120.  
  121. public void attContato(Contato contato) {
  122. try {
  123. connection = new ConnectionFactory().getConnection();
  124. String sql = "UPDATE CONTATO SET NOME = ?, EMAIL = ?, ENDERECO = ? WHERE NOME = ?";
  125. PreparedStatement stmt = connection.prepareStatement(sql);
  126. stmt.setString(1, contato.getNome());
  127. stmt.setString(2, contato.getEmail());
  128. stmt.setString(3, contato.getEndereco());
  129. stmt.setString(4, contato.getNome());
  130. }
  131. catch(SQLException e) {
  132. throw new RuntimeException(e);
  133. }
  134. finally {
  135. cf.closeConnection(connection);
  136. }
  137. }
  138.  
  139. public List<Contato> getLista() {
  140. List<Contato> contatos = new ArrayList<>();
  141. try {
  142. connection = new ConnectionFactory().getConnection();
  143. String sql = "SELECT * FROM CONTATO WHERE NOME = ?";
  144. PreparedStatement stmt = connection.prepareStatement(sql);
  145. ResultSet rs = stmt.executeQuery();
  146. Contato contato = new Contato();
  147. while(rs.next()) {
  148. contato.setNome(rs.getString("nome"));
  149. contato.setEndereco(rs.getString("endereco"));
  150. contato.setEmail(rs.getString("email"));
  151. stmt.execute();
  152. contatos.add(contato);
  153. }
  154. stmt.close();
  155. rs.close();
  156. }
  157. catch(SQLException e) {
  158. throw new RuntimeException(e);
  159. }
  160. finally {
  161. cf.closeConnection(connection);
  162. return contatos;
  163. }
  164.  
  165. }
  166. }
Add Comment
Please, Sign In to add comment