Home > Enterprise >  How to use form values from a JSP file into <sql:query> tag in another JSP file?
How to use form values from a JSP file into <sql:query> tag in another JSP file?

Time:10-26

I get the below error: I am able to see the user input but the sql is not taking in the value?

The error is as below:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: SELECT SOURCE AS source FROM flyaway.flightdetails WHERE source= Delhi; : Unknown column 'Delhi' in 'where clause'

my index.jsp code are as below:


<form action="ValidationFilter" method="get">
    Enter the source: <input type="text" name="source"><br>
    Enter the destination: <input type="text" name="destination"><br>
    Enter departure date: <input type="date" name="departure"><br>
    <input type="submit" value="Submit">
</form>

my flightList.jsp are as follows:

<sql:setDataSource
        var="myDB"
        driver="com.mysql.cj.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/flyaway?useSSL=false"
        user="root" password="1Leanest!"
/>

<%
     String source = request.getParameter("source");
     String destination = request.getParameter("destination");
%>


<sql:query var="listItems" dataSource="${myDB}">
        SELECT * FROM flyaway.flightdetails
        WHERE SOURCE= <%=source %>;
</sql:query>

I wanted to filter from the database in SOURCE column based on the user input from the form, but i get error.

CodePudding user response:

You need to wrap the value in single quotes

WHERE SOURCE= '<%=source%>';

but having sql in a jsp is ugly. Better to have a proper servlet/preparedStatement etc.

  • Related