我一直在這卡住。如何將sql查詢的結果發佈到servlet中?
這裏是我的servlet:
package HWpackage;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoData extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Demo of Data Persistence</title>");
out.println("</head>");
out.println("<body>");
try {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@coit-ora01.uncc.edu:1521:class";
String username = "johnR";
String password = "lAylko0K";
// Load database driver if it's not already loaded.
Class.forName(driver);
// Establish network connection to database.
Connection connection =
DriverManager.getConnection(url, username, password);
// Create a statement for executing queries.
Statement statement = connection.createStatement();
String query =
"SELECT sum(cost) FROM stocks where username = 'Bora'";
// Send query to database and store results.
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next()) {
out.println("Your total cost is");
}
connection.close();
} catch(ClassNotFoundException cnfe) {
System.err.println("Error loading driver: " + cnfe);
// Useful when you debug your program on the coit server
// where System.out.println("") becomes futile
out.println("<tr> Error loading driver: " + cnfe);
out.println("</tr>");
} catch(SQLException sqle) {
System.err.println("Error with connection: " + sqle);
// Again, for debug purpose
out.println("<tr> Error with connection: " + sqle);
out.println("</tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我想知道發佈此查詢在servlet的結果正確的方法:
String query =
"SELECT sum(cost) FROM stocks where username = 'Bora'";
後是
out.println("Your total cost is");
如果有人可以提供給我的信息,你會認真做我的夜晚。感謝大家。
我不明白是什麼問題,請您詳細說明一下吧?你是什麼意思'將SQL查詢的結果發佈到servlet'? – 2011-04-11 00:42:35