How do I update a multiple row with checkbox - java

I want to update a particular column in my database with the word 'trash' but I need to first tick the values in check box. Then when u tick the checkbox, the system will only update the status field of the id's selected
public void updateNow(Messages message){
Query query = em.createNativeQuery("Update Messages set status = 'trash' WHERE id =:id");
query.setParameter("id", message.getId());
query.executeUpdate();
}
}
<%
//updating the filed
String[] arr = request.getParameterValues("content");
int sum=0;
if(request.getParameter("update")!=null){
try{
for (int i=0; i<arr.length; i++){
sl.updateNow(arr[i]);
sum++;
msg = "<script>alert('You have move message to trash')</script>";
}
}catch (Exception e){
msg="Please Do select an email before performing an action" + e.getMessage();
e.printStackTrace();
}
}
%>
<html>
<body>
<form action="" method="post">
<%
List inboxFetch = sl.allMessages(user, "inbox");
Iterator inboxIterate = inboxFetch.iterator();
while(inboxIterate.hasNext()){
Messages all = (Messages) inboxIterate.next();
%>
<input type="checkbox" value="<%=all.getId() %>" name="content">
<%
}
%>
<br />
<hr />
<button type="submit" name="update" class="btn btn-sm btn-danger">Update</button>
</form>
</body>
</html>
Please how do I achieve this? I have been on this code now for quite some hours seems I can't get through it

Related

Pagination issue with JSP

I am able to display a table when a user clicks a search button. But now I want to split the table into chunks of 20 rows where the user can click next or previous to go forward and backward through all of the data. I am not allowed to use JavaScript for this assignment. Only JSP, Java, HTML.
The two debugging out.print() calls are not showing up. A different page is being loaded after one of the buttons is clicked, but the two debugging out.print calls are not displaying any HTML. I have checked out How to know which button is clicked on jsp this post but had no luck.
<form method="GET">
<center>
<input type="submit" name="previous_table" value="Previous" />
<input type="submit" name="next_table" value="Next" />
</center>
</form>
</br>
<%
String val1 = request.getParameter("previous_table");
String val2 = request.getParameter("next_table");
try {
if ("Previous".equals(val1)) { // Get previous results
out.println("<h1>HELLO 1</h1>");
buildTable(rs, out, false);
}
else if ("Next".equals(val2)) { // Get next results
out.println("<h1>HELLO 2</h1>");
buildTable(rs, out, true);
}
} catch(Exception e) {
out.print("<center><h1>"+e.toString()+"</h1></center>");
}
%>
I also have a follow up question. If the user clicks next or previous button, will my current table on the page be overwritten by the new one? That is my intent but I don't know if it will work that way.
I WAS ABLE TO FIX IT BY DOING THIS:
<form method="POST">
<center>
<input type="submit" name="previous_table" value="Previous" />
<input type="submit" name="next_table" value="Next" />
</center>
</form>
</br>
<%
String val1 = request.getParameter("previous_table");
String val2 = request.getParameter("next_table");
you should add name with value for button after that you can get by parameter click value.
`<input type="hidden" name="myprevious" value="previous"/>
<input type="hidden" name="mynext" value="next" />
<%
String val1 = request.getParameter("myprevious");
String val2 = request.getParameter("mynext");
try {
if (val1 == "previous") { // Get previous results
out.println("<h1>HELLO 1</h1>");
buildTable(rs, out, false);
}
else if (val2 == "next") { // Go next results
out.println("<h1>HELLO 2</h1>");
buildTable(rs, out, true);
}
} catch(Exception e) {
out.print("<center><h1>"+e.toString()+"</h1></center>");
}
%>
`
I hope it will help you.
Thanks.
Try to use the subList() method of a List<>().
As explained here.
HOW TO IMPLEMENT IT ##
you can put an hidden input in your form to give you the last index for your list like :
<input type="hiden" value="${last_index_of_your_list + 1}" name="index">
Then in your servlet part you put like this :
int index = Interger.ParseInt(request.getParameter("index"));
if(index <= 0){
datalist = datalist(0, 19>datalist.size()? datalist.size() : 19);
}else{
if(clicked_on_next){
datalist = datalist(index, index+19>datalist.size()? datalist.size() : index+19 );
}else{
datalist = datalist(index - 40, index-20>datalist.size()? datalist.size() : index-20 );
}
}
You are using hidden fields but you need to use submit button for next and previous.
<input type="submit" name="myprevious" value="previous"/>
<input type="submit" name="mynext" value="next" />
Make sure both are define in form. when we submit form after that you will get button value in parameter.same my previous answer. because we can not get parameter value without submit form.
Thanks.

How to get input value from one jsp page to another jsp page?

