Results 1 to 2 of 2
  1. #1
    Turk80 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Exclamation Can someone please explain to me the proper way to display the result of a SQL query

    I keep getting stuck on this.

    Here is my servlet:
    Java Code:
    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>
    
    }

    I'd like to know the correct way to post the result of this query in the servlet:
    Java Code:
    String query =
                    "SELECT sum(cost) FROM stocks where username = 'Bora'";
    after the is
    Java Code:
    out.println("Your total cost is" );
    If someone could provide me that information you will seriously make my night. Thanks everyone.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I recommend you use a PreparedStatement.

    You have your ResultSet, perhaps you should see what methods that class provides.

Similar Threads

  1. Replies: 1
    Last Post: 05-20-2010, 07:58 AM
  2. Replies: 2
    Last Post: 04-07-2009, 06:25 AM
  3. column result query
    By vikashkumar in forum JDBC
    Replies: 0
    Last Post: 04-03-2009, 02:23 PM
  4. Help!! how to turn query result to string
    By stefanybaez in forum Advanced Java
    Replies: 2
    Last Post: 12-11-2008, 11:42 AM
  5. Export query result, procces is to slow
    By Daniel in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 06-28-2007, 06:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •