使用MVC的思想,结合Servlet和JSP进行查询操作
步骤1:实体类Hero
步骤2:HeroDAO
步骤3:HeroListServlet
步骤4:web.xml
步骤5:listHero.jsp
步骤 1 : 实体类Hero
package bean; public class Hero {
public int id; public String name; public float hp; public int damage; public int getId() {
return id; } public void setId( int id) {
this .id = id; } public String getName() {
return name; } public void setName(String name) {
this .name = name; } public float getHp() {
return hp; } public void setHp( float hp) {
this .hp = hp; } public int getDamage() {
return damage; } public void setDamage( int damage) {
this .damage = damage; } } |
步骤 2 : HeroDAO
使用在前面教程中用到的HeroDAO用于从数据库查询数据
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import bean.Hero; public class HeroDAO {
public HeroDAO() {
try {
Class.forName( "com.mysql.jdbc.Driver" ); } catch (ClassNotFoundException e) {
e.printStackTrace(); } } public Connection getConnection()
|