如何在下面重写我的代码,直接在我的网页上而不是在控制台上打印结果?
public Observable<LinkedList<WeatherInfo>> getWeatherData(final String payload, final TempUnit tempUnit) {
PublishSubject weatherInfoPublisher = PublishSubject.create();
RetrofitProvider.getInstance().getCurrentWeather(payload + ",us", translateTempUnit(tempUnit))
.flatMap(new Function<String, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(String todayResponse) throws Exception {
Log.d(TAG, "Received today weather: " + todayResponse);
parseTodayData(todayResponse, weatherDataList);
return RetrofitProvider.getInstance().getForecastWeather(
payload + ",us", translateTempUnit(tempUnit), FORECAST_DAYS);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onNext(String futureResponse) {
Log.d(TAG, "Received future weather: " + futureResponse);
parseFutureData(futureResponse, weatherDataList);
weatherInfoPublisher.onNext(weatherDataList);
weatherInfoPublisher.onComplete();
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "The error is, " + e.getMessage());
}
@Override
public void onComplete() {
}
});
return weatherInfoPublisher;
}
答案 0 :(得分:2)
一种方法是从servlet响应中获取编写器,然后编写所需的HTML内容。我对您的doSQL()
方法进行了轻微的重构,以接受PrintWriter
作为参数。
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
doSQL(pw);
}
public static void doSQL(PrintWriter pw) {
try {
String url = "jdbc:msql://...";
Connection conn = DriverManager.getConnection(url,"user","password");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
pw.println("<html><table>");
while (rs.next()) {
// you only select one field, but you can easily adapt
// this code to have more fields (i.e. table columns)
String lastName = rs.getString("Lname");
pw.println("<tr><td>" + lastname + "</td></tr>");
}
pw.println("</table></html>");
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
答案 1 :(得分:0)
您需要返回结果集。
public static ResultSet doSQL() {
try {
String url = "jdbc:msql://...";
Connection conn = DriverManager.getConnection(url,"user","password");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
conn.close();
return rs;
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
return null
}
在你的servlet中,获取doSQL();
ResultSet rs = MyStaticFile.doSQL();
int rowCount = 0;
out.println("<P ALIGN='center'><TABLE BORDER=1>");
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
// table header
out.println("<TR>");
for (int i = 0; i < columnCount; i++) {
out.println("<TH>" + rsmd.getColumnLabel(i + 1) + "</TH>");
}
out.println("</TR>");
// the data
while (rs.next()) {
rowCount++;
out.println("<TR>");
for (int i = 0; i < columnCount; i++) {
out.println("<TD>" + rs.getString(i + 1) + "</TD>");
}
out.println("</TR>");
}
out.println("</TABLE></P>");
这不是一个完整的例子,还没有完全测试。这个参考将让你了解如何做到这一点。顺便说一下,不要使用静态进行数据库操作。您可以将其用于学习目的。
答案 2 :(得分:0)
以下是使用GET方法引用servlet响应代码为html的示例,同样可以为POST方法编写。在servlet中添加以下代码。
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws IOException {
PrintWriter out = response.getWriter( );
response.setContentType("text/html");
out.println("<html>");
out.println("<head>");
out.println("<title> Any Title </title>");
out.println("</head>");
out.println("<body>");
out.println("<H1>Last Name from a Servlet</H1></br>");
try {
String url = "jdbc:msql://...";
Connection conn = DriverManager.getConnection(url,"user","password");
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT Lname FROM Customers WHERE Snum = 2001");
while ( rs.next() ) {
// I want to print the ResultSet directly on my HTML page, how may I go about doing that?
String lastName = rs.getString("Lname");
out.println("<H1>"+ lastName +"</H1></br>");
System.out.println(lastName);
}
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
out.println("</body>");
out.println("</html>");
out.close();
}