title: Servlet and JSP connection in eclipse EE IDE with MYSQL database
body: <p>I am new to java.I am creating dynamic web Application in eclipse EE IDE with MYSQL database. I want to connect my database to app so far I have created JSP page for view. Below is my JSP code and Servlet for connection. I am not able to connect to database with this. My JSP page work fine. But I think problem is with Servlet. And also advice to I need to made the two java file one for Servlet and other for getting data from JSP page.
  Thanks in advance.</p>
  
  <p><strong>Servlet</strong></p>
  
  <pre><code>import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  
  public class NewServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  
   //Get user_name and pass_word from the JSP page
  
  
  String toolfirst=request.getParameter("tname1");
  String toolsecond=request.getParameter("tname2");
  String toolvalue=request.getParameter("tvalue");
  
  //Print the above got values in console
  
  System.out.println("The username is" +toolfirst);
  
  
  
  
  
  
  //Setting connection Parameters
  
  String connectionparams=”jdbc:mysql://localhost:3306/tool”;
  
  //database name
  
  String db=”tool”;      
  
  //Mysql user name and password   whichever  you have given during installation
  
  String uname=”root”                   
  String psword=””                 
  
  //Declaring classes required for Database support
  
  Connection connection=null;
  ResultSet rs;
  try {
  // Loading the available driver for a Database communication
  
  Class.forName("org.gjt.mm.mysql.Driver");
  
  //Creating a connection to the required database
  
  connection = DriverManager.getConnection(jdbc:mysql://localhost:3306/tool, root, );
  
  //Add the data into the database
  
  String sql = "insert into usertable values (?,?)";
  
  
  PreparedStatement prep =
  connection.prepareStatement(sql);
  
  //Setting the values which we got from JSP form
  
  prep.setString(1, tname1);
  prep.setString(2, tname2);
  prep.executeUpdate();
  prep.close();
    }catch(Exception E){
  
  //Any Exceptions will be caught here
  
  System.out.println(“The error is==”+E.getMessage());
  
  }
  finally{
  
  //After the entire execution this block will execute and the connection with database gets closed
  
  connection.close();
  
  
  
      }
  }
  </code></pre>
  
  <p><strong>JSP</strong></p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
   &lt;script type="text/javascript"&gt;
      $(document).ready(function() {
          $("#add").click(function() {
            $('#mytable tbody&gt;tr:last').clone(true).insertAfter('#mytable tbody&gt;tr:last');
      $('#mytable tbody&gt;tr:last #name').val('');
      $("#mytable tbody&gt;tr:last").each(function() {this.reset();});
            return false;
          });
      });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="post" action="NewServlet"&gt;
  &lt;a  id="add" href="javascript:void(0)"&gt;Add another Credit card&lt;/a&gt;
    &lt;table id="mytable" width="300" border="1" cellspacing="0" cellpadding="2"&gt;
    &lt;tbody&gt;
        &lt;tr class="person"&gt;
        &lt;td&gt;&lt;input type="text" name="tname1" id="name" /&gt;&lt;/td&gt;
        &lt;td&gt;&lt;input type="button" value="name" /&gt;&lt;/td&gt;
        &lt;td&gt;&lt;select name="tvalue"&gt;
        &lt;option&gt;value1&lt;/option&gt;
        &lt;option&gt;value2&lt;/option&gt;&lt;/select&gt;
        &lt;td&gt;&lt;input type="text" name="tname2" id="name" /&gt;&lt;/td&gt;
            &lt;/tr&gt;
                &lt;/tbody&gt;
              &lt;/table&gt;
                &lt;input type="submit" value="submit" &gt;
    &lt;/form&gt;
     &lt;a href="logout.jsp"&gt;Logout&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>