日期:2014-05-17 浏览次数:20790 次
package ffn.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DAOUtil {
public static final String URL="jdbc:mysql://localhost/ffn?useunicode=true&charset=utf8";
public static final String USER="root";
public static final String PASSWORD="111111";
private static Connection connection = null;
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection(URL,USER, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
package ffn.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ffn.entity.Food;
import ffn.util.DAOUtil;
public class FoodDAO {
public static Connection connection;
private static ResultSet rs;
public static List<Food> getFoodList(){
ArrayList<Food> list=new ArrayList<Food>();
try {
connection=DAOUtil.getConnection();
PreparedStatement ps=connection.prepareStatement("select * from food");
rs=ps.executeQuery();
while(rs.next()){
Food food=new Food();
food.setId(rs.getInt(1));
food.setName(rs.getString(2));
food.setDesc(rs.getString(3));
food.setPic(rs.getString(4));
food.setShopId(rs.getInt(5));
food.setPrice(rs.getDouble(6));
list.add(food);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
*在本类测试没问题呀,可以查询,可以打印
*/
public static void main(String[] args) {
FoodDAO dao=new FoodDAO();
List<Food> list=dao.getFoodList();
for(Food food:list){
System.out.println(food.getName()+":"+food.getDesc());
}
}
}
package ffn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ffn.dao.FoodDAO;
import ffn.entity.Food;
public class IndexServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServl