NullPointerException in connection with database

1

Anyway, I'm getting a nullpointerException so I have no clue how to deal with it, I read the net on and apparently the problem is in uninitialized variables and stuff like that, but I'm sure everything has been initialized here.

Servlet:

publicControllerLivros(){super();//TODOAuto-generatedconstructorstub}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstub//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);


}

class insert:

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();
    }

    }

}

connection class (I can not believe the error is here)

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;
        }
}
    
asked by anonymous 19.11.2017 / 16:24

1 answer

1

The error is not well in the code, but the code is wrong precisely because it hides the error. So it improves and at least treats the error properly. The connection failure will have to see why it occurs.

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");
    }
}

I put it in GitHub for future reference.

Obviously the error will have to be handled, but handling the error is different from hiding it, which is what is happening in the code. I will not try to treat because I do not know what the goal is. It could even catch an exception to launch a more meaningful one, or it could turn into error code , which many people do not like, especially in Java, or would only deal with the error in the class that takes care of the interaction with the user.

My codes usually have one or another catch in the whole application, I do not know why I see people codes a catch in each method, it does not make sense.

To tell you the truth I have serious doubts about whether the Conexao class has any function.

    
19.11.2017 / 17:42