zoukankan      html  css  js  c++  java
  • 毕业设计-1.10

    情况概述:

      完成了基本方法的编写,包括查询ID,查询地区时间天气情况,查询某日各个地区的天气情况。

    代码如下:

    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.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();
                }
            }
        }
    
    }

    Test.java

    package com.zlc.test;   
    import java.util.ArrayList;
    import java.util.List;
    
    import com.zlc.dao.IWeatherDao;
    import com.zlc.dao.impl.WeatherDaoImpl;
    import com.zlc.entity.WeatherBean;
    import com.zlc.service.IWeatherService;
    import com.zlc.service.impl.WeatherServiceImpl;
    import com.zlc.util.CommonMethod;
    /**  
    * @author Zhao Lucang
    * @version 创建时间:2021年2月26日 下午4:33:18  
    * 类说明  
    */
    public class Test {
        public static void main(String[] args) {
            CommonMethod common=new CommonMethod();
            IWeatherService service=new WeatherServiceImpl();
            IWeatherDao dao=new WeatherDaoImpl();
    
            //WeatherBean weathers=service.queryWeatherByID(1);
            //WeatherBean weathers=service.queryWeatherByCityDate("北京市",20150506);
            List<WeatherBean> weathers = new ArrayList<WeatherBean>();
            weathers=service.queryAllWeathersByDate(20150506);
            for(WeatherBean w:weathers) {
                System.out.println(w.getID()+" "+w.getProvince()+" "+w.getCity()+" "+w.getDate()+" "+w.getWeek()+" "+w.getWeather()+" "+w.getMinD()+" "+w.getMaxD()+" "+w.getWindDirection()+" "+w.getWindForce());
    
            }
            //System.out.println(weathers.getID()+" "+weathers.getProvince()+" "+weathers.getCity()+" "+weathers.getDate()+" "+weathers.getWeek()+" "+weathers.getWeather()+" "+weathers.getMinD()+" "+weathers.getMaxD()+" "+weathers.getWindDirection()+" "+weathers.getWindForce());
        }
    }
     

    结果如下:

  • 相关阅读:
    控件视图的实现原理
    建造者模式
    leetcode701
    leetcode991
    leetcode990
    leetcode989
    leetcode988
    leetcode987
    leetcode986
    leetcode985
  • 原文地址:https://www.cnblogs.com/zlc364624/p/14453660.html
Copyright © 2011-2022 走看看