Guest User

Untitled

a guest
Nov 19th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. public ControllerLivros() {
  2. super();
  3. // TODO Auto-generated constructor stub
  4. }
  5.  
  6. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  7. // TODO Auto-generated method stub
  8. //doGet(request, response);
  9. request.setCharacterEncoding("UTF-8");
  10. String nomeAutor = request.getParameter("nomeAutor");
  11. String nomeLivro = request.getParameter("nomeLivro");
  12. String nomeGenero = request.getParameter("nomeGenero");
  13.  
  14. if(nomeAutor == null || nomeAutor.trim().equals("") || nomeAutor.trim().equals("null")) {
  15. request.setAttribute("autorNull", "O autor não pode ser em branco:");
  16. request.getRequestDispatcher("Livros.jsp").forward(request, response);
  17. }
  18. if(nomeLivro == null || nomeLivro.trim().equals("") || nomeLivro.trim().equals("null")) {
  19. request.setAttribute("livroNull", "O nome do livro não pode ser em branco:");
  20. request.getRequestDispatcher("Livros.jsp").forward(request, response);
  21. }
  22. if(nomeGenero == null) {
  23. request.setAttribute("generoNull", "O genero não foi escolhido:");
  24. request.getRequestDispatcher("Livros.jsp").forward(request, response);
  25. }
  26.  
  27.  
  28. Livros livro = new Livros(1, nomeLivro, nomeGenero, nomeAutor);
  29.  
  30. insertLivros.insertLivro(livro);
  31.  
  32.  
  33. PrintWriter teste = response.getWriter();
  34.  
  35.  
  36. request.setAttribute("nomeAutor", nomeAutor);
  37. request.setAttribute("nomeLivro", nomeLivro);
  38.  
  39.  
  40. }
  41.  
  42. public class insertLivros {
  43.  
  44. public static void insertLivro(Livros livro) {
  45.  
  46.  
  47. Connection connection = null;
  48. PreparedStatement pstmt = null;
  49.  
  50. try {
  51. String query = null;
  52. query = "insert into livros(nomelivro, nomegenero, nomeautor) VALUES(?, ?, ?)";
  53.  
  54. //Statement statement = null;
  55.  
  56. connection = Conexao.getConexao();
  57. pstmt = connection.prepareStatement(query);
  58. pstmt.setString(1, livro.getNomeLivro());
  59. pstmt.setString(2, livro.getNomeGenero());
  60. pstmt.setString(3, livro.getNomeAutor());
  61.  
  62.  
  63. pstmt.executeUpdate();
  64. connection.close();
  65. } catch (SQLException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69.  
  70. }
  71.  
  72. }
  73.  
  74. public class Conexao {
  75.  
  76. public static java.sql.Connection getConexao(){
  77.  
  78. Connection connection = null;
  79.  
  80. String driver = "com.mysql.jdbc.Driver";
  81. try {
  82. Class.forName(driver);
  83. } catch (ClassNotFoundException e1) {
  84. // TODO Auto-generated catch block
  85. e1.printStackTrace();
  86. }
  87.  
  88.  
  89. String user = "root";
  90. String senha = "guimazx33";
  91. String url = "jdbc:mysql://localhost/aula";
  92.  
  93. try {
  94. connection = DriverManager.getConnection(url, user, senha);
  95. return connection;
  96.  
  97. }catch (SQLException e) {
  98. System.out.println("nao conectou");
  99. }
  100. return connection;
  101. }
  102. }
Add Comment
Please, Sign In to add comment