2012-02-28 86 views
0

我想在一個sql語句中使用我在JSP中創建的會話。但有困難。使用JSP會話的SQL語句

我已經捕獲它,這是我所做的SQL語句,但不起作用。

String mysql="select * from table1 where cottCode="<%= session.getAttribute("theName") %>""; 

任何幫助表示感謝,謝謝。

回答

0

我想在這裏你正在嘗試做這樣的事情:

<% 
    String name = session.getAttribute("theName"); 
    String mysql = "select * from table1 where cottCode=" + name; 
    // then connecting MySql server using driver in JSP to execute the SQL query 
%> 

這種代碼稱爲JSP腳本。

然而,像表達式:

<%= session.getAttribute("theName") %> 

稱爲JSP Expressions.They用於「插入腳本語言表達式的值,轉換成字符串,進料流返回到客戶端的數據。」像這樣:

<html> 
    <body> 
     <p>hi, my name is <%= session.getAttribute("theName") %> 
     </p> 
    </body> 
</html> 

所以我不認爲你可以在scriptlet中使用表達式。這裏是關於相關主題的教程,希望它會有所幫助: The Java EE 5 Tutorial

+0

感謝您的回覆,但是我找到了另一種解決方案。 我用它把它放到一個變量中 \t String pbutton = request.getParameter(「T2」); 然後對於聲明.... String mysql =「select * from table1 where cottCode ='」+ pbutton +「'」; 完成。感謝壽。 – Merked 2012-02-28 22:01:39

+0

@Merked您應該真正使用Prepared Statements來避免SQL注入,尤其是當您使用請求參數(P2)時 - http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement html的 – reevesy 2012-02-28 22:26:46