jsp update Query - html

Pls help me out...i just want to do edit operation using jsp..if i press edit button i just want to edit the current row data...how to set row data to the textbox...how to match the current row to text box
Employeeupdate.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# page import ="java.sql.*" %>
<%
try{
String n=request.getParameter("employeeid");
String p=request.getParameter("employeegender");
String e=request.getParameter("employeename");
String c=request.getParameter("employeesalary");
String d=request.getParameter("employeePhoneno");
String q=request.getParameter("employeeaddress");
String r=request.getParameter("employeeCountry");
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","root123");
PreparedStatement ps = conn.prepareStatement("Update emplo set emp_id=? emp_gender=?,emp_name=?,emp_sal=?,emp_phoneno=?,emp_address=? emp_country =? where emp_id =+ emp_id");
ps.setString(1,n);
ps.setString(2,p);
ps.setString(3,e);
ps.setString(4,c);
ps.setString(5,d);
ps.setString(6,q);
ps.setString(7,r);
int i=ps.executeUpdate();
ResultSet rs = ps.executeQuery();
if(i>0)
out.print( " successfully Updated by: "+e);
}catch (Exception e2) {System.out.println(e2);}
out.close();
%>
Employeeupdate.html
<html>
<body bgcolor="HotPink ">
<form action="EmployeeUpdate.jsp" method="get">
<center>
<h1>
<b><u>Employee Update form</u></b>
</h1>
<table border="2" bgcolor="Khaki " style="color: black">
<tr>
<td><b>Employee Id:</b></td>
<td><input type="text" name="employeeid"></td>
</tr>
<tr>
<td><b>Gender:</b></td>
<td><input type="radio" name="employeegender" value="male">Male<br>
<input type="radio" name="employeegender" value="female">Female</td>
</tr>
<tr>
<td><b>Employee Name:</b></td>
<td><input type="text" name="employeename"></td>
</tr>
<tr>
<td><b>Employee Salary:</b></td>
<td><input type="text" name="employeesalary"></td>
</tr>
<tr>
<td><b>Employee PhoneNo:</b></td>
<td><input type="text" name="employeePhoneno"></td>
</tr>
<tr>
<td><b>Employee Address</b></td>
<td><input type="text" name="employeeaddress"></td>
</tr>
<tr>
<td><b>Country:</b></td>
<td><select name="employeeCountry">
<option>Select</option>
<option>India</option>
<option>Austraila</option>
<option>SouthAfrica</option>
<option>England</option>
<option>Usa</option>
<option>UAE</option>
<option>Srilanka</option>
<option>Pakistan</option>
<option>other</option>
</select></td>
</tr>
<tr>
<center> <td><td><input type="submit" value="Update"></td></td></center>
</tr>
</table>
</center>
</form>
</body>
</html>

Move your HTML file into the jsp and then populate your text fields as the following
<td><b>Employee Id:</b></td>
<td><input type="text" name="employeeid" value=<%=request.getParameter("employeeid")%>></td> //etc
Note, however, that java expressions inside a jsp file (<%= %>) are considered bad practice but they are fine if it's just a simple page.
You really should create a servlet and move your business logic there (database access etc).

Related

Get the selected value in a select - option Thymeleaf and Spring Boot

