title: Unable to get connection to mysql database in netbeans java web application
body: <p>I am new to web development to java. I am writing this basic java web application that is going to store customer information into a database. I use the MVC-2 architecture. My Jsp sends a request to a servlet that in turn tries instatiates a bean an inserts that object into a database. 
  When i try to connect to the database (in debugging mode) the connection variable returns empty. So data cannot be inserted.</p>
  
  <p>This is the class that makes connections to the db</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package customer;
  
  import java.io.Serializable;
  import java.sql.Connection;
  import java.sql.DriverManager;
  
  /**
   *
   * @author 
   */
  public class DatabaseOperations implements Serializable 
  {
      private static Connection connection;
      public DatabaseOperations()
      {
          try
          {
              String username = "root";
              String password = "root";
              String url = "jdbc:mysql://localhost/test";
              Class.forName ("com.mysql.jdbc.Driver").newInstance();
              connection = DriverManager.getConnection (url, username, password);
  
              System.out.println("Database connection established");
          }
          catch(Exception e)
          {
  
          }
      }
  
      public static Connection getConnection()
      {
          return connection;
      }
  }
  </code></pre>
  
  <p>This is the method that adds a customer to the db</p>
  
  <pre><code>public void addCustomer(CustomerBean customer) throws SQLException {
          DatabaseOperations db = new DatabaseOperations();
          connection = DatabaseOperations.getConnection();
          statement = connection.createStatement();
  
          String query = "insert into customer (name, address, phone, email) "
                  + "values (" + customer.name + ","
                  + customer.address + ","
                  + customer.phone + ","
                  + customer.email + "," + ")";
  
          statement.executeUpdate(query);
      }
  </code></pre>
  
  <p>and finally this is the servlet where i call the method to add a customer</p>
  
  <pre><code>   /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package customer;
  
      import java.io.IOException;
      import java.io.PrintWriter;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import customer.CustomerBean;
      import javax.servlet.RequestDispatcher;
  
      /**
       *
       * @author 
       */
      public class CustomerServlet extends HttpServlet {
  
          /**
           * Processes requests for both HTTP
           * &lt;code&gt;GET&lt;/code&gt; and
           * &lt;code&gt;POST&lt;/code&gt; methods.
           *
           * @param request servlet request
           * @param response servlet response
           * @throws ServletException if a servlet-specific error occurs
           * @throws IOException if an I/O error occurs
           */
          protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              response.setContentType("text/html;charset=UTF-8");
              PrintWriter out = response.getWriter();
  
              CustomerBean customer = new CustomerBean();
              try {
                  out.println("tests");
  
                  customer.setName(request.getParameter("name"));
                  customer.setEmail(request.getParameter("email"));
                  customer.setAddress(request.getParameter("address"));
                  customer.setPhone(request.getParameter("phone"));
  
  /************** ADD CUSTOMER TO DB HERE***********************/
                  customer.addCustomer(customer);
  
                  request.setAttribute("cust", customer);
                  request.getRequestDispatcher("/index.jsp").forward(request, response);
              } 
              catch (Exception e) 
              {            
                  e.printStackTrace();
  
              }
          }
  </code></pre>