Update certain data in SQL with JSP - sql

i've been learning web programming (JSP and SQL) for the past few days, i need help with certain problem, so i need to show the list of all name of members from my database, add button beside it, and when i click it, i need to update the data in database, which name is exactly the same name besides the button .
I have successfully show the data and button, and i know syntax for updating in SQL, but i have no idea how to validate when i click the button, it updates the data with the same name exactly besides the button. Let's just pretend all name is unique. This is my sample code:
<div id="textArea">
<%
String query = "SELECT * FROM member";
ResultSet rs = st.executeQuery(query);
while(rs.next()){%>
<table>
<tr>
<td><%out.print(rs.getString("Fullname"));%></td>
<td> <input type="button" value="Change Role"/> </td>
</tr>
</table>
<%
}
%>
</div>
Any help is really appreciated. thanks!

You have to write Java code in a JSP Page for updating certain data from JSP.
You can write Java Code inside a scriptlet in a JSP page.
You have to write scriptlet inside <% ...%> tag, Please see sample below:
<%
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
Statement st=con.createStatement();
......
%>
Please follow the post below to execute update query directly from JSP:
Jsp sql update query

Related

Selenium Junit to find element

I am new to selenium.I am creating test script for duplicate registration into a website .When an error is displayed for duplicate registration i want to pass the testcase .
I am getting confused regarding the property of the webelement . I am not sure as to which field should i take or as how to write the findelement for the error image displayed.
Here is the HTML tag for the element image displayed:
<td>
<img src="images/bell.png">
</td>
This is for the text displayed along with error image :
<a onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['navigation'],'navigation:j_id322,navigation:j_id322','');}return false" style="color:#000000;" href="#">Found 1 patient(s) with same Patient Name, Gender and Age. Click here to view details</a>
Please help .Thanks
Based purely on the details provided, the following would work;
findElement(By.css("td>img[src='images/bell.png']")
and
findElement(By.css("a[onclick]")
However, I am sure that there is more context than this. Without the full html it is hard to give better solutions

Updating HTML table through JSTL and SQL

I have a HTML table which initially displays all records in the DB. I have just added a JQuery Datepicker component, and now I want to be able to retrieve records based on date. I have no problem extracting the date from the Datepicker component, but I do have a problem updating the existing HTML table. My code is pretty standard.
<sql:query var="retrieveSecurity" dataSource="jdbc/SecurityApp">SELECT * FROM security</sql:query>
<tbody>
<c:forEach var="row" items="${retrieveSecurity.rows}">
<tr>
<td>${row.security_id}</td>
<td>${row.description}</td>
<td class="center">${row.bid_price}</td>
<td class="center">${row.bid_percent_change}</td>
<td>${row.comment}</td>
</tr>
</c:forEach>
</tbody>
What I basically want to do is to have another SQL query, something like this...
<sql:query var="retrieveSecurity" dataSource="jdbc/SecurityApp">SELECT * FROM security where trade_date = "MYVALUE"
</sql:query>
I then want to be able to update the the tbody tag to point to this new SQL query, and call it instead. The columns won't change, so it should be the same table, just with filtered data. Not sure if what I am asking is the easiest/best solution, just looking for something that will work quickly for a prototype.
Hope this makes sense.

jsp foreach on jdbc list<map> format

so im having a little problem with my code. I used jdbc to make a query to our SQL database in our DAO like so:
List<Map<String,Object>> results = namedjdbcTemplate.queryForList(FINDALL, namedParameters);
Which is then passed up from the service to the controller where it is added to our model:
List<Map<String,Object>> locations = cmsAttributeService.getAttributeList(id);
model.addAttribute("locationlist", locations);
I then need to display all the entries in our jsp. Currently i do so like this:
<c:forEach items="${locationlist}" var="list">
<tr>
<td>
<c:out value="${list }"></c:out>
</td>
</tr>
</c:forEach>
My problem is the formatting it returns, for example:
{=UCMDB2.Project.name}
How do i get it to return just 'UCMDB2.Project.name' dropping the {= }? Ive tried all sorts of other calls like using list.value, list.key, all of which either break it entirely or blank the results out. Im fairly sure it should be something simple that im just missing. Anyone got any clues?
Changed it to List <String> and it worked.

Using EL in <jsp:include> tag

i have a jsf 1.2 / rich faces 3.2.2 project (with java 6). i want to display menu bar conditionally depending on type of user logged in. From the question How to conditionally include a file in my template using JSF and Facelets? i tried the following:-
...
<td valign="top" align="left" height="100%">
</f:verbatim>
<jsp:include page="../Menu${authenticateBean.menuSuffix}.jsp" /> <f:verbatim></td>
...
i also tried
<jsp:include page="../Menu#{authenticateBean.menuSuffix}.jsp" />
...
where authenticateBean.menuSuffix is a string that will return "A" or "B" and ultimately, theoretically "MenuA.jsp" or "MenuB.jsp" page should include in my page. but i get following error
javax.servlet.ServletException: File "/pages/includes/LeftPan.jsp" not found
Help. Plz.
Heres how I managed it. Placing as an answer if ne one needs…
<%
AuthenticateBean authBean=
((AuthenticateBean)FacesUtils.getManagedBean(AuthenticateBean.MANAGED_NAME));
String panSuffix = authBean.getPanSuffix();
String impPage = "../includes/Menu"+ panSuffix +".jsp";
%>
.
.
.
<jsp:include page="<%= impPage %>" />
In the getPanSuffix() i have placed business logic that checks the type of user logged on and return a string accordingly. for Type "A" user. MenuA.jsp would display and for type "B" user MenuB.jsp would display. May be a primitive way of doing things but worked for me.
Thanks All.

Getting the data out of the data Formview

I have an ASP pg with a formview list control on it that hooks up to a sql database customer table
I want to access the customer name outside the form.
How do I:
A) access the bound text box in the form view template? Something like Text1.text=formview1.customername.text (that doesn't work but that's the kinda thing)
B) access the database field in the table that the sqlsource connects to to feed the formview
Really appreciate the help. I bet it's easy to do but I'm just not getting there.
You are going to want to use the DataBinder Class. For example:
<asp:formview id="formViewContainer" ... other code>
..... formview code .....
<%# Eval("CustomerName") %>
..... more formview code .....
</asp:formview>
Then to access "CutomerName" outside of the formview use the DataBinder as follows:
<asp:textbox id="customerName" runt="server"
text='<%# DataBinder.GetPropertyValue(formViewContainer.DataItem, "CustomerName") %>' />