title: How can I choose a mysql table to insert data according to radio button? What's wrong in my jsp/java code?Why radio button don't work?
body: <p>I have two tables in mysql database, one for student and one for teacher. Both tables have the same columns.</p>
  
  <p>The code below inserts data only to the teacher's table. If user selects teacher's radio button, then the sign up data should be stored in teacher's table. If the user selects student's radio, then the sign up data should be stored in student's table.What's wrong in my jsp/jdbc code? What will be a solution for that?</p>
  
  <p>What wrong in this query.Why this query insert data in a only student table of mysql not in teacher2 table. <strong>either selected teacher/student radio button</strong>  </p>
  
  <p><strong>radio but don't work</strong>  </p>
  
  <pre><code>String gender = request.getParameter("gender");
  if (gender != null) {
  String table = gender.equals("teacher") ? "teacher2" : "student";
  // replace dots with your values
  String query = "INSERT INTO " + table + "(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) VALUES ('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";
  st.executeUpdate(query);}
  </code></pre>
  
  <p><strong>PROBLEM IS HERE ALSO</strong> </p>
  
  <p><code>st.executeUpdate(query);</code> // But data inserted in mysql </p>
  
  <p><a href="http://i.stack.imgur.com/ugn9B.jpg" rel="nofollow">enter image description here</a></p>
  
  <p><strong>index.jsp</strong></p>
  
  <pre><code>  &lt;form  method="GET " action="statement2.jsp" autocomplete="on"&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;input type="button" onclick="location.href='reg.jsp';" value="Sign Up" /&gt;
    &lt;/form&gt; 
  </code></pre>
  
  <p><strong>statement2.jsp</strong>  </p>
  
  <pre><code>  &lt;%
     String name=request.getParameter("first");
     String abc=request.getParameter("last");
     String cde=request.getParameter("user");
     String pass=request.getParameter("password");
     String confpass=request.getParameter("confirmpass");
     String emails=request.getParameter("email");
     String months=request.getParameter("month");
     String day=request.getParameter("day");
     String year=request.getParameter("year");
  
     String gender = request.getParameter("gender");
     if (gender != null) {
     String table = gender.equals("teacher") ? "teacher2" : "student";
  // replace dots with your values
     String query = "INSERT INTO " + table + "(firstname,lastname,username,password,confirmpassword,email,month,day,year,gender) VALUES ('"+name+"','"+abc+"','"+cde+"','"+pass+"','"+confpass+"','"+emails+"','"+months+"','"+day+"','"+year+"','"+gender+"')";
    st.executeUpdate(query);}     // problem is here
  
   %&gt; 
  </code></pre>
  
  <p><strong>reg.jsp</strong> // registeration page for sign up</p>
  
  <pre><code>     &lt;form  method="GET" action="statement2.jsp" autocomplete="on"&gt; 
         &lt;input id="firstnamesignup" name="first" required="required" type="text" placeholder="First" /&gt;
       &lt;input id="lastnamesignup" name="last" required="required" type="text" placeholder="Last" /&gt;
  
       &lt;select name="gender"&gt;
       &lt;option value="male"&gt;Male&lt;/option&gt;
       &lt;option value="female"&gt;Female&lt;/option&gt;
       &lt;/select&gt;      
       &lt;/form&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>db conn.jsp</strong>   //this file is 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;%@ 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>