Share on Google+Share on Google+

hamam
how to search for string using LIKE operator instead of integer?
1 Answer(s)      5 years and 10 months ago
Posted in : JSP-Interview Questions

dear sir, i have 3 jsp files, first "index.jsp", "dbtable.jsp", and "table2.jsp". what my code does is goes to the "index.jsp" file and let the user enters an ID to search for itemid, then it shows "dbtable.jsp" file with 3 columns of itemid, itemname, and description. the itemid is in hyperlink so when the user click on of them it'll go to the third file which is "table2.jsp" which will show the details of that hyperlink. note that "dbtable" and "table2" have relations in itemid. however, what i want to do is i want the user to search for the itemname instead of the itemid, and i want the itemname column to be in hyperlink not itemid. and do the same thing by clicking on the hyperlink on itemname column and show "table2" details. in other words, my "dbtable.jsp" query should be like this (select * from items where itemname LIKE '"+id). and my detail file "table2.jsp" should be (select * from itemdetails where item_id ='"+id"') or something like that. but as you see, first query needs integer and second one needs string. i hope you get the picture.

there are the codes:

***"index.jsp"***

    <html>
    <form method="post" action="dbtable.jsp">
    Enter id: <input type="text" name="id">
    <input type="submit" value="Search">
    </form>
    </html>

***"dbtable.jsp"***

    <[email protected] import="java.sql.*"%>
    <html>
    <table border="1">
    <%
    try{
    String v=request.getParameter("id");
    int id=Integer.parseInt(v);
    Class.forName("com.mysql.jdbc.Driver").newInstance();  
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("Select * from items where itemid="+id+"");
    while(rs.next()){
        %>
    <tr>
<td><a href="table2.jsp?id=<%=rs.getString("itemid")%>"><%=rs.getString("itemid")</a></td>
<td><%=rs.getString("item")%></td>
<td><%=rs.getString("description")%></td></tr>
    <%
    }
    }
    catch(Exception e){
        System.out.println(e);
    }
    %>
    </table>
    </html>

***"table2.jsp"***

    <[email protected] import="java.sql.*"%>
    <html>
    <table border="1">
    <%
    String v=request.getParameter("id");
    int id=Integer.parseInt(v);
    Class.forName("com.mysql.jdbc.Driver").newInstance();  
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("Select * from subinventory where itemid="+id+"");
    while(rs.next()){
        %>
    <tr>
<td><%=rs.getInt("itemid")%></td>
<td><%=rs.getInt("subinventoryid")%></td>
<td><%=rs.getInt("quantity")%></td></tr>
    <%
    }
    %>
    </table>
    </html>

Ads

View Answers

June 5, 2011 at 4:19 PM


modify "table2.jsp"

<[email protected] import="java.sql.*"%>
        <html>
        <table border="1">
        <%
        try{
        String v=request.getParameter("id");
        int id=Integer.parseInt(v);
        Class.forName("com.mysql.jdbc.Driver").newInstance();  
         Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("Select * from items where itemid="+id+"");
        while(rs.next()){
            %>
        <tr>
    <td><a href="table2.jsp?id=<%=rs.getString("itemid")%>"><%=rs.getString("itemid")%></a></td><td><%=rs.getString("item")%></td>
    <td><%=rs.getString("description")%></td></tr>
        <%
        }
        }
        catch(Exception e){
            System.out.println(e);
        }
        %>
        </table>
        </html>









Tutorials   
Java Spring Hibernate Struts Training why do not required application restart after jsp changes? How to use contentType Attribute in JSP? How to connect to MySQL in JSP? JSP Comment Types with example How to use ajax in jsp? How to access the database from JSP? how to show image as a link which path coming from database jsp netbeans javascript Logos on mail inbox. I want to update the multiple values of database depending on checkbox..? Please help me out.. jsp 18/04/2013 JSTL how to access element added via javascript dynamically using jsp code Electronic spreadsheets are useful in situation An E-mail protocol for storage JSP JSP JSP jsp servlets Exception: com.mysql.jdbc.Driver display data from database on selecting option of dropdownlist display data from database on selecting option of dropdownlist display data from database on selecting option of dropdownlist display data from database on selecting option of dropdownlist Jsp Query run command prompt from jsp program question for c++ please help me how to set up Netbeans for JSP 3 dropdown list from the database using JSP autocompletion in textfield fetching from database in jsp openning the pdf or doc file in same jsp EXT JS tt need coding JSp session time out how to display values from database into table using jsp how to set the class path variable in tomcat ? need help please online examination system mini project problem in writing coding. Please help me. How to read the body content of custom tag? need help...................please please help me jsp coding please. help me image upload Assertions I want to store the value of local variable into Global variable how to create customise shortcut keys in jsp?

Ads

 
Advertisement null

Ads