inserting values into database using java netbeans - java

It is a simple program for inserting into database.There are no errors but on inserting the value only 2 columns(year and subject) are getting affected in all the other 4 columns it is showing null value.how do I correct it.....................................................................................................
File name-index.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>INSERT</title>
<link rel="stylesheet" type="text/css" href="style1.css" />
</head>
<body>
<h1>Insert File Details</h1>
<form action="loginprocess.jsp">
<table border="1">
<tbody>
<tr>
<td>File No :</td>
<td><input type="text" name="File No" value="" size="50" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="Name" value="" size="50" /> </td>
</tr>
<tr>
<td>Place of Origin :</td>
<td><input type="text" name="origin" value="" size="50" /></td>
</tr>
<tr>
<td>Year :</td>
<td><input type="text" name="year" value="" size="50" /> </td>
</tr>
<tr>
<td>Subject :</td>
<td><input type="text" name="subject" value="" size="50" /></td>
</tr>
<tr>
<td>ISBN :</td>
<td><input type="text" name="ISBN" value="" size="50" /> </td>
</tr>
</tbody>
</table>
<input type="reset" value="Clear" name="Clear" />
<input type="submit" value="Submit" name="Submit" />
</form>
</body>
</html>
File name LoginDao.java
package bean;
import java.sql.*;
public class LoginDao {
public static boolean validate(LoginBean bean){
boolean status=false;
try{
Connection conn=ConnectionProvider.getConn();
String sql="insert into file(Fileno,name,place,year,subject,ISBN) values (?,?,?,?,?,?)";
PreparedStatement pstmt =conn.prepareStatement(sql);
pstmt.setInt(1, bean.getfileno());
pstmt.setString(2,bean.getname());
pstmt.setString(3, bean.getoriginPlace());
pstmt.setInt(4, bean.getyear());
pstmt.setString(5, bean.getsubject());
pstmt.setInt(6, bean.getisbn());
int rs=pstmt.executeUpdate();
if(rs>0){status=true;}
}catch(Exception e){}
return status;
}
}
File name LoginBean.java
package bean;
public class LoginBean {
private String name,subject,originPlace;
private int fileno,year,isbn;
public int getfileno() {
return fileno;
}
public void setfileno(int fileno) {
this.fileno = fileno;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getoriginPlace() {
return originPlace;
}
public void setoriginPlace(String originPlace) {
this.originPlace = originPlace;
}
public int getyear() {
return year;
}
public void setyear(int year) {
this.year = year;
}
public String getsubject() {
return subject;
}
public void setsubject(String subject) {
this.subject = subject;
}
public int getisbn() {
return isbn;
}
public void isbn(int isbn) {
this.isbn = isbn;
}
}
File name-loginprocess.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<%#page import="bean.LoginDao"%>
<jsp:useBean id="obj" class="bean.LoginBean"/>
<jsp:setProperty property="*" name="obj"/>
<%
boolean status=LoginDao.validate(obj);
if(status){
out.println("success");
session.setAttribute("session","TRUE");
}
else
{
out.print("failed");}
%>
<jsp:include page="index.jsp"></jsp:include>
</body>
</html>

If you are using jsp benas tag then property name in class(bean class) and <input > name is jsp must be same.only two field have same name. so only two value are inserted.
In class in jsp
File No fileno
So put same name in class and bean.

I believe that Java cannot match the names in your <input> elements in your JSP file to the correct setter methods in the bean. For example, you had this element and setter:
<input type="text" name="Name" value="" size="50" />
public void setname(String name) {
this.name = name;
}
and the property was never set (due to using setname() instead of setName()), but this did work:
<input type="text" name="year" value="" size="50" />
public void setyear(int year) {
this.year = year;
}
because Java could match year (lowercase) to the correct setter.
Try using the following code:
<tr>
<td>File No :</td>
<td><input type="text" name="FileNo" value="" size="50" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="Name" value="" size="50" /> </td>
</tr>
<tr>
<td>Place of Origin :</td>
<td><input type="text" name="Origin" value="" size="50" /></td>
</tr>
<tr>
<td>Year :</td>
<td><input type="text" name="Year" value="" size="50" /> </td>
</tr>
<tr>
<td>Subject :</td>
<td><input type="text" name="Subject" value="" size="50" /></td>
</tr>
<tr>
<td>ISBN :</td>
<td><input type="text" name="ISBN" value="" size="50" /> </td>
</tr>
Use this bean definition:
public class LoginBean {
private String name,subject,originPlace;
private int fileno,year,isbn;
public int getFileNo() {
return fileno;
}
public void setFileFo(int fileno) {
this.fileno = fileno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrigin() {
return originPlace;
}
public void setOrigin(String originPlace) {
this.originPlace = originPlace;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public int getISBN() {
return isbn;
}
public void setISBN(int isbn) {
this.isbn = isbn;
}
}

Related

error:"items" does not support runtime expressions in c:forEach jsp

This is my Servlet class
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
#WebServlet("/Register")
public class Register extends HttpServlet {
static int id=0;
private static final long serialVersionUID = 1L;
private static final ArrayList<Employee> EMPLOYEES = new ArrayList<>();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
id++;
int uage=0;
long mob=0;
String uexit = request.getParameter("uexit");
if(uexit.equals("Exit")) {
System.out.println("\n\n\n\n\n\n\n\n");
request.getRequestDispatcher("exit.jsp").forward(request, response);
}
else {
String uname = request.getParameter("uname");
uage = Integer.parseInt(request.getParameter("uage"));
mob = Long.parseLong(request.getParameter("umob"));
if(uname!=null && uage!=0 && mob!=0) {
Employee employee = new Employee(id, uname, uage, mob);
EMPLOYEES.add(employee);
request.getSession().setAttribute("employees" , EMPLOYEES);
RequestDispatcher rd = getServletContext().getRequestDispatcher("register.jsp");
rd.forward(request, response);
}
else {
request.getSession().setAttribute("employees", null);
request.getRequestDispatcher("register.jsp").forward(request, response);
}
}
for(int i=0; i<EMPLOYEES.size(); i++) {
System.out.print(EMPLOYEES.get(i).id + "\t");
System.out.print(EMPLOYEES.get(i).name + "\t");
System.out.print(EMPLOYEES.get(i).age + "\t");
System.out.print(EMPLOYEES.get(i).mob + "\t");
System.out.println();
}
}
This is my register.jsp page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%#page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MyProject</title>
</head>
<h2 align="center">
<font color="#BF360C" size="10" face="Times New Roman"><b><i>
Register</i></b></font>
</h2>
<body style="background-color: #AED6F1">
<form action="Register" method="POST">
<table align="center" cellpadding="20" width="600"
style="background-color: #33C3DD;">
<tr>
<th><font size="5" face="Times New Roman"><b><i>Name:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="uname"></td>
</tr>
<tr>
<th><font size="5" face="Times New Roman"><b><i>Age:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="uage"></td>
</tr>
<tr>
<th><font size="5" face="Times New Roman"><b><i>Mobile
Number:</i></b></font></th>
<td colspan="2" align="center"><input type="text" name="umob"></td>
</tr>
<tr>
<th colspan="3" align="center"><input type="submit"
name="uexit" value="Register"><br /> <br /> <input
type="submit" name="uexit" value="Exit"></th>
</tr>
</table>
</form>
<br>
<br>
<table cellpadding="20" cellspacing="5" width="900"
style="background-color: #FDFEFE;" align="center">
<tr style="background-color: #FDFDFD">
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Sr. No.</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Name</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Age</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Mobile Number:</i></b></font></th>
<th style="background-color: #4468D1"><font size="5"
face="Times New Roman"><b><i>Edit</i></b></font></th>
</tr>
<c:forEach items="${employees}" var="item">
<tr>
<td style="background-color: #4468D1"><c:out value = "${item.id}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.name}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.age}"/></td>
<td style="background-color: #4468D1"><c:out value = "${item.mob}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
This is my Entity class
public class Employee {
public int id;
public String name;
public int age;
public long mob;
public Employee() {}
public Employee(int id, String name, int age, long mob) {
this.name= name;
this.age= age;
this.mob= mob;
this.id=id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age=age;
}
public long getMob() {
return mob;
}
public void setMob(int mob) {
this.mob = mob;
}
}
I want to pass list EMPLOYEES from the servlet to jsp page abd want to show each Employee object with <c:forEach> but it keeps on saying exception. so please anyone answer me.
Also tell me which jstl standard jar to download if i m using tomcat 8.5
i have jstl-jar 1.2 but i dont think it's working because this error is keeps on coming no matter how i do it.
Try to use
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
instead of
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
in your jsp code
Seems you are doing something wrong here
<tr>
<td>${item}</td>
</tr>
According to your code you are getting list of object in item . How you are printing a list which containing object ?
Try using
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.age}</td>
<td>${item.mob}</td>
</tr>

Spring Thymeleaf TemplateProcessingException in SpringInputGeneralFieldTagProcessor

I create in controller method:
#RequestMapping(value = "/user/registration", method = RequestMethod.GET)
public String showRegistrationForm(WebRequest request, Model model) {
UserDto userDto = new UserDto();
model.addAttribute("user", userDto);
return "registration";
}
and when I turn to URL localhost:8080/user/registration SpringInputGeneralFieldTagProcessor throw TemplateProcessingException .
org.thymeleaf.exceptions.TemplateProcessingException: Error during
execution of processor
'org.thymeleaf.spring4.processor.SpringInputGeneralFieldTagProcessor'
(template: "registration" - line 12, col 36)
How to resolve this?
registration.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Registration page</title>
</head>
<body>
<form name='regForm' th:userDto="${user}" th:action="#{/registration}" enctype="utf8" method="post">
<table>
<tr>
<td>User:</td>
<td><input type="text" th:field="*{username}"/></td>
</tr>
<tr>
<td>Email:</td>
<td><input type='email' th:field="*{email}"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' th:field="*{password}"/></td>
</tr>
<tr>
<td>Matching password:</td>
<td><input type='password' th:field="*{matchingPassword}"/></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
my UserDto.java
package com.eve.web.dto;
import com.eve.validation.ValidEmail;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
public class UserDto {
#NotNull
#NotEmpty
private String username;
#NotNull
#NotEmpty
private String password;
private String matchingPassword;
#ValidEmail
#NotNull
#NotEmpty
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMatchingPassword() {
return matchingPassword;
}
public void setMatchingPassword(String matchingPassword) {
this.matchingPassword = matchingPassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
since you are using user, you should add to all your attributes user.
here you will find the working code
`
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Registration page</title>
</head>
<body>
<form name='regForm' th:userDto="${user}" th:action="#{/registration}" enctype="utf8" method="post">
<table>
<tr>
<td>User:</td>
<td><input type="text" th:field="*{user.username}"/></td>
</tr>
<tr>
<td>Email:</td>
<td><input type='email' th:field="*{user.email}"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' th:field="*{user.password}"/></td>
</tr>
<tr>
<td>Matching password:</td>
<td><input type='password' th:field="*{user.matchingPassword}"/></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
`

Property 'id' not found on type com.spring.schoolmanagement.model.Subject

I'm trying to put two objects in my JSP view but while getting the value by getter is working fine and while referring the value by property of the object throws error.
JSP Code Snipet is:
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr><h2 class="intro-text text-center">Manage Subject</h2><hr>
</div>
<div class="col-md-12">
<c:url var="addAction" value="/data-entry/subject/add" ></c:url>
<form:form action="${addAction}" commandName="subject">
<table>
<c:if test="${!empty subject.name}">
<tr>
<td>
<form:label path="id">
<spring:message text="ID"/>
</form:label>
</td>
<td>
<form:input path="id" readonly="true" size="8" disabled="true" />
<form:hidden path="id" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="name">
<spring:message text="Name"/>
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td>
<form:label path="description">
<spring:message text="Description"/>
</form:label>
</td>
<td>
<form:input path="description" />
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty subject.name}">
<input type="submit"
value="<spring:message text="Edit Subject"/>" />
</c:if>
<c:if test="${empty subject.name}">
<input type="submit"
value="<spring:message text="Add Subject"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
<br>
<h3>Subject List</h3>
<c:if test="${!empty subjectlist}">
<table class="tg">
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<c:forEach items="${subjectlist}" var="subject">
<tr>
<td>${subject.getID()}</td>
<td>${subject.getName()}</td>
<td>${subject.getDescription()}</td>
<td><a href="<c:url value='/admin/subject/edit/${subject.getID()}' />" >Edit</a></td>
<td><a href="<c:url value='/admin/subject/remove/${subject.getID()}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</div>
</div>
</div>
Model Class:
package com.spring.schoolmanagement.model;
public class Subject {
private int id;
private String name;
private String description;
public void setID(int id) {
this.id = id;
}
public int getID() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setDescription( String description ) {
this.description = description;
}
public String getDescription() {
return this.description;
}
#Override
public String toString(){
return "{ID=" + id + ",Name=" + name + ",Description=" + description + "}";
}
}
Controller Class:
package com.spring.schoolmangement;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.spring.schoolmanagement.dao.SubjectDAOImpl;
import com.spring.schoolmanagement.model.Subject;
/**
* Handles requests for admin specific pages.
*/
#Controller
public class AdminController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
#Autowired
private SubjectDAOImpl subjectService;
/**
* Simply selects the home view to render by returning its name.
*/
#RequestMapping(value = {"/admin/", "/admin"}, method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "admin-dash-board";
}
#RequestMapping(value = "/admin/student")
public String student(Model model) {
return "admin-student";
}
#RequestMapping(value = "/admin/subject")
public String displaySubjects(Model model) {
model.addAttribute("subject", new Subject());
model.addAttribute("subjectlist", this.subjectService.getAll());
return "manage-subject";
}
#RequestMapping(value = "/admin/subject/edit/{id}")
public String course(#PathVariable("id") int id, Model model) {
model.addAttribute("subject", this.subjectService.getById(id));
model.addAttribute("subjectlist", this.subjectService.getAll());
return "manage-subject";
}
}
Please suggest where I'm making the mistake.
Change your getter/setter method from:
public void setID(int id) {
this.id = id;
}
public int getID() {
return this.id;
}
To
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
Your Bean is missing naming convention and should follow JavaBean (read pdf section 8.8) naming convention.

Spring form ID field not populated on POST

In the below Spring form, my object's ID field is populated, but when I receive the submission in the controller method, all of the form's fields are populated except for the ID field. I've quintuple-checked that the field type and getter/setter types are all the same non-primitive, as I've seen many of the other questions on SO similar to this and that seems to be the common issue. The controller doesn't have any method-level #ModelAttributes, so it isn't being populated otherwise.
Here's the declaration of the POST method, as I debugged it on the first containing line and found that the form's id field is empty:
#RequestMapping(value="/{orgId}", method=RequestMethod.POST)
public String editOrganizationPost(#PathVariable int orgId,
#Valid #ModelAttribute(ORG_FORM) OrganizationForm orgForm,
BindingResult result, RedirectAttributes att,
HttpServletRequest request) {
Here's the form object:
public class OrganizationForm {
private Integer id;
#NotBlank
private String name;
#NotBlank
private String description;
private Set<User> users;
int moveToOrganizationId = 0;
String moveToOrganizationName;
int[] moveFromOrganizationUserSelect = null; // List of selected users
// to be moved to a new
// organization
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public int getMoveToOrganizationId() {
return this.moveToOrganizationId;
}
public void setMoveToOrganizationId(int moveToOrganizationId) {
this.moveToOrganizationId = moveToOrganizationId;
}
public String getMoveToOrganizationName() {
return this.moveToOrganizationName;
}
public void setMoveToOrganizationName(String moveToOrganizationName) {
this.moveToOrganizationName = moveToOrganizationName;
}
public int[] getMoveFromOrganizationUserSelect() {
return this.moveFromOrganizationUserSelect;
}
public void setMoveFromOrganizationUserSelect(
int[] moveFromOrganizationUserSelect) {
this.moveFromOrganizationUserSelect = moveFromOrganizationUserSelect;
}
public boolean isNew() {
return this.id == null || this.id == 0;
}
}
Here is the markup from the JSP file:
<form:form method="post" action="${submitUrl}" commandName="organizationForm">
<form:errors path="*" />
<form:hidden path="id" />
<table class="adminTable editContent">
<tr class="bg_lgtGrey">
<td><fmt:message
key="manageOrganizations.organizationForm.name" />:</td>
<td><form:input path="name" cssClass="inputbox"
tabindex="4" /></td>
</tr>
<tr class="bg_lgtGrey">
<td><fmt:message
key="manageOrganizations.organizationForm.description" />:</td>
<td><form:textarea path="description"
cssClass="inputbox" tabindex="4" /></td>
</tr>
<tr>
<td colspan="2" align="right"><c:choose>
<c:when test="${!organizationForm.new}">
<input type="submit" class="btn btn-primary"
id="submit_button" value="Update" />
</c:when>
<c:otherwise>
<input type="submit" class="btn btn-primary"
id="submit_button" value="Create" />
</c:otherwise>
</c:choose></td>
</tr>
</table>
</form:form>
And here is the generated HTML:
<form id="organizationForm" action="/admin/organizations/1" method="post">
<input id="id" name="id" type="hidden" value="1">
<table class="adminTable editContent">
<tbody><tr class="bg_lgtGrey">
<td>Organization Name:</td>
<td><input id="name" name="name" class="inputbox" tabindex="4" type="text" value="Organization1"></td>
</tr>
<tr class="bg_lgtGrey">
<td>Organization Description:</td>
<td><textarea id="description" name="description" class="inputbox" tabindex="4"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" class="btn btn-primary" id="submit_button" value="Update">
</td>
</tr>
</tbody></table>
</form>
I used Chrome devtools to catch the POST data and here it is:
id=1&name=Organization1&description=
...yet at the breakpoint at the first line of the editOrganizationPost method, the form.id field is set to 0.
I have spent forever trying to figure out why it will bind the name and description but not the ID when sent. I could obviously just inject the ID from the path variable but I am dumbfounded as to why it wouldn't just populate the field naturally.
I had the same problem.. the id field would always be set to 0. Changing the id field to something else didn't work either. After more hit and trail, removing
disabled="true"
from
<form:input path"id" disabled="true"/>
fixed the issue.
I assumed
<form:hidden path="id">
would give the same error but it didn't.

Implementing a java bean into a jsp servlet

I am trying to connect my JSP servlets to a posgress database and I am currently using a java bean class which is playing the role of the middle man. I am experiencing some difficulties with making the registration form successfully store user information into the database. I would really appreciate if you would kindly help me out.
Thanks a lot in advance.
JSP servlet:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register here</title>
</head>
<body>
<form method="post" action="registration.jsp">
<center>
<table border="1" width="30%" cellpadding="5">
<thead>
<tr>
<th colspan="2">Enter Information Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>First Name</td>
<td><input type="text" name="fname" value="" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname" value="" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="" /></td>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td>Current Country</td>
<td><input type="text" name="country" value="" /></td>
</tr>
<tr>
<td>Current City</td>
<td><input type="text" name="city" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
<tr>
<td colspan="2">Already have an account? Login Here</td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
The Java Bean that I use :
public class UserBean {
private int id;
private String username;
private String password;
private String email;
private String firstName;
private String lastName;
private String endDate;
private boolean validated;
public UserBean() {
// Empty constructor
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEndDate() {
return endDate;
}
public boolean isValidated() {
return validated;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmail(String email) {
this.email = email;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public void setValidated(boolean validated) {
this.validated = validated;
}
}
Your POJO JavaBean won't magically get populated with the data. It has no connection to the database and no way to get or save data.
You need a controller that fetches data from the DB, creates model objects, and populates them with the data. The controller is also responsible for saving beans
You could write this yourself but it's generally better to use existing ORM frameworks like JPA2, a custom persistence provider API like Hibernate, or something like MyBatis. If you really want, you can hand-roll your controller with direct JDBC calls, injecting the connection from the environment, but that tends to produce a lot of boilerplate code for little benefit even with things like Spring JDBC to help smooth things over.
Some IDEs, like NetBeans and Eclipse, can even auto-generate models and controllers for you, though I've never been very happy with the results (particularly the failure to use a parent-class and generic methods and the lack of any sort of useful error handling).

Categories