I am trying to get a one-page input value to another page while clicking the order button.
while I will take a number of item value and click the order button, it will carry the value the order page. The page code is here,
<td><%=rs.getString("product_price")%></td>
<td> <input type="number" name="no_item" value="1" /></td>
<td class="text-center" width="250">
Order
Edit
Delete
</td>
The order page code is here,
<%
statement = connection.createStatement();
String u=request.getParameter("u");
String item_num =request.getParameter("no_item");
int num=Integer.parseInt(u);
String Data = "select * from products_tbl where id='"+num+"'";
rs = statement.executeQuery(Data);
String product_price;
while (rs.next()) {
%>
<input type="hidden" name="id" value='<%=rs.getString("id")%>'/>
<div class="form-group">
<h4 style="float:left; padding-right:8px;">Product Name:</h4> <h4> <%=rs.getString("product_name")%> </h4>
</div>
<div class="form-group">
<%
product_price = rs.getString("product_price"); int num1 = Integer.parseInt(product_price); %>
</div>
<%= item_num %>
<%= num1 %>
<%
}
%>
Onclick of order button add the input value in url query string. you have need to use java script in jsp page.
<script>
function order(page, id){
input_value = document.getElementById('no_item').value;
location.href= page+"?u="+id+"&no_item="+input_value;
}
</script>
Add the onclick function within order button.
<a onclick="order('order.jsp', '<%=rs.getString("id")%>')" class="btn btn-success">Order</a>
In order.jsp page you will get the input value.
request.getParameter("no_item");

Using jsp el to display a list of users from different groups

