毕业设计-1.13

情况概述:

  今天完成了接口类的实现,并完成了简单测试。

代码如下:

WeatherDaoImpl.Java

package com.zlc.dao.impl;

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 com.mysql.jdbc.Statement;
/**  
* @author Zhao Lucang
* @version 创建时间:2021年2月26日 下午4:07:57  
* 类说明  
*/
import com.zlc.dao.IWeatherDao;
import com.zlc.entity.WeatherBean;
import com.zlc.entity.WeatherCountBean;
import com.zlc.util.CommonMethod;

public class WeatherDaoImpl implements IWeatherDao {
    Connection conn = null;
    PreparedStatement ps = null;

    // 查询此ID是否存在
    @Override
    public boolean isExist(int ID) {
        // TODO Auto-generated method stub
        return queryWeatherByID(ID) == null ? false : true;
    }

    // 根据编号查询天气
    @SuppressWarnings("restriction")
    @Override
    public WeatherBean queryWeatherByID(int ID) {
        WeatherBean weathers = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM table_city_degree_2011 WHERE ID=?";
        // System.out.println(sql);
        try {
            Object[] params = { ID };
            rs = CommonMethod.executeQuery(sql, params);
            if (rs.next()) {
                int id = rs.getInt("ID");
                String province = rs.getString("Province");
                String city = rs.getString("City");
                int date = rs.getInt("Date");
                String week = rs.getString("Week");
                int mind = rs.getInt("MinD");
                int maxd = rs.getInt("MaxD");
                String weather = rs.getString("Weather");
                String winddirection = rs.getString("WindDirection");
                String windforce = rs.getString("WindForce");
                weathers = new WeatherBean(id, province, city, date, week, mind, maxd, weather, winddirection,
                        windforce);
            }
            return weathers;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } finally {
            try {
                CommonMethod.closeAll(rs, (Statement) ps, CommonMethod.conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 根据地点,时间查询
    @SuppressWarnings("restriction")
    @Override
    public WeatherBean queryWeatherByCityDate(String City, int Date) {
        WeatherBean weathers = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sub_date = String.valueOf(Date).substring(0, 4);
        String sql = "SELECT * FROM table_city_degree_" + sub_date + " WHERE City=? AND Date=?";
        // System.out.println(sql);
        try {
            Object[] params = { City, Date };
            rs = CommonMethod.executeQuery(sql, params);
            if (rs.next()) {
                int id = rs.getInt("ID");
                String province = rs.getString("Province");
                String city = rs.getString("City");
                int date = rs.getInt("Date");
                String week = rs.getString("Week");
                int mind = rs.getInt("MinD");
                int maxd = rs.getInt("MaxD");
                String weather = rs.getString("Weather");
                String winddirection = rs.getString("WindDirection");
                String windforce = rs.getString("WindForce");

                weathers = new WeatherBean(id, province, city, date, week, mind, maxd, weather, winddirection,
                        windforce);
            }
            return weathers;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } finally {
            try {
                CommonMethod.closeAll(rs, (Statement) ps, CommonMethod.conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 根据时间遍历全国
    @SuppressWarnings("restriction")
    @Override
    public List<WeatherBean> queryAllWeathersByDate(int Date) {
        // TODO Auto-generated method stub
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sub_date = String.valueOf(Date).substring(0, 4);
        String sql = "SELECT * FROM table_city_degree_" + sub_date + " WHERE Date=?";
        List<WeatherBean> weathers = new ArrayList<WeatherBean>();
        // System.out.println(sql);
        try {
            Object[] params = { Date };
            rs = CommonMethod.executeQuery(sql, params);
            while (rs.next()) {
                // 实例化StudentBean
                WeatherBean weather = new WeatherBean();
                weather.setID(rs.getInt("ID"));
                weather.setProvince(rs.getString("Province"));
                weather.setCity(rs.getString("City"));
                weather.setDate(rs.getInt("Date"));
                weather.setWeek(rs.getString("Week"));
                weather.setMinD(rs.getInt("MinD"));
                weather.setMaxD(rs.getInt("MaxD"));
                weather.setWeather(rs.getString("Weather"));
                weather.setWindDirection(rs.getString("WindDirection"));
                weather.setWindForce(rs.getString("WindForce"));
                weathers.add(weather);
            }
            return weathers;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } finally {
            try {
                CommonMethod.closeAll(rs, (Statement) ps, CommonMethod.conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/zlc364624/p/14461008.html