2014-03-02 66 views
0

我有這個類:調用帶參數的構造函數在JSP

public class Interets { 
    static Statement St ; 
    public ResultSet rs; 

    public Interets(Integer IdUser) throws SQLException, ServletException, IOException{ 
     String res=" "; 


    try{ 

     ResultSet result = St.executeQuery("SELECT description FROM interets, avoir, consomateur WHERE avoir.id_interet=interets.id_interets AND avoir.id_user=consomateur.code_bar AND consomateur.code_bar="+IdUser+""); 

      ResultSetMetaData resultMeta = (ResultSetMetaData) result.getMetaData(); 


      while(result.next()){   
       String Newligne=System.getProperty("line.separator"); 
      for(int i = 1; i <= resultMeta.getColumnCount(); i++) 

      res=res+Newligne+result.getObject(i).toString(); 

       System.out.println(res); 

      } 
     } 
     catch (Exception e) { 
      System.out.println("Erreur dans la requete d'affichage "); 
     } 

    } 
} 

我想調用它的構造在JSP這樣的:

<div id="corps"><h1> 
<%Interets inte=new Interets(${IdUser})%> 
    </h1> 
</div> 

但我發現了語法錯誤一樣

) 「令牌語法錯誤 」「,;預計」

「上標記語法錯誤,錯置結構」

,所以我怎麼能做到這一點。 謝謝

+0

我已經在我的servlet這個方法:\t公共無效doInteret(HttpServletRequest的請求,HttpServletResponse的響應)拋出的SQLException,了ServletException,IOException異常{ \t \t \t \t \t ID用戶=(整數)會話。的getAttribute( 「ID用戶所」); \t \t Interets inte = new Interets(); \t \t} – user3365621

回答

0

由於您使用<% ... %>,你應該寫有效 Java代碼,讓你在你的語句結尾缺少一個分號;

<%Interets inte=new Interets(${IdUser})%> 
             ^here you're missing a semicolon... 

而且,你不能混用EL與小腳本,所以代碼應該是:

<% 
    Integer idUser = (Integer)request.getAttribute("IdUser"); 
    //if the attribute is stored in HttpSession (probably) 
    //or ServletContext (oddly), use the right method to get it 
    Interets inte =new Interets(idUser); 
%> 

只是要注意,這可能會解決您當前編輯的問題,但也有在當前的DESI幾個問題gn例如你不初始化Statement任何地方你Interets類,但依然在構造函數中使用它......


你應該學會基本的Java和JDBC的基本首次嘗試打開到數據庫的連接之前並打印結果在網絡環境中。我不會深入到這些主題,而是寫一個可能的解決方案,以你目前的代碼,我會評論了錯誤的代碼,以便您注意到什麼是錯的,或者你不應該做的:

public class Interets { 
    //static Statement St ; 
    //public ResultSet rs; 

