I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please give me some idea where I am going wrong ??
I just want to retrieve data from my emp_mstr table and display it using my JSP file...
The table emp_mstr is as follows :-
- CREATE TABLE EMP_MSTR(
-
EMP_NO VARCHAR(10) PRIMARY KEY,
-
PASSWORD VARCHAR(6),
-
BRANCH_NO VARCHAR(10),
-
FNAME VARCHAR(25),
-
MNAME VARCHAR(25),
-
LNAME VARCHAR(25),
-
DEPT VARCHAR(30),
-
DESIG VARCHAR(30),
-
ADDR VARCHAR(50)
-
);
My JSP file is as follows :-
employeeDetailForm.java
- package com.kmg.employee;
-
import org.apache.struts.action.ActionForm;
-
public class employeeDetailForm extends ActionForm{
-
-
private String empNo;
-
private String password;
-
private String fName;
-
private String mName;
-
private String lName;
-
private String dept;
-
private String desig;
-
private String address;
-
-
public String getempNo() {
-
return empNo;
-
}
-
public void setempNo(String empNo) {
-
this.empNo = empNo;
-
}
-
public String getpassword() {
-
return password;
-
}
-
public void setpassword(String password) {
-
this.password = password;
-
}
-
public String getfName() {
-
return fName;
-
}
-
public void setfName(String name) {
-
fName = name;
-
}
-
public String getmName() {
-
return mName;
-
}
-
public void setmName(String name) {
-
mName = name;
-
}
-
public String getlName() {
-
return lName;
-
}
-
public void setlName(String name) {
-
lName = name;
-
}
-
public String getDept() {
-
return dept;
-
}
-
public void setDept(String dept) {
-
this.dept = dept;
-
}
-
public String getDesig() {
-
return desig;
-
}
-
public void setDesig(String desig) {
-
this.desig = desig;
-
}
-
public String getAddress() {
-
return address;
-
}
-
public void setAddress(String address) {
-
this.address = address;
-
}
-
-
}
-
employeeDetailAction.java
- package com.kmg.employee;
-
-
import com.kmg.employee.employeeDetailForm;
-
import com.kmg.services.*;
-
-
import java.sql.Connection;
-
import javax.servlet.http.*;
-
import javax.sql.DataSource;
-
import org.apache.struts.action.*;
-
-
public class employeeDetailAction extends Action {
-
-
public ActionForward execute(ActionMapping mapping, ActionForm form,
-
HttpServletRequest request, HttpServletResponse response)
-
throws Exception {
-
employeeDetailForm employeeDetailForm = (employeeDetailForm) form;
-
EmployeeDAO employeeDAO = new EmployeeDAO();
-
Employee employee = new Employee();
-
-
Connection con = null;
-
DataSource ds = null;
-
-
ds = getDataSource(request);
-
employee.setempNo(employeeDetailForm.getempNo());
-
String password = employeeDetailForm.getpassword();
-
String fName = employeeDetailForm.getfName();
-
String mName = employeeDetailForm.getmName();
-
String lName = employeeDetailForm.getlName();
-
String dept = employeeDetailForm.getDept();
-
String desig = employeeDetailForm.getDesig();
-
String address = employeeDetailForm.getAddress();
-
con = employeeDAO.getConnection(ds);
-
employee = employeeDAO.getRecord(con, employee);
-
employeeDAO.closeConnection(con);
-
return mapping.getInputForward();
-
-
}
-
}
-
EmployeeDAO.java
- package com.kmg.services;
-
-
import java.sql.Connection;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
-
import javax.sql.DataSource;
-
-
public class EmployeeDAO {
-
public Connection getConnection(DataSource datasource) {
-
Connection con = null;
-
DataSource ds = datasource;
-
try {
-
con = ds.getConnection();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
return con;
-
}
-
-
public void closeConnection(Connection con) {
-
try {
-
con.close();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
}
-
-
public static Employee getRecord(Connection con, Employee employee) {
-
String qry = "select PASSWORD from EMP_MSTR where EMP_NO=?";
-
String password = null;
-
ResultSet resultSet = null;
-
try {
-
PreparedStatement pstat = con.prepareStatement(qry);
-
pstat.setString(1, employee.getempNo());
-
resultSet = pstat.executeQuery();
-
while (resultSet.next()) {
-
employee.setpassword(resultSet.getString("PASSWORD"));
-
employee.setfName(resultSet.getString("FNAME"));
-
employee.setmName(resultSet.getString("MNAME"));
-
employee.setlName(resultSet.getString("LNAME"));
-
employee.setDept(resultSet.getString("DEPT"));
-
employee.setDesig(resultSet.getString("DESIG"));
-
employee.setAddress(resultSet.getString("ADDR"));
-
}
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
-
return employee;
-
}
-
}
-
Employee.java
- package com.kmg.services;
-
-
public class Employee {
-
private String empNo;
-
private String password;
-
private String fName;
-
private String mName;
-
private String lName;
-
private String dept;
-
private String desig;
-
private String address;
-
public String getempNo() {
-
return empNo;
-
}
-
public void setempNo(String empNo) {
-
this.empNo = empNo;
-
}
-
public String getpassword() {
-
return password;
-
}
-
public void setpassword(String password) {
-
this.password = password;
-
}
-
public String getfName() {
-
return fName;
-
}
-
public void setfName(String name) {
-
fName = name;
-
}
-
public String getmName() {
-
return mName;
-
}
-
public void setmName(String name) {
-
mName = name;
-
}
-
public String getlName() {
-
return lName;
-
}
-
public void setlName(String name) {
-
lName = name;
-
}
-
public String getDept() {
-
return dept;
-
}
-
public void setDept(String dept) {
-
this.dept = dept;
-
}
-
public String getDesig() {
-
return desig;
-
}
-
public void setDesig(String desig) {
-
this.desig = desig;
-
}
-
public String getAddress() {
-
return address;
-
}
-
public void setAddress(String address) {
-
this.address = address;
-
}
-
-
}
-
struts-config.xml
- <?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
-
-
<struts-config>
-
<data-sources>
-
<data-source type="org.apache.commons.dbcp.BasicDataSource">
-
<set-property
-
property="driverClassName"
-
value="com.mysql.jdbc.Driver" />
-
<set-property
-
property="url"
-
value="jdbc:mysql://localhost:3306/bank_sys?autoReconnect=true" />
-
<set-property
-
property="username"
-
value="root"/>
-
<set-property
-
property="password"
-
value="hr"/>
-
</data-source>
-
</data-sources>
-
<form-beans>
-
<form-bean name="index1Form"
-
type="com.kmg.customer.Index1Form"/>
-
-
<form-bean name="index2Form"
-
type="com.kmg.employee.Index2Form"/>
-
-
<form-bean name="employeeDetailForm"
-
type="com.kmg.employee.employeeDetailForm"/>
-
</form-beans>
-
-
<!-- Action Mappings Configuration -->
-
<action-mappings>
-
<action path="/index1"
-
type="com.kmg.customer.Index1Action"
-
name="index1Form"
-
input="/jsp/index1.jsp">
-
<forward name="accountDetail" path="/jsp/accountDetail.jsp"/>
-
</action>
-
-
<action path="/index2"
-
type="com.kmg.employee.Index2Action"
-
name="index2Form"
-
input="/jsp/index2.jsp">
-
<forward name="employeeDetail" path="/jsp/employeeDetail.jsp"/>
-
</action>
-
-
<action path="/employeeDetail"
-
type="com.kmg.employee.employeeDetailAction"
-
name="employeeDetailForm"
-
input="/jsp/employeeDetail.jsp">
-
</action>
-
-
</action-mappings>
-
</struts-config>
-
-
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
-
-
<servlet>
-
<servlet-name>action</servlet-name>
-
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
-
<init-param>
-
<param-name>config</param-name>
-
<param-value>/WEB-INF/struts-config.xml</param-value>
-
</init-param>
-
<init-param>
-
<param-name>debug</param-name>
-
<param-value>3</param-value>
-
</init-param>
-
<init-param>
-
<param-name>detail</param-name>
-
<param-value>3</param-value>
-
</init-param>
-
<load-on-startup>0</load-on-startup>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>action</servlet-name>
-
<url-pattern>*.do</url-pattern>
-
</servlet-mapping>
-
<welcome-file-list>
-
<welcome-file>/jsp/employeeDetail.jsp</welcome-file>
-
</welcome-file-list>
-
-
-
</web-app>
-
-
I am sorry for posting all the code if its against the community guide lines.
But I really need help. I m SOO lost. How to display data retrieved from database using JSP file ?? Nothing is getting displayed when I run the JSP file.