title: Tomcat Servlet and MySQL
body: <p>I am using XAMPP on windows 8. What i want to do is to access a MySQL Database from a Servlet.</p>
  
  <p>I have included MySQL Connector J jar file to the /xampp/tomcat/lib directory and below is my code that i want to compile and run</p>
  
  <pre><code>import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import com.mysql.jdbc.Driver;
  
  public class Exercise7 extends HttpServlet {
  
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws IOException, ServletException
  {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("&lt;html&gt;");
      out.println("&lt;head&gt;&lt;title&gt;Servlet - MySQL&lt;/title&gt;&lt;/head&gt;");
      out.println("&lt;body&gt;");
  
      Connection con = null;
      Statement st = null;
      ResultSet rs = null;
  
      String url = "jdbc:mysql://localhost:3306/test";
      String user = "root";
      String password = "";
  
      try {
          con = DriverManager.getConnection(url, user, password);
          st = con.createStatement();
          rs = st.executeQuery("SELECT VERSION()");
  
          if (rs.next()) {
              out.println(rs.getString(1));
          }
  
      } catch (SQLException ex) {
          /*Logger lgr = Logger.getLogger(Version.class.getName());
          lgr.log(Level.SEVERE, ex.getMessage(), ex);*/
          out.println("Can't connect");
  
      } finally {
          try {
              if (rs != null) {
                  rs.close();
              }
              if (st != null) {
                  st.close();
              }
              if (con != null) {
                  con.close();
              }
  
          } catch (SQLException ex) {
              /*Logger lgr = Logger.getLogger(Version.class.getName());
              lgr.log(Level.WARNING, ex.getMessage(), ex);*/
              out.println("Can't connect");
          }
      }
      out.println("&lt;/body&gt;&lt;/html&gt;");
  }
  }
  </code></pre>
  
  <p>When i access the page I see "can't connect" as result and that clearly means that there is a problem with connection between servlet and mysql, so can anyone help me with this.</p>