I am trying to display a list of members in a group using a accordion. I am using jsp,el and servlets for this purpose.
Here are two database table for understanding the problem
above are two tables -groups and group_members respectively ..I am accessing data from them and storing them in the two java objects and making a list of these two objects.
my servlet for these is as follows;;
//current user's user-name
String currentUser = request.getParameter("username");
String group_name=null;
String GetGroupInfo = "select * from groups where creator_username=?";
//get all the groups where the creator's user-name is currentUser
try {
List<Group> groups = new ArrayList<Group>();
List<GroupDetails> groupList = new ArrayList<GroupDetails>();
ps3 = currentCon.prepareStatement(GetGroupInfo);
ps3.setString(1,currentUser);
rs3 = ps3.executeQuery();
//set values for Group object
while(rs3.next())
{
Group groupObj = new Group();
groupObj.setGroup_id(rs3.getString("group_id"));
groupObj.setGroup_name(rs3.getString("group_name"));
groupObj.setGroup_description(rs3.getString("group_description"));
groupObj.setCreator_username(rs3.getString("creator_username"));
groupObj.setCreated_on(rs3.getString("created_on"));
groups.add(groupObj);
String query = "select * from group_members where creator_username=? and group_id=?";
ps = currentCon.prepareStatement(query);
ps.setString(1,currentUser);
ps.setString(2, groupObj.getGroup_id());
rs = ps.executeQuery();
while(rs.next())
{
GroupDetails groupInfo = new GroupDetails();
groupInfo.setIs_admin(rs.getString("is_admin"));
groupInfo.setAdded_on(rs.getString("added_on"));
groupInfo.setCreator_username(rs.getString("creator_username"));
groupInfo.setGroup_name(rs.getString("group_name"));
groupInfo.setGroup_id(rs.getString("group_id"));
groupInfo.setUser_name(rs.getString("user_name"));
groupInfo.setMember_id(rs.getString("member_id"));
String memberFullname = "select firstname,lastname,user_type from users where username ='" + groupInfo.getUser_name() + "'";
ps2 = currentCon.prepareStatement(memberFullname);
rs2 = ps2.executeQuery();
if(rs2.next())
{
String member_fullname = rs2.getString("firstname") + " " + rs2.getString("lastname");
groupInfo.setMember_name(member_fullname);
groupInfo.setMember_usertype(rs2.getString("user_type"));
}
group_name = groupInfo.getGroup_name();
groupList.add(groupInfo);
}
}
//request group name
request.setAttribute("group_name",group_name);
request.setAttribute("individual_group", groups);
request.setAttribute("groupList", groupList);
request.setAttribute("currentUser", currentUser);
RequestDispatcher rd = request.getRequestDispatcher("/ViewAllExistingGroups");
rd.forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
Jsp page to display the groups in accordion style - group name is displayed in the accordion title and members in the accordion content.
<body>
<% String username = (String)request.getAttribute("currentUser"); %>
<div class="container">
<div class="panel panel-default">
<div class="panel panel-heading">
</div>
<div class="panel panel-body">
<div id="accordion">
<c:forEach var="group1" items="${individual_group}">
<h5> ${group1.group_name}</h5>
<div>
<ul class="list-group">
<li class="list-group-item title">
<strong style="display:inline;"> About: <h5 style="display: inline;"> ${group1.group_description}</h5></strong>
</li>
<c:forEach var="group" items="${groupList}">
<li class="list-group-item title">
<img src="${pageContext.request.contextPath}/css/images/user.png" class="img img-circle" style="display: inline" />
<strong style="display:inline;"> ${group.member_name} <h5 style="display: inline;">(${group.member_usertype})</h5></strong>
</li>
</c:forEach>
</ul>
</div>
</c:forEach>
</div>
<br/>
<form action="ListConnectedUsers" method="get">
<input type="hidden" name="username" value="<%=username %>" />
<input type="submit" class="btn btn-default pull-left" value="Back"/>
</form>
</div>
<div class="panel panel-footer">
</div>
</div>
</div>
</body>
This is what i m getting in my jsp page. but the all the members are being displayed in each group of accordion panel
Each of the accordion section is showing all the members from the group_members table.
I want it to display three members in 'BEIT classroom' , three members in '3 BUDDIES' and 1 members 'SUDD GROUP'
I want the help with the jsp el or any condition if needed to present in manner i explained.
Sorry for such a long question but i tried explaining it properly. Please help
I think i can create a object where i can bundle the groupname and the group members full name.

How to send variable in action java class to jquery?

When I click on the button will be equal to the amount of undefind while the amount is equal to 1
in jsp page :
<%
String err1 = (String) request.getAttribute("err2");
int code = 0;
if (err1 != null){
code = Integer.parseInt(err1);
System.out.println(" code " + code);
}
%>
<button type="button" id="btnok">ok</button>
<br />
<%if(code == 3){ %>
<input id="txtcode" type="hidden" value="1" />
<%}%>
<br />
in jquery code:
$(document).ready(function() {
$("#btnok").click(function(event) {
var xxx = $("#txtcode").val();
alert("xxx 1 test + " + xxx);
});
});
The most likely reason is that the condition:
<%if(code == 3){ %>
<input id="txtcode" type="hidden" value="1" />
<%}%>
is not being met and the input is not being inserted to the DOM. Reload the page and view the page source to see if the input is inserted.

Enabling entire column of database as options in dropdown list

The code I have written gives me the output of only a single data in the drop down list. I want my drop down list to retrieve and hold all the data of the fname and userid column of the table. I am using struts concept and JSP. Kindly help as to how I should modify my code?
public String subjectAllocation2()throws Exception
{
try{
Map session=ActionContext.getContext().getSession();
Long idd=(Long)session.get("USERID");
System.out.println("sss"+idd);
ResultSet rs=ConnectionDb.getStatement().executeQuery("select USERID,FNAME from DIP_FACULTY");
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
ArrayList al = new ArrayList();
while(rs.next())
{
for (int i = 1; i <= columns; i++)
{
String value1 = rs.getString(i);
al.add(value1);
}
setUserid(rs.getString(1));
setFname(rs.getString(2));
System.out.println("The details="+rs.getString(1)+rs.getString(2));
}
catch(Exception e)
{
System.out.println("exception");
}
return "subjectAllocation2";
}
and this is the JSP code
<s:form action="addSubject" method="post" enctype="multipart/form-data">
<table border="1" cellpadding="15" width="300">
<tr><td><s:combobox name="semestar" label="Semestar" list="{'1st','2nd','3rd','4th','5th','6th','7th','8th'}"></s:combobox></td>
<td><s:combobox name="branch" label="Branch" list="{'CSE','IT','EEE','ETC','MECH','EE','CIVIL'}"></s:combobox></td>
<td><s:radio name="section" label="Section" list="{'A','B'}"/></td></tr>
<tr><td>
<s:select name="fname" label="Faculty name" list="fname"></s:select>
</td></tr>
<tr><td><s:combobox name="userid" label="Faculty id" list="userid"></s:combobox></td></tr>
<tr><td><s:combobox name="subject" label="Subject Name" list="{'Software Engineering','Soft Computing','Computer Graphics','Real Time System','Introduction To Digital Signal Processing','Entreprenaurship Development'}"></s:combobox></td></tr>
<tr><td><s:select name="sub_code" label="Subject Code" list="{'PCS098','HCS232','WQ1232','PNB342','WQW232','QAZ213'}"></s:select></td></tr>
<s:checkbox name="theory" label="Theory"></s:checkbox>
<s:checkbox name="practical" label="Practical"></s:checkbox>
<tr><td><s:submit value="allocate"/></td><td><s:submit action="reset" value="Reset"/></td></tr>
</table>
</s:form>
<s:property value="msg"/>

Categories