Im trying to generate a search options and in myweb page the user choose the value that is looking for this is my html code:
<table>
<tr>
<td><h2 align="left">Red Social: </h2></td>
<td>
<select th:field="*{redes}" name="redSoc">
<option th:each="r : ${redes}" th:value="${r.nombreRedSocial}"
th:text="${r.nombreRedSocial}"></option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Buscar"/></td>
</tr>
</table>
And this is my controller code:
#GetMapping("/RedesSociales")
public String infoPorRedSocial(Model model, #RequestParam(name = "redSoc", required = false) String redSoc) {
RedesSociales[] redes = redesService.obtenerTodasRedes();
model.addAttribute("redes", redes);
//model.addAttribute("grupo", null);
System.out.println(redSoc);
if (redSoc != null) {
System.out.println(redSoc);
model.addAttribute("info", infoService.obtenerTodosPorRedSocial(redSoc));
}
return "visual/reportes/ListarPorRedSocial";
}
Now as you can see im trying to get the value redSoc but im not sure how to the get this value from the selected option i tried several methods but didnt work.
EDIT:
I was using this code:
<form action="/RedesSociales" method="GET">
<h2 align="left">Red Social: </h2>
<input type="text" name="redSoc"/> <br>
<input type="submit" value="Buscar"/>
</form>
But this was not usefull because the option would be insert by the user and i wanted to made it more dynamic thats why now im using the current values stored in the database
already solve the problem Change my code to this :
<table> <tr> <td><h2 align="left">Red Social: </h2></td>
<td> <select name="redSoc"> <option th:each="r : ${redes}" th:value="${r.nombreRedSocial}" th:text="${r.nombreRedSocial}">
</option>
</select> </td> </tr>
<tr> <td><input type="submit" value="Buscar"/></td> </tr> </table>

SET VALUE to INPUT type Text with FUNCTION HTML

this is part of my code :
<script>
function setCard() {
var carta = document.getElementById('idlistaCarte').value;
document.getElementById('idcodice_carta').innerHTML = carta;
}
</script>
<table>
<tr>
<td><p>Conferma Pagamento</p></td>
</tr>
<tr>
<td><select name="listaCarte" id="idlistaCarte">
<%
int i = 0;
for (String titolareCarta : listatitolare_carta) {
%>
<option value="<%=listacodice_carta.get(i)%>"
id="idcodice_carta_select">
<%=titolareCarta%>,<%=listacodice_carta.get(i)%>,<%=listatipologia_carta.get(i)%>
</option>
<%
i++;
}
%>
</select></td>
</tr>
<tr>
<td><input type="button" onclick="setCard()" value="Set Card"></td>
<td><p id="idcodice_carta"></p></td>
</tr>
<tr>
<td><input type="text" placeholder="codice_carta"
name="codice_carta" id="idcodice_carta" required></td>
</tr>
</table>
I'm trying to pass the value in the input type text with id = "idcodice_carta" taking the value from the select via button.
I can not make it.
trying to populate a p tag this works, but I need to have the value in the input field.
How can I solve the problem?
Thank you in advance.
you need to change id either
<td><p id="idcodice_carta"></p></td>
or
<input type="text" placeholder="codice_carta"
name="codice_carta" id="idcodice_carta" required>
you can't use same id in one page ex "idcodice_carta"

What is the syntax of replace statement?

This is my controller
#RequestMapping(value = "/deleteMultiple", method = RequestMethod.POST)
public ModelAndView deleteEmp(#ModelAttribute("employee") Employee emp,#RequestParam(value="Id",required=false) String Id,Model model) {
List<Employee> empList = empService.getEmpList();
List<Employee> empListById = empService.getemp_IdDetails(Id);
empList=empService.searchEmpDetails(emp);
empList.addAll(empListById);
Employee emp1=new Employee();
if(empList!=null){
model.addAttribute("delete", empList);
if(empList.isEmpty()){
model.addAttribute("message","This Employee Details are not present");
}
}
model.addAttribute("employee", emp1);
return new ModelAndView("delete","empList",empList);
}
This is my sql
#Override
public List<Employee> getemp_IdDetails(String emp_Ids){
List<Employee> empList = new ArrayList<Employee>();
String sql="select * from EmpDetails where emp_Id IN" + " ("+emp_Ids+") ";
sql.replace("emp_Id", emp_Ids);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
empList = jdbcTemplate.query(sql, new EmployeeRowMapper());
System.out.println("empList:"+empList.size());
return empList;
}
This is my jsp
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Drop down</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#delete").click(function(){
var d = [];
$.each($("#delete option:selected"), function(){
d.push($(this).val());
});
var s=(d.join(", "));
console.log(s);
$("#delete_empId").val(s);
console.log($("#delete_empId").val());
});
});
</script>
</head>
<body>
<div align="center">
<form:form method="post" action="${pageContext.request.contextPath}/deleteMultiple" modelAttribute="employee">
<table>
<tr>
<td>First Name :</td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>Last Name :</td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td>emp_Id :</td>
<td><form:input path="emp_Id" /></td>
</tr>
<tr>
<td>email_Id :</td>
<td><form:input path="email_ID"/></td>
</tr>
<tr>
<td>phone_No :</td>
<td><form:input path="phone_No"/></td>
</tr>
<tr>
<td>City :</td>
<td><form:input path="city"/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Search" /></td>
</tr>
</table>
<input type= "hidden" id="delete_empId" name="delete_empId" />
</form:form>
</div>
<div align="center">
<h2 style="color:blue">SelectBox:</h2>
<form:form method="post" action="${pageContext.request.contextPath}/deleteMultipleEmpDetails">
<select id="delete" multiple name="employees">
<c:forEach items="${empList}" var="employee">
<option value= "${employee.emp_Id}" >${employee.firstName}</option>
${msg}
</c:forEach>
</select>
<input type="submit" name="delete" value="delete">
</form:form>
</div>
<div>
<h4 style="color:red">${message}</h4>
</div>
</body>
</html>
This is my code i am retriving the values from jsp and passing the values to my controller.So i need to replace the values of emp_Id by emp_Ids.I have wriiten the replace function in sql is that coorect.I am getting null pointer exception.

