📄 cityweatherreportserver.java
字号:
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
public class cityWeatherReportServer extends UnicastRemoteObject implements cityWeatherReportInterface
{
/* 这是“城市天气预报”的服务器程序,用于响应客户端的请求 */
Connection con1 = null; //定义数据库连接对象
Connection con2 = null;
Statement stmt1 = null; //定义数据库声明对象
Statement stmt2 = null;
ResultSet rset1 = null; //定义记录结果集对象
ResultSet rset2 = null;
private String cityName = null;
private cityWeatherReportServer() throws RemoteException
{
super();
}
public cityWeatherReportServer(String cityName) throws RemoteException
{
super();
this.cityName = cityName;
}
public String getCityTemperature() throws RemoteException
{
/* 处理城市天气温度的查询请求 */
String ResponseStr = null;
String sSQLCmd = null;
int Recordset1 = 0;
int Recordset2 = 0;
String HighTemperature = null;
String LowTemperature = null;
if(cityName.equals("") || cityName == null || cityName.length() < 1)
{
ResponseStr = "error";
}
else
{
sSQLCmd = "SELECT HighTemperature, LowTemperature FROM CityWeather WHERE CityName = '"+ cityName +"'";
try
{
/* 建立与后台数据库服务器的连接 */
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
con1 = DriverManager.getConnection("jdbc:odbc:DemoDB","demo","demo");
stmt1 = con1.createStatement ();
rset1 = stmt1.executeQuery (sSQLCmd);
while(rset1.next())
{
Recordset1 = rset1.getInt(1);
Recordset2 = rset1.getInt(2);
HighTemperature = Integer.toString(Recordset1);
LowTemperature = Integer.toString(Recordset2);
ResponseStr = "(最高气温)" + HighTemperature + "℃" + " (最低气温)" + LowTemperature + "℃";
}
rset1.close();
con1.close();
}
catch(SQLException e)
{
ResponseStr = "error";
System.out.println("!服务器端数据库连接出错:" + e.toString());
}
}
return ResponseStr;
}
public String getWeatherForecast() throws RemoteException
{
/* 处理城市天气情况的查询请求 */
String ResponseStr = null;
String sSQLCmd = null;
String Recordset1 = null;
String WeatherInfo = null;
if(cityName.equals("") || cityName == null || cityName.length() < 1)
{
ResponseStr = "error";
}
else
{
sSQLCmd = "SELECT WeatherInfo FROM CityWeather WHERE CityName = '"+ cityName +"'";
try
{
/* 建立与后台数据库服务器的连接 */
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
con2 = DriverManager.getConnection("jdbc:odbc:DemoDB","demo","demo");
stmt2 = con2.createStatement ();
rset2 = stmt2.executeQuery (sSQLCmd);
while(rset2.next())
{
Recordset1 = rset2.getString(1);
WeatherInfo = (Recordset1 == null) ? "不详" : Recordset1;
ResponseStr = WeatherInfo;
}
rset2.close();
con2.close();
}
catch(SQLException e)
{
ResponseStr = "error";
System.out.println("!服务器端数据库连接出错:" + e.toString());
}
}
return ResponseStr;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -