2010-12-10 67 views
1

我有一個查詢MySQL數據庫的Jsp頁面,我想將結果集對象作爲響應對象發送到一個HTML頁面?我需要結果集對象來填充表和圖表。如何將jsp中的ResultSet對象發送回html(javascript)?

1.如何將resultSet對象轉換爲JavaScript對象?

  1. 我如何從JSP發送ResultSet對象以HTML嗎?(我指的是語法)

我使用XMLHttpRequest的獲取調用JSP頁面

回答

3

不要使用JSP。使用查詢數據庫的Servlet,獲得結果的List,並將其轉換爲JS可以無縫使用的JSON字符串。

首先創建一個代表數據庫表單行的javabean類。例如。 Product

public class Product { 
    private Long id; 
    private String name; 
    private String description; 
    private BigDecimal price; 

    // Add/generate c'tors, getters, setters and other boilerplate. 
} 

將創建一個DAO類火災的查詢和ResultSet映射到List<Product>

public class ProductDAO { 

    // ... 

    public List<Product> find(String search) throws SQLException { 
     Connection connection = null; 
     PreparedStatement statement = null; 
     ResultSet resultSet = null; 
     List<Product> products = new ArrayList<Product>(); 

     try { 
      connection = database.getConnection(); 
      statement = connection.prepareStatement(SQL_FIND); 
      statement.setString(1, search); 
      resultSet = statement.executeQuery(); 
      while (resultSet.next()) { 
       Product product = new Product(); 
       product.setId(resultSet.getLong("id")); 
       product.setName(resultSet.getString("name")); 
       product.setDescription(resultSet.getString("description")); 
       product.setPrice(resultSet.getBigDecimal("price")); 
       products.add(product); 
      } 
     } finally { 
      if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {} 
      if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {} 
      if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {} 
     } 

     return products; 
    } 
} 

然後創建它使用DAO類來獲取產品並將其轉換爲一個JSON字符串的Google Gson一點幫助一個Servlet類。

public class ProductServlet extends HttpServlet { 

    // ... 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     try { 
      List<Product> products = productDAO.find(request.getParameter("search")); 
      response.setContentType("application/json"); 
      response.setCharacterEncoding("UTF-8"); 
      response.getWriter().write(new Gson().toJson(products)); 
     } catch (SQLException e) { 
      throw new ServletException("DB error", e); 
     } 
    } 
} 

地圖這個servlet在web.xml/productsurl-pattern和如下(我使用jQuery,因爲它讓你最終有10倍的JavaScript代碼消除crossbrowsersensitive樣板)調用它在JavaScript。

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <title>SO question 4407861</title> 
     <script src="http://code.jquery.com/jquery-latest.min.js"></script> 
     <script> 
      $(document).ready(function() { 
       $('#searchform').submit(function() { 
        $.getJSON("products", $(this).serialize(), function(products) { 
         var table = $('#resulttable'); 
         $.each(products, function(index, product) { 
          $('<tr>').appendTo(table) 
           .append($('<td>').text(product.id)) 
           .append($('<td>').text(product.name)) 
           .append($('<td>').text(product.description)) 
           .append($('<td>').text(product.price)); 
         }); 
        }); 
        return false; 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <form id="searchform"> 
      <input type="text" name="search"> 
      <input type="submit"> 
     </form> 
     <table id="resulttable"></table> 
    </body> 
</html> 
+0

感謝您的代碼片段,它真的幫助我 – sanre6 2010-12-22 07:30:42

相關問題