Exactly i"m getting this error? Unknown column 'Nationality' in 'field list' using in jsp, mysql and tomcat?

i ill created the program of create, insert, update, delete, exit using in jsp and mysql only. i got the solutions for insert, delete, exit, and create but, update section only i got error., in my DB name is employee and table name is employees, the fields are in the order like 1. employee name, 2. employee_id, 3. nationality,. In update part i'm using the Employee_id is displayed in default, it's also displayed but i got error in 3 field of Nationality. I send my code if you any clear my error,. thanks in advance. the coding following as:
<%# page import="java.sql.*" %>
<%# page import="java.sql.DriverManager.*" %>
<%# page import="java.sql.Statement.*" %>
<%# page import="java.io.*" %>
<html>
<head>
<title>
UPDATING DETAILS ABOUT EMPLOYEE
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h3>ENTER THE EMPLOYEE DETAILS</h3><br>
<form action="editEmployee.jsp" name="UpdateForm" method="post">
<table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
<tr>
<td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
</tr>
<tr>
<td align="center">EMPLOYEE NAME:</td>
<td align="center"><input type="text" name="EmployeeName"></td>
</tr>
<tr>
<td align="center"> EMPLOYEE NATIONLITY:</td>
<td align="center"><input type="text" name="Nationality"></td>
</tr>
<%
String EmployeeName=request.getParameter("EmployeeName");
String Nationality=request.getParameter("Nationality");
PreparedStatement ps;
Connection con=null;
String driverName="com.mysql.jdbc.Driver";
String connectionUrl="jdbc:mysql://localhost:3306/";
String user="root";
String password="sarakrish";
String dbName="employee";
try{
Class.forName(driverName);
con=DriverManager.getConnection(connectionUrl+dbName,user,password);
ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=?");
ps.setString(1, EmployeeName);
ps.setString(2, Nationality);
ps.executeUpdate();
}
catch(Exception e)
{
out.print(e.getMessage());
e.printStackTrace();
}
%>
</table><br><br>
<input type="submit" value="UPDATE" name="method" class="button" >
<input type="reset" value="CLEAR">
<input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
</form>
</body>
</html>
hey buddy's thanks for little conversations. I ill do some changes in my program and got the output., As the code is following as:
<%# page import="java.sql.*" %>
<%# page import="java.sql.DriverManager.*" %>
<%# page import="java.sql.Statement.*" %>
<%# page import="java.io.*" %>
<%
PreparedStatement ps;
Connection con=null;
ResultSet rs=null;
Statement st=null;
String driverName="com.mysql.jdbc.Driver";
String connectionUrl="jdbc:mysql://localhost:3306/";
String user="root";
String password="sarakrish";
String dbName="employee";
String emp_id = "";
String emp_name = "";
String nationality = "";
try{
Class.forName(driverName);
con=DriverManager.getConnection(connectionUrl+dbName,user,password);
if(request.getParameter("edit")!=null && request.getParameter("edit").toString().equals("yes")){
String EmployeeId = request.getParameter("emp_id");
String EmployeeName=request.getParameter("EmployeeName");
String Nationality=request.getParameter("Nationality");
ps=con.prepareStatement("update employees set EmployeeName=?,Nationlity=? where Employee_id=?");
ps.setString(1, EmployeeName);
ps.setString(2, Nationality);
ps.setString(3, EmployeeId);
ps.executeUpdate();
ps.close();
}
st = con.createStatement();
String sql="SELECT * FROM employees where employee_id='"+request.getParameter("emp_id")+"'";
rs=st.executeQuery(sql);
while (rs.next()) {
emp_id = rs.getString(2);
emp_name = rs.getString(1);
nationality = rs.getString(3);
break;
}
rs.close();
st.close();
con.close();
}catch(Exception e){
out.print(e.getMessage());
e.printStackTrace();
}
%>
<html>
<head>
<title>
UPDATING DETAILS ABOUT EMPLOYEE
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h3>ENTER THE EMPLOYEE DETAILS</h3><br>
<%=request.getParameter("edit")%>
<form action="editEmployee.jsp" name="UpdateForm" method="post">
<input type="hidden" name="edit" id="edit" value="yes">
<input type="hidden" name="emp_id" id="emp_id" value="<%=emp_id %>">
<table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
<tr>
<td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
</tr>
<tr>
<td align="center">EMPLOYEE NAME:</td>
<td align="center"><input type="text" name="EmployeeName" value="<%=emp_name %>"></td>
</tr>
<tr>
<td align="center"> EMPLOYEE NATIONLITY:</td>
<td align="center"><input type="text" name="Nationality" value="<%=nationality %>"></td>
</tr>
</table><br><br>
<input type="submit" value="UPDATE" name="method" class="button" >
<input type="reset" value="CLEAR">
<input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
</form>
</body>
</html>
Assuming this as string , since your Table details are unclear . Pass the arguments as String
ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=? where employee_id=? ");
ps.setString(1,'EmployeeName');
ps.setString(2,'Nationality');
ps.setString(3,1);
Also avoid using scriptlets , read through the link Aniket suggested.
Hope it helps !!

