Hi, I am making a program in Struts 1.3.8 in which i have to access data from mysql. I am able to access data but data is not coming in tabular form. These are my action class and jsp page with iterator.
SearchDataAction.java
package com.myapp.struts;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class SearchDataForm extends org.apache.struts.action.ActionForm {
class user {
String users;
String value;
}
int userId;
Connection con ;
ResultSet rs = null ;
public int getuserId() {
return userId;
}
public void setuserId(int userId) {
this.userId = userId;
}
public SearchDataForm() {
super();
}
@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
if (userId < 1) {
errors.add("userId", new ActionMessage("error.userId.required"));
}
List<String> listMsg = new ArrayList<String>();
try {
String str = "select * from emp where userId = ? " ;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:///employee", "root", "1234");
PreparedStatement ps = (PreparedStatement) con.prepareStatement(str);
ps.setInt(1,userId);
rs = ps.executeQuery();
int count = 0;
while(rs.next()) {
count++;
listMsg.add("UserId"+" "+ rs.getInt("userId"));
listMsg.add("FirstName"+" "+ rs.getString("firstName"));
listMsg.add("LastName"+" "+ rs.getString("lastName"));
listMsg.add("Age"+" "+ rs.getInt("age"));
listMsg.add("Number"+" " +rs.getLong("number"));
}
if(count==0)
{
listMsg.add("userId"+"\t\t"+userId+"\t\t"+"not found");
}
rs.close();
ps.close();
con.close();
}
catch(SQLException e)
{
errors.add("SQLException", new ActionMessage("error.SQLException"));
}
catch(Exception ex)
{
ex.printStackTrace();
}
request.setAttribute("listMsg", listMsg);
return errors;
}
}
search.jsp
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head> <title> Search </title>
</head>
<body>
<h1> Search </h1>
<table> <tbody>
<logic:iterate name="listMsg" id="listMsgId">
<tr>
<bean:write name="listMsgId"/><br>
</tr>
</logic:iterate>
</tbody>
</table>
<p> Now, return back to <a style="cursor : auto; color: green" href="welcomeStruts.jsp"> home </a>page</p>
</body>
</html>
thanks.