📄 weathercontent.java
字号:
import java.io.*;
import java.text.*;
import java.sql.*;
import java.util.*;
public class WeatherContent
{
String[] m_weatherLow24 = new String[5] ;
String[] m_weatherHigh24 = new String[5];
String[] m_weatherCondition24 = new String[5];
String[] m_weatherLow48 = new String[5];
String[] m_weatherHigh48 = new String[5];
String[] m_weatherCondition48 = new String[5];
String[] m_city=new String[5];
String m_today ;
String m_tomorrow;
int cityNum;
WeatherContent()
{
int i=0;
for (i=0;i<5;i++){
m_weatherLow24[i]="";
m_weatherHigh24[i]="";
m_weatherCondition24[i]="";
m_weatherLow48[i]="";
m_weatherHigh48[i]="";
m_weatherCondition48[i]="";
m_city[i]="";
}
m_today="";
m_tomorrow="";
cityNum=0;
}
public void getWeather(String userName)
{
//取得用户名
/*根据m24、n24、n48表中的最大的time做为查询的时间条件
将一天的时间分为三个时间段,
a、0点-8:30查询表n24及n48,时间条件为昨天的时间
b、8:30-18:00查询表m24,时间为当天的时间
c、18:00-24查询表n24、查询时间条件为今天的时间*/
//查询用户定制的城市信息,将其存入城市数组arrCity[]中
int j=0;
int i=0;
for (j=0;j<5;j++)
{
m_city[j] = new String ();
// weatherContent[j] = new WeatherContent();
}
DBOperater DB = new DBOperater ("ps_weather");
ResultSet rs= DB.executeQuery ("select Qx_dz from ps_weather where user_name='"+ userName +"'" );//erson.getName()+"'";
if (rs!=null){
try
{
if (rs != null){
while(rs.next())
{
m_city[cityNum] = rs.getString(1);
if (m_city[cityNum]==null)
m_city[cityNum]="";
else
m_city[cityNum]=m_city[cityNum].trim();
cityNum++;
}
}
}
catch(Exception e) {
}
}
DB.close();
if (cityNum == 0){
m_city[cityNum] = "北京";
// weatherContent[cityNum].getWeather (city[cityNum]);
cityNum++;
}
//根据城市信息及时间信息查询符合条件的今天的天气信息
String weatherTime="";//查询时间
String tableName = "";//要查询的表名"m24"or"n24"
int nHour,nMonth,nYear;
Calendar currentTime = Calendar.getInstance ();
nHour = currentTime.get (currentTime.HOUR_OF_DAY );
/* nMonth = currentTime.get (currentTime.MONTH ) + 1 ;
nYear = currentTime.get (currentTime.YEAR );
nDay = currentTime.get (currentTime.DATE );*/
//根据不同的条件得到天气情况
if ((nHour > 8.5)&&(nHour <18))
{
tableName="m24";
}
else
{
tableName="n24";
}
//开始查24小时天气预报
String queryString="",queryStringT24="";
DBOperater DBT24= new DBOperater ("weather");
ResultSet rsT24 = DBT24.executeQuery ("select time from " + tableName+" ORDER BY time DESC ");
if (rsT24 !=null){
try
{ if (rsT24.next ())
{
weatherTime= rsT24.getString (1);
if (weatherTime==null)
weatherTime="";
else
weatherTime=weatherTime.trim();
}
//生成第一天的日期形式
m_today=weatherTime.substring(6) ;
int rq1=0;
if (tableName == "n24")
{
// rq1 = Integer.valueOf (m_today,10).intValue ();
//rq1 = rq1 +1;
//m_today = rq1+"日";
m_today = "明天";
}
else
{
m_today = "今天";
//m_today= m_today+"日";
}
}
catch (Exception e){
}
}
DBT24.close();
DBOperater DB24 = new DBOperater ("weather24");
for (i=0;i<cityNum;i++){
ResultSet rs24 =DB24.executeQuery ( "select condition , high ,low from " + tableName +" where city='"+m_city[i]+"'"+" and time='" + weatherTime+"'");
if (rs24!=null){
try{
while(rs24.next())
{
m_weatherCondition24[i] = rs24.getString (1);
m_weatherHigh24[i] = rs24.getString(2) + "℃";
m_weatherLow24[i] = rs24.getString (3)+ "℃-";
}
}
catch (Exception e){
}
}
}
DB24.close();
//得到第二天的天气情况
DBOperater DBT48 = new DBOperater ("weather");
ResultSet rsT48 = DBT48.executeQuery("select time from n48 ORDER BY time DESC ");
if (rsT48 != null){
try
{ if (rsT48.next ())
{
weatherTime= rsT48.getString (1);
if (weatherTime==null)
weatherTime ="";
else
weatherTime=weatherTime.trim();
int rq2;
m_tomorrow = weatherTime.substring (6);
if (m_today=="今天")
m_tomorrow = "明天";
else
m_tomorrow= "后天";
}
}catch(Exception e){
}
}
DBT48.close();
DBOperater DB48 = new DBOperater ("weather48");
for (i=0;i<cityNum;i++)
{
ResultSet rs48=DB48.executeQuery ( "select condition , high ,low from n48 where city='"+m_city[i]+"'"+" and time='" + weatherTime+"'");
if (rs!=null){
try{
while(rs48.next())
{
m_weatherCondition48[i] = rs48.getString (1);
m_weatherHigh48[i] = rs48.getString(2) + "℃";
m_weatherLow48[i] = rs48.getString (3) +"℃-";
}
}
catch (Exception e){
}
}
}
DB48.close();
}
public String getWeatherLow24(int i) {
if (m_weatherLow24[i] == null)
m_weatherLow24[i]="";
return m_weatherLow24[i];
}
public String getWeatherHigh24(int i) {
if (m_weatherHigh24[i] == null)
m_weatherHigh24[i]="";
return m_weatherHigh24[i];
}
public String getWeatherCondition24(int i) {
if (m_weatherCondition24[i] == null || m_weatherCondition24[i]=="")
m_weatherCondition24[i]="未知";
return m_weatherCondition24[i];
}
public String getWeatherLow48(int i) {
if (m_weatherLow48[i] == null )
m_weatherLow48[i]="";
return m_weatherLow48[i];
}
public String getWeatherHigh48(int i) {
if (m_weatherHigh48[i] == null)
m_weatherHigh48[i]="";
return m_weatherHigh48[i];
}
public String getWeatherCondition48(int i) {
if (m_weatherCondition48[i] == null || m_weatherCondition48[i] == "")
m_weatherCondition48[i] = "未知";
return m_weatherCondition48[i];
}
public String getWeatherToday(){
if (m_today==null)
m_today="";
return m_today;
}
public String getWeatherTomorrow(){
if (m_tomorrow == null)
m_tomorrow ="";
return m_tomorrow;}
public String getCity(int i){
if (m_city[i]==null)
m_city[i]="";
return m_city[i];
}
public int getCityNum(){
return cityNum;
}
/*public String makeChannel(Person p)
{
return null;
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -