动态显示页面


用两种方式在页面上动态显示内容。有待改善!谢谢赐教 !

package dao;

import java.sql.*;

public class DBTools {

public static Connection getConnection()
{
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora", "scott", "tiger");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

}


public class personPO {
String name;
int age ;
String sex ;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import po.personPO;

public class ArrayListDAO {
ArrayList list = null;

public ArrayList selectOracle() {
try {
list = new ArrayList();
DBTools tool = new DBTools();

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dao.ArrayListDAO;

/**
* MyEclipse Struts Creation date: 08-29-2008
*
* XDoclet definition:
*
* @struts.action path="/testStruts" name="testStrutsForm"
*                input="/form/testStruts.jsp" scope="request" validate="true"
*/
public class TestStrutsAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ArrayListDAO dao = new ArrayListDAO();
ArrayList list = dao.selectOracle();
request.setAttribute("list", list);
try {
request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
} catch (ServletException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
return mapping.findForward("MyJsp");
}
}

   name  sex  age


name sex age
ArrayList list = (ArrayList) request.getAttribute("list"); for (int i = 0; i < list.size(); i++) { personPO po = new personPO(); po.getName() po.getSex() po.getAge() }









Connection con = tool.getConnection();
String sql = "select * from person";
PreparedStatement ste = con.prepareStatement(sql);
ResultSet set = ste.executeQuery();
while (set.next()) {
personPO po = new personPO();
po.setName(set.getString("name"));
po.setAge(set.getInt("age"));
po.setSex(set.getString("sex"));
list.add(po);
}
} catch (SQLException e) {

e.printStackTrace();
}
return list;
}
}

你可能感兴趣的:(java,DAO,sql,struts,MyEclipse)