title: Why this statement rs=st.executeQuery(query); is not excuting? How can I select only a table depend on input type=radio from mysql from two tables?
body: <p>Why is this query <code>rs=st.executeQuery(query)</code>; not executed to select a table from database?</p>
  
  <pre><code>  String gender = request.getParameter("gender");
    if (gender != null) {
    String table = gender.equals("teacher") ? "teacher2" : "student";
  
    String query ="select username,password from " +table+ " where username='"+name+"' AND password='"+abc+"'";
  
    rs=st.executeQuery(query);  //Why This statement having error
    }
  </code></pre>
  
  <p><strong>Mysql Query</strong> for this table <a href="http://i.stack.imgur.com/ehGsk.jpg" rel="nofollow">enter image description here</a>i think this query is wrong</p>
  
  <pre><code> "select username,password from "+table+" where username='"+name+"' AND password='"+abc+"'";
  </code></pre>
  
  <p>I have two tables one is for the teacher and one is for students both have all same columns with same data type.</p>
  
  <p>How can I select table from mysql database to get login. I have two tables one is for student and one is for teacher if user select teacher, radio button and enter username and password if user username and password equals to mysql database username and password he will get login same case is for student if he select student radio but and enter username and password if username, password equals to student table of mysql username, password he will get login.</p>
  
  <p><strong>Why this query is not executed to select a table</strong> </p>
  
  <p><code>rs=st.executeQuery(query);</code> </p>
  
  <p><strong>image of error coming</strong></p>
  
  <p><a href="http://i.stack.imgur.com/CSWSm.jpg" rel="nofollow">enter image description here</a>  //error</p>
  
  <p><strong>index.jsp</strong>    </p>
  
  <pre><code> &lt;form  method="GET " action="statement.jsp" autocomplete="on"&gt; 
  
   &lt;input id="username" name="username" required="required" type="text" placeholder="Username"/&gt;
   &lt;input id="password" name="password" required="required" type="password" placeholder="Password" /&gt; 
  
   &lt;input type="radio" name="gender" value="teacher" checked/&gt; Teacher
   &lt;input type="radio" name="gender" value="Student"/&gt;Student
  
   &lt;input type="submit" value="Login" /&gt; 
   &lt;/form&gt; 
  </code></pre>
  
  <p><strong>statement.jsp</strong></p>
  
  <pre><code> &lt;%@page import="java.sql.DriverManager"%&gt;
   &lt;%@page import="java.sql.ResultSet"%&gt;
   &lt;%@page import="com.mysql.jdbc.Statement"%&gt;
   &lt;%@page import="com.mysql.jdbc.Connection"%&gt;
   &lt;%@page  import=" java.sql.SQLException" %&gt;
   &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  
   &lt;%@include file="db conn.jsp" %&gt;
   &lt;% 
   String name=request.getParameter("username");
   String abc=request.getParameter("password");       
  
   String gender = request.getParameter("gender");
  
   if (gender != null) {
   String table = gender.equals("teacher") ? "teacher2" : "student";
  
   String query ="select username,password from "+table+" where username='"+name+"' AND password='"+abc+"'";
  
     rs=st.executeQuery(query);  //Why This statement having error
    }
      if(rs.next()) 
     {
      response.sendRedirect("main.jsp");
     }
     else
     {
      response.sendRedirect("index.jsp");
      }
    %&gt;
  </code></pre>
  
  <p><strong>db conn.jsp</strong>  // For database connection</p>
  
  <pre><code>  &lt;%@page import="com.mysql.jdbc.Connection"%&gt;
    &lt;%@page import="com.mysql.jdbc.Statement"%&gt;
    &lt;%@page import="java.sql.ResultSet"%&gt;
    &lt;%@page import="java.sql.DriverManager"%&gt;
    &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;JSP Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt;Hello World!&lt;/h1&gt;
  
    &lt;%@ page import ="java.sql.*" %&gt;
    &lt;%
      Connection c1 = null;
      Statement st = null;
      ResultSet rs = null;
  
      Class.forName("com.mysql.jdbc.Driver");
      c1 = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher","root", "abcde");
      {
      System.out.println("Couldn't find the driver!");
      System.out.println("Couldn't connect: print out a stack trace and exit.");
      System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
  
      st = (Statement) c1.createStatement();
      System.out.println("Statement Created Successfully");
      {
      System.out.println("We got an exception while creating a statement:" + "that probably means we're no longer connected.");
       }
      if (c1!= null) {
      System. out.println("Hooray! We connected to the database!");
       } else {
      System.out.println("We should never get here.");
      }}
    %&gt;
  </code></pre>
  
  <p><strong>error</strong></p>
  
  <pre><code> HTTP Status 500 -
   type Exception report
   message
   description The server encountered an internal error () that prevented it from fulfilling this request.
   exception
  
   org.apache.jasper.JasperException: Exception in JSP: /statement.jsp:29
  
   26:    String table = gender.equals("teacher") ? "teacher2" : "student";
   27:    // replace dots with your values
   28:     String query ="select * from " +table+ "where username='"+name+"' AND password='"+abc+"'";
   29:     st.executeQuery(query); 
   30:     
  </code></pre>
  
  <p><strong>Stacktrace:</strong></p>
  
  <p><code>org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:729)</code></p>