一、项目背景
随着计算机应用的日益普及和深化,随时随地了解今明天的天气情况已经成为一种趋势。本项目要开发的是基于单机的天气查询系统。由于现代都市的快节奏,再天天守着电视机看雷打不动的天气预报已经变得越来越不现实。因此,实现一个将各种天气查询服务功能集成起来的信息系统就显得十分必要,既可以节省时间,又可以实现个性化的服务,减少不必要的等待和冗余的信息。
开发的系统要求界面友好,方便直观。既要方便管理员对天气情况进行添加、删除、修改、查询和统计等管理,又要方便用户及时了解最新的天气
状况。
二、项目需求分析
1、后台性能需求
后台能实现数据库的增删改查,能够发布当日天气,能及时更改天气状况,能增加、删除、修改日期,还能实现用户修改信息、用户查询天气等功能。
2、功能需求
本次课题为基于mysql数据库的天气查询系统,共有一个端。web端主要为管理员,功能设计图如图2.1所示:
图2.1 功能设计图
(1)用户注册功能
当用户进入主界面没有账号密码时可以点击注册,就可以注册一个新账号。
(2)用户登录功能
当用户进入主界面就可以直接用已有账号密码登录。
(3)用户修改密码
当用户忘记自己的密码,或者当用户想要修改密码的时候,用户可以点击修改密码来修改自己的密码。
(4)用户首页
用户能在这一界面进行天气查询也可以添加忘记密码问题
管理员登录
管理员进入后台后直接输入账号密码登录进入管理员后台。
(5)管理员管理日期
管理员在这一界面可以增加、删除、修改,查找日期,将对应日期添加进去
(6)管理员管理省级地区
管理员在这一界面可以增加、删除、修改,查找省级地区,将对应省级地区添加进去
(7)管理员管理市级地区
管理员在这一界面可以增加、删除、修改,查找市级地区,将对应市级地区添加进去
(8)管理员管理县级地区
管理员在这一界面可以增加、删除、修改,查找县级地区,将对应县级地区添加进去
(9)管理员天气
管理员在这一界面可以增加、删除、修改,查找天气,将对应天气添加进去
三、项目设计
1、数据库设计
2、概要设计
(1)总体设计图
总体设计分为用户和管理员,如图3.1所示,用户分为更改密码、查询天气、添加忘记密码问题,三个页面。管理员主要为后台管理数据,包括添加/删除省级地区信息、添加/修改市级地区信息、添加/修改县级地区信息、天气的查找,删除,修改,增加。
实现图片
数据库选择的是MySql技术,用Navicat for MySQL来管理数据库,通过MySql技术,使界面连接至数据库中,用户可以使用登录注册等功能。而管理员可以对用户数据,资料数据进行动态的管理。 开发工具选择的是IDEA2020版本。 发布工具使用的是Tomcat9.0.38版本。 部分代码展示
AdminDaoImpl
package com.hngy.zp.dao.Impl;
import com.hngy.zp.dao.AdminDao;
import com.hngy.zp.entity.*;
import com.hngy.zp.util.DBUtil_Weather;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class AdminDaoImpl implements AdminDao {
@Override
public Admin selectAdmin(Admin admin) {
Connection connection = DBUtil_Weather.getConnection();
Admin admin1 = null;
String select = "select * from T_admin_zp where admin_name_zp=? and admin_password_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setString(1, admin.getAdmin_name_zp());
preparedStatement.setString(2, admin.getAdmin_password_zp());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
admin1 = new Admin();
admin1.setAdmin_name_zp(resultSet.getString("admin_name_zp"));
admin1.setAdmin_password_zp(resultSet.getString("admin_password_zp"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return admin1;
}
@Override
public int addDate(Date date) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String insert = "insert into T_Date_zp values(null,?)";
try {
PreparedStatement preparedStatement = connection.prepareStatement(insert);
preparedStatement.setString(1, date.getDate_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<Date> selectDatesByDates(String date1, String date2) {
List<Date> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_Date_zp WHERE date_zp BETWEEN ? AND ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setString(1, date1);
preparedStatement.setString(2, date2);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Date date = new Date();
date.setDate_id_zp(resultSet.getInt("date_id_zp"));
date.setDate_zp(resultSet.getString("date_zp"));
list.add(date);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public int updateDatesByDateId(Date date) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String update = "update T_Date_zp set date_zp=? where date_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(update);
preparedStatement.setString(1, date.getDate_zp());
preparedStatement.setInt(2, date.getDate_id_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public int deleteDateByDateId(int date_id_zp) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String delete = "delete from T_Date_zp where date_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(delete);
preparedStatement.setInt(1, date_id_zp);
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public int addProvinceArea(Province province) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String insert = "insert into T_Nationwide_zp values(null,?)";
try {
PreparedStatement preparedStatement = connection.prepareStatement(insert);
preparedStatement.setString(1, province.getProvince_name_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<Province> selectAreaByAreaName(Province province) {
List<Province> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_Nationwide_zp as a where a.province_name_zp like ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setString(1, province.getProvince_name_zp());
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Province province1 = new Province();
province1.setProvince_id_zp(resultSet.getInt("province_id_zp"));
province1.setProvince_name_zp(resultSet.getString("province_name_zp"));
list.add(province1);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public int updateAreaByAreaName(Province province) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String update = "update T_Nationwide_zp set province_name_zp=? where province_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(update);
preparedStatement.setString(1, province.getProvince_name_zp());
preparedStatement.setInt(2, province.getProvince_id_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public int deleteAreaByAreaName(Province province) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String delete = "delete from T_Nationwide_zp where province_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(delete);
preparedStatement.setInt(1, province.getProvince_id_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<Province> selectProvinceAreas() {
List<Province> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_Nationwide_zp";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Province province1 = new Province();
province1.setProvince_id_zp(resultSet.getInt("province_id_zp"));
province1.setProvince_name_zp(resultSet.getString("province_name_zp"));
list.add(province1);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public int addTownArea(Town town) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String insert = "insert into T_City_zp values(null,?,?,?)";
try {
PreparedStatement preparedStatement = connection.prepareStatement(insert);
preparedStatement.setString(1, town.getCity_name_zp());
preparedStatement.setString(2, town.getCity_area_number_zp());
preparedStatement.setInt(3, town.getProvince_id_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<Town> twoSelectAreaByAreaName(String city_name_zp) {
List<Town> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_City_zp as a where a.city_name_zp like ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setString(1, city_name_zp);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Town town = new Town();
town.setCity_id_zp(resultSet.getInt("city_id_zp"));
town.setCity_name_zp(resultSet.getString("city_name_zp"));
town.setCity_area_number_zp(resultSet.getString("city_area_number_zp"));
town.setProvince_id_zp(resultSet.getInt("province_id_zp"));
list.add(town);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public int updateAreaByAreaIdServlet(Town town) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String update = "update T_City_zp set city_name_zp=?,city_area_number_zp=?,province_id_zp=? where city_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(update);
preparedStatement.setString(1, town.getCity_name_zp());
preparedStatement.setString(2, town.getCity_area_number_zp());
preparedStatement.setInt(3, town.getProvince_id_zp());
preparedStatement.setInt(4, town.getCity_id_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<Town> selectCountyAreas() {
List<Town> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_City_zp";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Town town = new Town();
town.setCity_id_zp(resultSet.getInt("city_id_zp"));
town.setCity_name_zp(resultSet.getString("city_name_zp"));
list.add(town);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public int addCountyArea(County county) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String insert = "insert into T_County_zp values(null,?,?,?)";
try {
PreparedStatement preparedStatement = connection.prepareStatement(insert);
preparedStatement.setString(1, county.getCounty_name_zp());
preparedStatement.setString(2, county.getCounty_area_number_zp());
preparedStatement.setInt(3, county.getCity_id_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<County> threeSelectAreaByAreaName(String county_name_zp) {
List<County> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_County_zp as a where a.county_name_zp like ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setString(1, county_name_zp);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
County county = new County();
county.setCounty_id_zp(resultSet.getInt("county_id_zp"));
county.setCounty_name_zp(resultSet.getString("county_name_zp"));
county.setCounty_area_number_zp(resultSet.getString("county_area_number_zp"));
county.setCity_id_zp(resultSet.getInt("city_id_zp"));
list.add(county);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public int deleteAreaByAreaId(int county_id_zp) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String delete = "delete from T_County_zp where county_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(delete);
preparedStatement.setInt(1, county_id_zp);
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public int twoDeleteAreaByAreaId(int city_id_zp) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String delete = "delete from T_City_zp where city_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(delete);
preparedStatement.setInt(1, city_id_zp);
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<Date> selectDates() {
List<Date> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_Date_zp";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Date date = new Date();
date.setDate_id_zp(resultSet.getInt("date_id_zp"));
date.setDate_zp(resultSet.getString("date_zp"));
list.add(date);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public List<County> twoSelectCountyAreas() {
List<County> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_County_zp";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
County county = new County();
county.setCounty_id_zp(resultSet.getInt("county_id_zp"));
county.setCounty_name_zp(resultSet.getString("county_name_zp"));
list.add(county);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public int addWeather(Weather weather) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String insert = "insert into T_Weather_zp values(null,?,?,?,?,?,?)";
try {
PreparedStatement preparedStatement = connection.prepareStatement(insert);
preparedStatement.setFloat(1, weather.getWeather_temperature_zp());
preparedStatement.setString(2, weather.getWeather_quality_zp());
preparedStatement.setString(3, weather.getWeather_state_zp());
preparedStatement.setInt(4, weather.getDate_id_zp());
preparedStatement.setInt(5, weather.getCounty_id_zp());
preparedStatement.setFloat(6, weather.getWeather_temperature_height_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<Weather> queryWeatherByAreaName(String areaName_zp) {
List<Weather> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "SELECT a.* FROM T_Weather_zp as a INNER JOIN T_County_zp as b on a.county_id_zp=b.county_id_zp WHERE b.county_name_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setString(1, areaName_zp);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Weather weather = new Weather();
weather.setWeather_id_zp(resultSet.getInt("weather_id_zp"));
weather.setWeather_temperature_zp(resultSet.getFloat("weather_temperature_zp"));
weather.setWeather_quality_zp(resultSet.getString("weather_quality_zp"));
weather.setWeather_state_zp(resultSet.getString("weather_state_zp"));
weather.setDate_id_zp(resultSet.getInt("date_id_zp"));
weather.setCounty_id_zp(resultSet.getInt("county_id_zp"));
weather.setWeather_temperature_height_zp(resultSet.getFloat("weather_temperature_height_zp"));
list.add(weather);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public List<Weather> queryWeatherByDates(String date1, String date2) {
List<Weather> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select a.* from T_Weather_zp as a INNER JOIN T_Date_zp as b on a.date_id_zp=b.date_id_zp WHERE b.date_zp BETWEEN ? AND ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setString(1, date1);
preparedStatement.setString(2, date2);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Weather weather = new Weather();
weather.setWeather_id_zp(resultSet.getInt("weather_id_zp"));
weather.setWeather_temperature_zp(resultSet.getFloat("weather_temperature_zp"));
weather.setWeather_quality_zp(resultSet.getString("weather_quality_zp"));
weather.setWeather_state_zp(resultSet.getString("weather_state_zp"));
weather.setDate_id_zp(resultSet.getInt("date_id_zp"));
weather.setCounty_id_zp(resultSet.getInt("county_id_zp"));
weather.setWeather_temperature_height_zp(resultSet.getFloat("weather_temperature_height_zp"));
System.out.println(weather.toString());
list.add(weather);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public List<Weather> queryWeatherByDatesAndUserName(String date1, String date2, String areaName_zp) {
List<Weather> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
System.out.println(date1 + date2 + areaName_zp);
String select = "select a.* from (T_Weather_zp as a INNER JOIN T_Date_zp as b on a.date_id_zp=b.date_id_zp) INNER JOIN T_County_zp as c on c.county_id_zp=a.county_id_zp WHERE b.date_zp BETWEEN ? AND ? and c.county_name_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setString(1, date1);
preparedStatement.setString(2, date2);
preparedStatement.setString(3, areaName_zp);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Weather weather = new Weather();
weather.setWeather_id_zp(resultSet.getInt("weather_id_zp"));
weather.setWeather_temperature_zp(resultSet.getFloat("weather_temperature_zp"));
weather.setWeather_quality_zp(resultSet.getString("weather_quality_zp"));
weather.setWeather_state_zp(resultSet.getString("weather_state_zp"));
weather.setDate_id_zp(resultSet.getInt("date_id_zp"));
weather.setCounty_id_zp(resultSet.getInt("county_id_zp"));
weather.setWeather_temperature_height_zp(resultSet.getFloat("weather_temperature_height_zp"));
System.out.println(weather.toString());
list.add(weather);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
@Override
public int deleteWeather(int weather_id_zp) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String delete = "delete from T_Weather_zp where weather_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(delete);
preparedStatement.setInt(1, weather_id_zp);
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public int updateWeather(Weather weather) {
int x = 0;
Connection connection = DBUtil_Weather.getConnection();
String update = "update T_Weather_zp set weather_temperature_zp=?,weather_quality_zp=?,weather_state_zp=?,date_id_zp=?,county_id_zp=?,weather_temperature_height_zp=? where weather_id_zp=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(update);
preparedStatement.setFloat(1, weather.getWeather_temperature_zp());
preparedStatement.setString(2, weather.getWeather_quality_zp());
preparedStatement.setString(3, weather.getWeather_state_zp());
preparedStatement.setInt(4, weather.getDate_id_zp());
preparedStatement.setInt(5, weather.getCounty_id_zp());
preparedStatement.setFloat(6, weather.getWeather_temperature_height_zp());
preparedStatement.setInt(7, weather.getWeather_id_zp());
x = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return x;
}
@Override
public List<Weather> selectWeather(int county_id_zp) {
List<Weather> list = new ArrayList<>();
Connection connection = DBUtil_Weather.getConnection();
String select = "select * from T_Weather_zp where county_id_zp=?";
System.out.println(county_id_zp);
try {
PreparedStatement preparedStatement = connection.prepareStatement(select);
preparedStatement.setInt(1, county_id_zp);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Weather weather = new Weather()