I want to display values from database into table based on condition in query, how to display that? For example i have some number of books in database but i want to display books based on either bookname or authorname, for this i created one jsp page with fields bookname and authorname and search button and also action for that in jsp, if i enter either bookname or authorname and click on search button the values from database based on the bookname or authorname entered must be display as view.action must be written in jsp only I tried but didnt get values.In servlets its working but not working in jsp
<%@ page language="java" %> <%@ page import="java.sql.*" %> <script> function change(){ var cid=document.getElementById("book").selectedIndex; var val = document.getElementById("book").options[cid].text; window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?id="+cid+"&&value="+val); } function extract(){ var ide=document.getElementById("info").selectedIndex; var bookname = document.getElementById("info").options[ide].text; window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?book="+bookname); } </script> <%! Connection conn = null; ResultSet rs =null; Statement st=null; String query=""; %> <% String value=request.getParameter("value"); Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root";, "root"); st = conn.createStatement(); rs = st.executeQuery("select * from book"); %> <select id="book" onchange="change();"> <option value="0">--Please Select--</option> <% while(rs.next()){ %> <option value="<%=rs.getString("books")%>"><%=rs.getString("books")%></option> <% if(rs.getString("books").equals(value)){%> <option value="<%=value%>" selected disabled><%=value%></option> <% } } %> </select> <select id="info" onchange="extract(this)"> <option value="0">--Please Select--</option> <% String id=request.getParameter("id"); rs=st.executeQuery("select * from bookInformation where bookid='"+id+"'"); while(rs.next()){ %> <option value="<%=rs.getString("id")%>" ><%=rs.getString("booknames")%></option> <% } %> </select> <% String book=request.getParameter("book"); String author=""; String price=""; rs=st.executeQuery("select * from bookInformation where booknames='"+book+"'"); while(rs.next()){ author=rs.getString("writer"); price=rs.getString("price"); } if((book!=null)&&(author!=null)&&(price!=null)){ %> <table > <tr><td>Book Name</td><td><input type="text" value="<%=book%>"></td></tr> <tr><td>Author</td><td><input type="text" value="<%=author%>"></td></tr> <tr><td>Price</td><td><input type="text" value="<%=price%>"></td></tr> </table> <% } %> </body> </html>
For the above code, we have used following database tables:
1)book
CREATE TABLE `book` ( `bookid` bigint(20) NOT NULL auto_increment, `books` varchar(255) default NULL, PRIMARY KEY (`bookid`) );
2)bookinformation
CREATE TABLE `bookinformation` ( `id` bigint(255) NOT NULL auto_increment, `bookid` int(255) default NULL, `booknames` varchar(255) default NULL, `writer` varchar(255) default NULL, `Price` double default NULL, PRIMARY KEY (`id`) );
For more information, visit the following link:
Ads