Today we are going to learn how to

Display combo box value/Database value on same JSP page

Here we are getting values from a combo box and check it with the database and display the values in the same jsp page.
First create the table


 create table Result(year varchar(20),class varchar(20),Subject varchar(20
),total_students varchar(20),passout_students varchar(20),marks_obtained_60_to_8
0varchar(20),marks_obtained_above_80 varchar(20),overall_result varchar(20));

Now in the jsp page itself we are displaying a combo box and a button when we press the button the corresponding class values is displayed in the below field.


   <%@page import="java.sql.*,java.io.*;"%>
      <tr>

      <td width="60"height="30">
   <form name="s" action="#" method="get">
      <select name='class_name'>

      <%

      for(int i=1;i<12;i++)

      {

      out.println("<option value="+i+">"+i+"</option>");

      }

      %>

      </td>

      <select name='from_year'>

      <%

      for(int i=2010;i>2000;i--)

      {

      out.println("<option value="+i+">"+i+"</option>");

      }

      %>

      </td>

      <td>

      <select name='to_year'>

      <%

      for(int i=2010;i>2000;i--)

      {

      out.println("<option value="+i+">"+i+"</option>");

      }

      %>

      </td>

      <td>

      <input type="submit" name="submit" value="Go" ></td>
  </form>
      </tr>

      <table align="center" border="1" align="center">

      <br>

      <tr bgcolor="#36559E" align="center">

      <td width="150"height="30"><font size="+1">Year</td></font>

      <td width="150"height="30"><font size="+1">Class</td></font>

      <td width="150"height="30"><font size="+1">Subject</td></font>

      <td width="150"height="30"><font size="+1">Total Students</td></font>

      <td width="150"height="30"><font size="+1">Passout Students</td></font>

      <td width="150"height="30"><font size="+1">Marks Obtained 60 To 80</td></font>

      <td width="150"height="30"><font size="+1">Marks Obtained over 80</td></font>

      <td width="150"height="30"><font size="+1">Overall Result</td></font>

  </tr>

      <%

      String submit=request.getParameter("submit");

      if(submit!=null && (submit.equals("Go")))

      {

      String class_name=request.getParameter("class_name");

     out.println("class_name is"+

      class_name);

      Connection con ;

      try {

    //  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;

       Class.forName("com.mysql.jdbc.Driver") ;

    //  con = DriverManager.getConnection("jdbc:odbc:sql;user=sa;password=1234");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
  out.println("First connection ok.");

out.println("Connection created");

      Statement st=con.createStatement();

      out.println("st going to execute");

      String query="select year, class, Subject, total_students, passout_students, 

marks_obtained_60_to_80, marks_obtained_above_80, overall_result from Result where 

class='"+class_name+"' ";

      out.println("now data are selecting");

      ResultSet rs=st.executeQuery(query);

      System.out.println("now data in rs.....");

      out.println("now going to rs block............");

      while(rs.next())

      {out.println("now in rs block............");

      %>

      <tr align="center">

      <td width="150"height="30"><%out.println(rs.getString(1));%>

      <td width="150"height="30"><%out.println(rs.getString(2));%>

      <td width="150"height="30"><%out.println(rs.getString(3));%>

      <td width="150"height="30"><%out.println(rs.getString(4));%>

      <td width="150"height="30"><%out.println(rs.getString(5));%>

      <td width="150"height="30"><%out.println(rs.getString(6));%>

      <td width="150"height="30"><%out.println(rs.getString(7));%>

      <td width="150"height="30"><%out.println(rs.getString(8));%>
 </tr>
  <%
 }
 rs.close();
 con.close();
   }

catch(Exception e){out.println(e);}
  }

      %>

Now you can able to get the same value from the database in same page,happy coding

Related Posts

  1. Display current time and date in your Jsp page
  2. compare current time with database retrieved time in jsp
  3. Create database and table from jsp page
  4. Finding/removing duplicate values from database using sql
  5. captcha project in jsp/servlet