Need to write One Pojo class(employee class), to retrieve db data in JSP.
public class Employee {
String id;
String name;
String mobileNo;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
}
JSP provide below:
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("userId");
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "dbname";
String userId = "root";
String password = "password";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<h2 align="center"><font><strong>Retrieve data from database in jsp</strong></font></h2>
<table align="center" cellpadding="5" cellspacing="5" border="1">
<%
try{
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String sql ="select `id`,`first_name`,`mobile_no` from `user_info` where `first_name` like '%Chettupalli%'";
List<Employee> empList = new ArrayList<Employee>();
Employee emp;
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
emp = new Employee();
emp.setId(resultSet.getString("id"));
emp.setName(resultSet.getString("first_name"));
emp.setMobileNo(resultSet.getString("mobile_no"));
empList.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
}
%>
<form>
<!-- You can prepare your form based on your requirement-->
</form>
</table>
empList will gets prepared at the time of rendering this JSP.
So you just relay on empList object to prepare your form with dynamic employee data.
I think this will be work for your requirement.