用于实现对数据库中的数据进行访问并获取,将获取的数据转化成JSON数组
引入TomCat
链接数据库配置JDBCUntil
//mysql 5 和 8
//连接数据库四要素有没有变化?
//driver 5 com.mysql.jdbc.Driver 8 com.mysql.cj.jdbc.Driver
//url 5 可以不加任何参数 8 有的需要添加时区。 shanghai
//username root
//password root
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/pokemon?useSSL=false";
private static String username="root";
private static String password="justin";
static { //只会执行一次。
try {
//直接进行类加载 加载驱动 只需要加载一次。
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//工厂模式 创建一个方法获取链接对象
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,username,password);
}
Servlet项目创建
创建实体类
使用lombok可以简化shux
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
实体类对象
@Data
public class Fication {
private String classfication;
private Integer pokemon_count;
}
创建DAO包
public interface GoodsDAO {
//传说级宝可梦的六种属性值
public List<Goods> findattackTop5() throws SQLException;
//各个世代的宝可梦数量
public List<pokecount> findpokemon() throws SQLException;
//所有宝可梦的身高体重属性
public List<shuxin> findAll() throws SQLException;
//宝可梦为男性的概率
public List<Male> findsex() throws SQLException;
//宝可梦各个主属性的数量
public List<Type> findtype() throws SQLException;
//宝可梦捕捉概率和种族值
public List<Capture> findRate() throws SQLException;
//宝可梦的精灵类型
public List<Fication> findFaction() throws SQLException;
}
dao的实现类
public class GoodsDAOImpl implements GoodsDAO {
private Connection conn=null;
@Override
public List<Goods> findattackTop5() throws SQLException {
// 获取数据库连接
this.conn= DButil.getConnection();
// 查询数据库中 pokecount 表中的数据并返回
return DButil.queryList(this.conn, Goods.class, "select * from legendary order by rand () limit 5 ");
}
@Override
public List<pokecount> findpokemon() throws SQLException {
this.conn= DButil.getConnection();
return DButil.queryList(this.conn, pokecount.class, "select * from pokecount");
}
@Override
public List<shuxin> findAll() throws SQLException {
this.conn= DButil.getConnection();
return DButil.queryList(this.conn, shuxin.class, "select * from pokeshuxin");
}
@Override
public List<Male> findsex() throws SQLException {
this.conn= DButil.getConnection();
return DButil.queryList(this.conn, Male.class, "select * from male");
}
@Override
public List<Type> findtype() throws SQLException {
this.conn= DButil.getConnection();
return DButil.queryList(this.conn, Type.class, "select * from type");
}
@Override
public List<Capture> findRate() throws SQLException {
this.conn=DButil.getConnection();
return DButil.queryList(this.conn,Capture.class,"select * from rate ORDER BY rand() LIMIT 10;");
}
@Override
public List<Fication> findFaction() throws SQLException {
this.conn=DButil.getConnection();
return DButil.queryList(this.conn,Fication.class,"select * from faction ORDER BY rand() LIMIT 6");
}
}
创建services包
接口代码和dao包一样
service的实现类
public class GoodsServiceImpl implements GoodsService {
private GoodsDAO goodsDAO=new GoodsDAOImpl();
@Override
public List<Goods> findattackTop5() throws SQLException {
return goodsDAO.findattackTop5();
}
@Override
public List<pokecount> findpokemon() throws SQLException {
return goodsDAO.findpokemon();
}
@Override
public List<shuxin> findAll() throws SQLException {
return goodsDAO.findAll();
}
@Override
public List<Male> findMale() throws SQLException {
return goodsDAO.findsex();
}
@Override
public List<Type> findType() throws SQLException {
return goodsDAO.findtype();
}
@Override
public List<Capture> findRate() throws SQLException {
return goodsDAO.findRate();
}
@Override
public List<Fication> findFaction() throws SQLException {
return goodsDAO.findFaction();
}
}
创建Controller包
将获取的数据库数据转化成JSON数组
pom.xml中导入json包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.37</version>
</dependency>
创建Controller类
// 使用@WebServlet注解指定Servlet的URL映射
@WebServlet("/findtop.do")
public class FandGoodsServlet extends HttpServlet {
// 创建GoodsService对象并初始化
private GoodsService goodsService = new GoodsServiceImpl();
// 处理HTTP GET请求
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
List<Goods> all = null;
try {
// 调用goodsService的findattackTop5方法来获取数据
all = goodsService.findattackTop5();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
// 将获取到的数据转换成JSON字符串
String s = JSON.toJSONString(all);
// 在控制台打印JSON字符串
System.out.println("s = " + s);
// 将JSON字符串写入HTTP响应
resp.getWriter().write(s);
}
}