title: JDBC to MySQL via NetBeans in Java Web Application
body: <p>I am trying to establish a connection with the database ( MySQL) in this servlet in the <code>doPost()</code> method. I have no idea where the problem is. Can I establish the connection within this servlet or do I need to make a new Java class?</p>
  
  <pre><code>package com.EventListing.servlets;
  
  import java.io.IOException;
  import java.sql.Connection;
  import java.io.PrintWriter;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  public class ViewEventListing extends HttpServlet {
  
  
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  
      } 
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  
          PrintWriter out = response.getWriter();
          String eventTitle= request.getParameter("eventTitle");
          String location = request.getParameter("location");
  
          out.println("Welcome "+ eventTitle+"! The Email Address "+location+" has been "
                    + "added to our mailing list.");
  
  
           try {
               Class.forName("com.mysql.jdbc.Driver");
               out.println("Driver Loading Done!");
           } catch(ClassNotFoundException e) {
               e.printStackTrace();
           }
           String connURL = "jdbc:mysql://localhost/eventlistings" ;
           String user = "root";
           String pass = "sheridan";
  
  
              Connection conn ;;
          try {
               conn =  DriverManager.getConnection(connURL, user, pass);
          } catch (SQLException ex) {
              Logger.getLogger(ViewEventListing.class.getName()).log(Level.SEVERE, null, ex);
          }
          out.println("Connection Done!");
  
  
          String sql = "INSERT INTO eventlistings "+"(Title, Location)" + 
      "VALUES('simran', 'Brampton');";
  
  
            PreparedStatement ps = conn.prepareStatement();
  
            out.close();
      }
  
      /** 
       * Returns a short description of the servlet.
       * @return a String containing servlet description
       */
      @Override
      public String getServletInfo() {
          return "Short description";
      }// &lt;/editor-fold&gt;
  
  }
  </code></pre>
  
  <p>This is the servlet using which I want to fetch some data from MySQL but I could not even connect to the database. Where I am trying to use the Prepared Statement that is, preparedStatement ps, I get an error.</p>