5 Replies - 914 Views - Last Post: 30 December 2014 - 09:12 AM

#1 Imadev   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 14

problem to connect application in Java EE to database

Posted 29 December 2014 - 07:26 AM

hi guys,
i have a problem in connecting my web application in javaEE, when i run the servlet i have an error : java.lang.NullPointerException

and this is a screenshot for the error : Posted Image

the error is in this line : statement = connexion.createStatement();

please i need help

this the code where i try to connect to the database

public class login_servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    public login_servlet() {
        super();
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        //this.getServletContext().getRequestDispatcher( "/login.jsp" ).forward( request, response );
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        java.sql.Connection connexion = null;
        java.sql.Statement statement = null;
        ResultSet resultat = null;
        String userQuery="";
 
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
        }
 
        try {
            connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/Dlala", "IPLS", "password");
        } catch (SQLException e) {
            e.printStackTrace();
        }
 
        try {
            statement = connexion.createStatement();
 
            // Exécution de la requête
            resultat = statement.executeQuery("SELECT user FROM Client;");
 
            // Récupération des données
            while (resultat.next()) {
                userQuery = resultat.getString("user");
            }
        } catch (SQLException e) {
        } finally {
            // Fermeture de la connexion
            try {
                if (resultat != null)
                    resultat.close();
                if (statement != null)
                    statement.close();
                if (connexion != null)
                    connexion.close();
            } catch (SQLException ignore) {
            }
        }
        //************************************************//
 
        response.setContentType("text/html");   
        PrintWriter out = response.getWriter();   
 
        String n=request.getParameter("user");   
        String p=request.getParameter("pass");  
 
        HttpSession session = request.getSession(false); 
        if(session!=null) 
        session.setAttribute("name", n); 
 
        if( n.equals(userQuery)){   
            RequestDispatcher rd=request.getRequestDispatcher("home.jsp");   
            rd.forward(request,response);   
        }   
        else{  
            out.print("<p style=\"color:red\">Sorry username or password error</p>");   
            RequestDispatcher rd=request.getRequestDispatcher("login.jsp");   
            rd.include(request,response);   
        }
 
        out.close(); 
    }
}


please i need help

Is This A Good Question/Topic? 0
  • +

Replies To: problem to connect application in Java EE to database

#2 kathy025   User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 190
  • Joined: 19-December 14

Re: problem to connect application in Java EE to database

Posted 29 December 2014 - 10:57 AM

View PostImadev, on 29 December 2014 - 10:26 PM, said:

the error is in this line : statement = connexion.createStatement();

Have you checked if you're successfully creating a Connection object?
if(connexion == null) {
	System.out.println("connection is null");
}


See if it enters the if-block.

This post has been edited by kathy025: 29 December 2014 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 Imadev   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 14

Re: problem to connect application in Java EE to database

Posted 29 December 2014 - 11:01 AM

i will do it thanks
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12680
  • View blog
  • Posts: 45,861
  • Joined: 27-December 08

Re: problem to connect application in Java EE to database

Posted 29 December 2014 - 02:15 PM

*Moved to Java EE*
Was This Post Helpful? 0
  • +
  • -

#5 Imadev   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 14

Re: problem to connect application in Java EE to database

Posted 29 December 2014 - 05:42 PM

hi,
in fact i looked at the consol (which i completly forget) and i saw this other exception :
java.sql.SQLException: Access denied for user 'IPLS'@'localhost' (using password: YES)

i created the DB using phpmyadmin, i work on a virtual machin, user is IPLS and password is "password"

i executed this command on the sql editor :
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION;

but nothing has changed
Was This Post Helpful? 0
  • +
  • -

#6 Imadev   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 14

Re: problem to connect application in Java EE to database

Posted 30 December 2014 - 09:12 AM

i corrected that by modifying the file config.inc.php i put "root" as password and i can now access to PhpMyAdmin as root and not as "IPLS"
and i get connection from the application.
thanks a lot for helping :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1