Hi, Following is my code:
<%@page import="java.sql.*"%> <h1>example.jsp</h1> <table border="1"> <tr> <td></td> <th>domainName</th> </tr> <% try{ Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/entity","root", "rahul"); ResultSet rs = null; Statement st=null; st=conn.createStatement(); rs = st.executeQuery("select * from domain where categoryId=1"); while(rs.next()){ %> <tr> <td> <input type="checkbox" name="check" value=<%= rs.getString("domainId") %> onClick="if (this.checked) {showState(this.value)()}" value="ON"> </td> <td><%= rs.getString("domainName") %></td> </tr> <% } }catch(SQLException e){ System.out.println(e.getMessage()); } %> <tr> <td> <input type=submit value="submit"> </td> </tr> </table>
I want to display records, when i click on submit button or any dumy button like submit.
for your info. In above code, it contains,
"domain" table containg three columns.
domainId--as number domainName--as string categoryId--as number
Plese, help me.
Hi Friend,
We have made changes in the code:
1)checkboxExample.jsp:
<%@page import="java.sql.*"%> <head> <script> var xmlHttp; var arr= new Array() ; Array.prototype.clean = function(deleteValue) { for (var i = 0; i < this.length; i++) { if (this[i] == deleteValue) { this.splice(i, 1); i--; } } return this; }; function showState(){ var total="" for(var i=0; i < document.form.check.length; i++){ if(document.form.check[i].checked) arr[i]=document.form.check[i].value; } arr.clean(undefined); if(typeof XMLHttpRequest != "undefined"){ xmlHttp= new XMLHttpRequest(); } else if (window.ActiveXObject){ xmlHttp= new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlHttp==null){ alert ("Browser does not support XMLHTTP Request") return } var url= "gettable.jsp"; url += "?val=" +arr; xmlHttp.onreadystatechange = stateChange; xmlHttp.open("GET", url, true); xmlHttp.send(null); } function stateChange(){ if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ document.getElementById("sel").innerHTML=xmlHttp.responseText; } } </script> </head> <form name="form"> <table border="1"> <tr><td></td> <th>Id</th> <th>Name</th> <th>Address</th> </tr> <% try{ Connection conn = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root"); ResultSet rs = null; Statement st=null; st=conn.createStatement(); rs = st.executeQuery("select * from employee"); while(rs.next()){ %> <tr><td><input type="checkbox" name="check" value=<%= rs.getString("empid") %> ></td> <td><%= rs.getString("empid") %></td> <td><%= rs.getString("name") %></td> <td><%= rs.getString("address") %></td> </tr><% } }catch(SQLException e){ System.out.println(e.getMessage()); } %> </table> <input type="button" value="submit" onClick="showState();"> </form> <br> <br> Selected Data<br> <div id='sel'> </div>
2) gettable.jsp:
<%@page language="java" import ="java.sql.*" %> <% String val[] = request.getParameterValues("val"); String st=""; for(int i=0;i<val.length;i++){ st+=val[i]+""; } String str[]=st.split(","); for(int i=0;i<str.length;i++){ System.out.println(str[i]); } ResultSet rs = null; String buffer=""; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); Statement stmt = con.createStatement(); for(int i=0;i<str.length;i++){ String ide=str[i]; int id=Integer.parseInt(ide); rs = stmt.executeQuery( "SELECT * FROM employee WHERE empid='"+id+"'"); while(rs.next()){ buffer=buffer+"<table border><tr><td>"+rs.getString("empid")+"</td><td>"+rs.getString("name")+"</td><td>"+rs.getString("address")+"</td></tr></table>"; } } response.getWriter().println(buffer); %>
Thanks
Thank you Deepak for your valuable reply...........:-) it really works
But,I want some more help from you.
In my program , i have given to link to different jsp pages. Suppose it contains two links with two different jsp pages.
Both jsp pages contains differents database tables, n it contains same checkboxes.
So, from your above code, i can display selected checkbox on same page. It works...
But when i go to another page link containing same format, it does not display previous page's selected checkboxes.
So, i want such output, which can show previous pages selected checkbox as well as i aslo include next page's selected checkbox in display list on same pages respectively.( i don't know exactly, but can we store it in session and in collection so that we can display all selected checkbox)
Please,please help me... waiting for your reply
Thank you Deepak for your valuable reply...........:-) it really works
But,I want some more help from you.
In my program , i have given to link to different jsp pages. Suppose it contains two links with two different jsp pages.
Both jsp pages contains differents database tables, n it contains same checkboxes.
So, from your above code, i can display selected checkbox on same page. It works...
But when i go to another page link containing same format, it does not display previous page's selected checkboxes.
So, i want such output, which can show previous pages selected checkbox as well as i aslo include next page's selected checkbox in display list on same pages respectively.( i don't know exactly, but can we store it in session and in collection so that we can display all selected checkbox)
Please,please help me... waiting for your reply
Hey please, help me.. i am new in java and jsp
please, send me modified code
waiting your reply