Geting the first line in the textarea from database

I am trying to retrieve the value of an HTML textarea in jsp page from mysql database.The problem I am having is that I only can get he first line in the textarea(When I'm saving address in databse, It's retrieving first line only). This is happening for text box also, where data has space in the database, it's only getting first line of the data.
Thanks in advance.
HTML Code:
<div class="container2">
<table>
<tr>
<td align="right">TPO Name</td>
<td><input type="text" readonly name="tponame" value=<%=rs.getString("tponame")%>></td>
</tr>
<tr>
<td align="right">Name of College</td>
<td><input type="text" readonly name="college_name" value=<%=rs.getString("college_name")%>></td>
</tr>
<tr>
<td align="right">Contact Number</td>
<td><input type="text" name="contactno" value=<%=rs.getString("contactno")%>></td>
</tr>
<tr>
<td align="right">Contact Email</td>
<td><input type="text" name="contactemail" value=<%=rs.getString("contactemail")%>></td>
</tr>
<tr>
<td align="right">Address</td>
<td><input type="text" name="address" value=<%=rs.getString("address")%>></td>
</tr>
<tr>
<td colspan="2"><input type="submit" class="button" value="Update" style="float:right;"></input></td>
</tr>
JSP code:
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/FMS","root","root");
Statement st=con.createStatement();
String tponame= request.getParameter("tponame");
String college_name=request.getParameter("college_name");
String contactno=request.getParameter("contactno");
String contactemail=request.getParameter("contactemail");
String address=request.getParameter("address");
String sql = ("update tpo_details set contactno='"+contactno+"',contactemail='"+contactemail+"',address='"+address+"' where tponame='"+tponame+"'");
st.executeUpdate(sql);
response.sendRedirect("update_tpo_network_data.jsp");
%>