Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:38:46+00:00 2026-06-17T21:38:46+00:00

i am new to jsp i try to print a my database value in

  • 0

i am new to jsp i try to print a my database value in my jsp page,
the below program work fine while i directly print the s1=”january” and s2=”2012″ value,both datatype VARCHAR

String QueryString = "SELECT reading,totalcost,paiddate,receiptnumber FROM userseven WHERE readingmonth = '"+s1+"' AND readingyear= '"+s2+"'";

if i try to print my request.getparameter(“t1”)( t1 contain january ) value request.getparameter(“t2”);(t2 contain 2011)

it won’t print any think,

String QueryString = "SELECT reading,totalcost,paiddate,receiptnumber FROM userseven WHERE readingmonth = '"+s11+"' AND readingyear= '"+s22+"'";

           <body>
           <form action="yeardb.jsp">
           <table border="1" >
           <tr>
           <td>Select Year</td>
           <td>
           <select name="t1">
           <option value="2013">2013</option>
           <option value="2012">2012</option>
           <option value="2011">2011</option>
           <option value="2010">2010</option>
           </select>
           </td></tr><tr><td>
          <tr><td>Select Month:</td>
          <td>
          <select name="t2"> 
          <option value="january">JANUARY</option>
          <option value="march">March</option>
          <option value="may">May</option>
           <option value="july">JULY</option>
           <option value="aug">AUGUEST</option>
            <option value="oct">OCTOBER</option>
            <option value="dec">DECEMBER</option>
            </select></td></tr><tr><td>
           <input type= "submit" value="submit" >
          </td></tr>
          </table>
           </form>
          </body>



          yeardb.jsp
       ********
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">

       <%@page import="com.mysql.jdbc.Driver"%>
       <%@ page import="java.sql.*" %>
       <%@ page import="java.io.*" %> 

      <html>
      <head>
     <title>display data from the table using jsp</title>
      </head>
       <body>
     <h1>welcome</h1>


     <%
     String s11=request.getParameter("t1");
     String s22=request.getParameter("t2");
     String s1="january";
     String s2="2011";
     out.print("i am String"+s11);
     out.print("i am String"+s22);
      out.print("outside try");
     try {
      out.print("inside try");
      String connectionURL = "jdbc:mysql://localhost:3306/horizontal";

      Connection connection = null;

      Statement statement = null;


      ResultSet rs = null;


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

      connection = DriverManager.getConnection(connectionURL, "root", "root");
      statement = connection.createStatement();
      out.print("before query");

    String QueryString = "SELECT reading,totalcost,paiddate,receiptnumber FROM userseven WHERE readingmonth = '"+s1+"' AND readingyear= '"+s2+"'";


      out.print("after query");

      rs = statement.executeQuery(QueryString);

%>

  <TABLE cellpadding="15" border="1" style="background-color: #ffffcc;">
  <%
   out.print("outside while");
   while (rs.next()) {
    out.print("inside while");
   %>
      <TR>

      <TD><%=rs.getString(1)%></TD>
      <TD><%=rs.getString(2)%></TD>
      <TD><%=rs.getString(3)%></TD>
      <TD><%=rs.getString(4)%></TD>

     </TR>
    <%   }    %>
    <%
// close all the connections.
    rs.close();
    statement.close();
   connection.close();
      }      catch (Exception ex) {
%>
</font>
<font size="+3" color="red"></b>
    <%
            out.println("Unable to connect to database.");
        }
    %>
    </TABLE><TABLE>
      <TR>

        <button type="submit"><-- back</button></TD>
       </TR>
     </TABLE>
    </font>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:38:47+00:00Added an answer on June 17, 2026 at 9:38 pm

    I looked a little deeper at your code, you are setting:

    String s22=request.getParameter("t2");
    

    t2 contains the month, but your s2 variable contains the year. Swap those around and you should be fine.

     String s11=request.getParameter("t1");  <- This is your year
     String s22=request.getParameter("t2");  <- This is your month
     ....
     String QueryString = "SELECT reading,totalcost,paiddate,receiptnumber FROM userseven WHERE readingmonth = '"+s22+"' AND readingyear= '"+s11+"'";
    

    With that said, you should really consider using parameterized queries (this is vulnerable to SQL Injection) — look at using PreparedStatement instead:

    String QueryString = "SELECT reading,totalcost,paiddate,receiptnumber FROM userseven WHERE readingmonth = ? AND readingyear= ?";
    PreparedStatement ps = connection.prepareStatement(QueryString )
    ps.setString(1, s22);
    ps.setString(2, s11);
    rs = ps.executeQuery();
    

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to create a new file inside a JSP and try to save
I am new to JSP/Servlets/MVC and am writing a JSP page (using Servlets and
I am new to do this sorting technique in jsp page of dynamic content.
I am new to JSP and I am trying to write the below code:
I'm getting the following error when I try to run a jsp page with
Hi I'm new in java/jsp developing. I'm trying to connect to database depending on
I am very new to JSP. I need to find in which file the
i am new to jsp and just built my first application. I am following
I am new to JSP and generating a form with a text area. Is
i am new in JSP,i have some problem with the following code : <%@

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.