    public List<String> getDescriptionOfInterets(Integer idUser) { 
     String res=" "; 
     //these three: Connection, Statement, ResultSet MUST ALWAYS be local 
     //variables of a method, NEVER assigned to class field AND NEVER to 
     //static variables to avoid synchronization problems in a 
     //multithreaded environment such as a web application 
     Connection con = null; 
     PreparedStatement ps = null; 
     ResultSet rs = null; 
     List<String> descriptionList = new ArrayList<String>(); 
     try{ 
      //this is one way to open a database connection 
      //you should change it to a database connection pool 
      //which is a different topic you should search about 
      //since you didn't say which database you're working with 
      //I'll assume it's mysql 
      Class.forName("com.mysql.jdbc.Driver"); 
      //similar with the database credentials 
      String user = "root"; 
      String password = "root"; 
      con = DriverManager.getConnection(
       "jdbc:mysql://localhost:3306/dbname", user, password); 
      /* 
      ResultSet result = St.executeQuery("SELECT description FROM interets, avoir, consomateur WHERE avoir.id_interet=interets.id_interets AND avoir.id_user=consomateur.code_bar AND consomateur.code_bar="+IdUser+""); 

      ResultSetMetaData resultMeta = (ResultSetMetaData) result.getMetaData(); 
      */ 
      ps = con.prepareStatement("SELECT description " 
       + "FROM interets, avoir, consomateur " 
       + "WHERE avoir.id = interets.id AND " 
       + "avoir.id?user = consomateur.code_bar AND " 
       + "contomateur.code_bar = ?"); 
      ps.setInt(1, idUser); 

      rs = ps.executeQuery(); 
      //while(result.next()){ 
      while (rs.next) { 
       //String Newligne=System.getProperty("line.separator"); 
       //for(int i = 1; i <= resultMeta.getColumnCount(); i++) 
       //res=res+Newligne+result.getObject(i).toString(); 
       //System.out.println(res); 
       descriptionList.add(rs.getString(1)); 
      } 
     } catch (Exception e) { 
      //Handle your error! This is very important to check the causes of the problem 
      //you should at least print the stacktrace 
      //printing a single line saying "oh no! I got an error" doesn't help 
      System.out.println("Erreur dans la requete d'affichage."); 
      e.printStacktrace(); 
     } finally { 
      //remember to ALWAYS close the opened resources 
      close(rs); 
      close(ps); 
      close(con); 
     } 
    } 

    public void close(ResultSet rs) { 
     if (rs != null) { 
      try { 
       rs.close(); 
      } catch (SQLException e) { 
       //silent... 
      } 
     } 
    } 

    public void close(Statement st) { 
     if (st != null) { 
      try { 
       st.close(); 
      } catch (SQLException e) { 
       //silent... 
      } 
     } 
    } 

    public void close(Connection con) { 
     if (con != null) { 
      try { 
       con.close(); 
      } catch (SQLException e) { 
       //silent... 
      } 
     } 
    } 
} 

然後在你的JSP:

Description of Interets 
<table> 
<% 
    Integer idUser = (Integer)request.getAttribute("IdUser"); 
    //if the attribute is stored in HttpSession (probably) 
    //or ServletContext (oddly), use the right method to get it 
    Interets interets = new Interets(); 
    List<String> descriptionList = interets.getDescriptionOfInterets(idUser); 
    for (String description : descriptionList) { 
    %> 
     <tr> 
      <td><%= description %></td> 
     </tr> 
    <% 
    } 
%> 
</table> 

甚至更​​好,使用ServletExpression LanguageJSTL(假設你的JSP被命名爲 「interets.jsp」,並在根包):

@WebServlet("/interets") 
public class InteretsServlet extends HttpServlet { 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
     HttpSession session = request.getSession(); 
     Integer idUser = (Integer)session.getAttribute("IdUser"); 
     Interets interets = new Interets(); 
     List<String> descriptionList = interets.getDescriptionOfInterets(idUser); 
     request.setAttribute("descriptionList", descriptionList); 
     request.getRequestDispatcher("/interets.jsp").forward(request, response); 
    } 
} 

而且JSP可以像

Description of Interets 
<table> 
    <c:forEach items="${descriptionList}" value="description"> 
     <tr> 
      <td>${description}</td> 
     </tr> 
    </c:forEach> 
</table> 
+0

我試過你的解決方案luigi,錯誤消失了,謝謝你,我corretec thie聲明聲明是這樣的:static Statement St = null;這是你的意思(我很抱歉,我是新來的Java)。但是現在結果沒有顯示在我的頁面中,而是顯示Exception中的消息。 – user3365621

+0

@ user3365621正如我在答案的底部所指出的那樣,您目前的設計存在幾個問題。首先查看Java基礎知識和JDBC基礎知識會更好。我會嘗試添加更多內容到這個答案,以嘗試保存您當前的代碼... –

+0

@ user3365621答案已更新。 –

相關問題