title: Inserting data into table using java
body: <p>I want to insert user related information into table(users),When i am creating the form i have used drop-down list.There are two tables(users and department) in my database.I want to show the name of all the departments of table department in the drop down list(so that the user selects that department name) and insert into table users. How should i do this?</p>
  
  <p>file name-c_user.jsp</p>
  
  <pre><code>&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;create&lt;/title&gt;
      &lt;link rel="stylesheet" type="text/css" href="style.css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;Create User&lt;/h1&gt;
      &lt;br&gt;
      &lt;h2&gt;&lt;a href='logout.jsp'&gt;Log out&lt;/a&gt;&lt;/h2&gt;
      &lt;br&gt;
      &lt;h3&gt;&lt;a href='success.jsp'&gt;Go Back&lt;/a&gt;&lt;/h3&gt;
      &lt;form method="post" action="registration.jsp"&gt;
          &lt;center&gt;
          &lt;table border="1" width="30%" cellpadding="3"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th colspan="2"&gt;Login Here&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;td&gt;User Name :&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="uname" value="" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;User Id :&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="uid" value="" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Department :&lt;/td&gt;
                      &lt;td&gt;&lt;select name="departments"&gt;
                              &lt;option&gt;&lt;/option&gt;
                              &lt;option&gt;&lt;/option&gt;
                          &lt;/select&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Email Id :&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="email" value="" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Mobile No. :&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="mobile" value="" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Password :&lt;/td&gt;
                      &lt;td&gt;&lt;input type="password" name="pass" value="" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;input type="submit" value="Login" /&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="reset" value="Reset" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
  
              &lt;/tbody&gt;
          &lt;/table&gt;
          &lt;/center&gt;
      &lt;/form&gt;
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>file name-registration.jsp</p>
  
  <pre><code>&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;%@ page import ="java.sql.*" %&gt;
  &lt;%
  String user = request.getParameter("uname");  
  String id = request.getParameter("uid");
  String email = request.getParameter("email");
  String mobile = request.getParameter("mobile");    
  String pwd = request.getParameter("pass");   
  String department="";
  
  
  Class.forName("com.mysql.jdbc.Driver");
  Connection con =    DriverManager.getConnection("jdbc:mysql://localhost:3306/login",
          "root", "root");
  Statement st = con.createStatement();
  //ResultSet rs;
  int i = st.executeUpdate("insert into users(uname, id, department, email,   mobile, pass) values ('" + user + "','" + id + "','" + department + "','" +   email + "','" + mobile + "','" + pwd + "')");
  if (i &gt; 0) {
      //session.setAttribute("userid", user);
      response.sendRedirect("welcome.jsp");
     // out.print("Registration Successfull!"+"&lt;a href='index.jsp'&gt;Go to   Login&lt;/a&gt;");
  } else {
      response.sendRedirect("c_user.jsp");
  }
  %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>