Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ControllerLivros() {
- super();
- // TODO Auto-generated constructor stub
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- //doGet(request, response);
- request.setCharacterEncoding("UTF-8");
- String nomeAutor = request.getParameter("nomeAutor");
- String nomeLivro = request.getParameter("nomeLivro");
- String nomeGenero = request.getParameter("nomeGenero");
- if(nomeAutor == null || nomeAutor.trim().equals("") || nomeAutor.trim().equals("null")) {
- request.setAttribute("autorNull", "O autor não pode ser em branco:");
- request.getRequestDispatcher("Livros.jsp").forward(request, response);
- }
- if(nomeLivro == null || nomeLivro.trim().equals("") || nomeLivro.trim().equals("null")) {
- request.setAttribute("livroNull", "O nome do livro não pode ser em branco:");
- request.getRequestDispatcher("Livros.jsp").forward(request, response);
- }
- if(nomeGenero == null) {
- request.setAttribute("generoNull", "O genero não foi escolhido:");
- request.getRequestDispatcher("Livros.jsp").forward(request, response);
- }
- Livros livro = new Livros(1, nomeLivro, nomeGenero, nomeAutor);
- insertLivros.insertLivro(livro);
- PrintWriter teste = response.getWriter();
- request.setAttribute("nomeAutor", nomeAutor);
- request.setAttribute("nomeLivro", nomeLivro);
- }
- public class insertLivros {
- public static void insertLivro(Livros livro) {
- Connection connection = null;
- PreparedStatement pstmt = null;
- try {
- String query = null;
- query = "insert into livros(nomelivro, nomegenero, nomeautor) VALUES(?, ?, ?)";
- //Statement statement = null;
- connection = Conexao.getConexao();
- pstmt = connection.prepareStatement(query);
- pstmt.setString(1, livro.getNomeLivro());
- pstmt.setString(2, livro.getNomeGenero());
- pstmt.setString(3, livro.getNomeAutor());
- pstmt.executeUpdate();
- connection.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public class Conexao {
- public static java.sql.Connection getConexao(){
- Connection connection = null;
- String driver = "com.mysql.jdbc.Driver";
- try {
- Class.forName(driver);
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- String user = "root";
- String senha = "guimazx33";
- String url = "jdbc:mysql://localhost/aula";
- try {
- connection = DriverManager.getConnection(url, user, senha);
- return connection;
- }catch (SQLException e) {
- System.out.println("nao conectou");
- }
- return connection;
- }
- }
- public class insertLivros {
- public static void insertLivro(Livros livro) {
- try {
- Connection connection = Conexao.getConexao();
- PreparedStatement pstmt = connection.prepareStatement("insert into livros(nomelivro, nomegenero, nomeautor) VALUES(?, ?, ?)");
- pstmt.setString(1, livro.getNomeLivro());
- pstmt.setString(2, livro.getNomeGenero());
- pstmt.setString(3, livro.getNomeAutor());
- pstmt.executeUpdate();
- } finally {
- connection.close();
- }
- }
- }
- public class Conexao {
- public static Connection getConexao(){
- Class.forName("com.mysql.jdbc.Driver"); //não sei se isto é necessário
- return DriverManager.getConnection("root", "guimazx33", "jdbc:mysql://localhost/aula");
- }
- }
Add Comment
Please, Sign In to add comment