2012-12-23 55 views
-4

我想使用JSP從Oracle獲取數據。應該從表單文本框傳入多個參數。使用JSP從Oracle獲取數據

<form method="post" action="num_post.jsp"> 
Enter Number: <input name="num" type="text" id="num" /> 

<input type="submit" name="Submit" value="Submit" /> 
</form> 

這裏在文本字段中,我想傳遞多個參數,例如, 123,456,789,896等

現在在num_post.jsp中,我有這段代碼來請求JSP中傳遞的參數。

<% 
String[] num=request.getParameterValues("num"); 

int i=0; 

for(i=0;i<num.length;i++) 
{ 
     out.println("number Elements  :"+num[i]+"<br/>"); 
} 
%> 

現在我想使用陣列參數從Oracle獲取數據,例如:num[i]

<%@page import="java.sql.*"%> 
<%@ page import = "java.io.*"%> 
<% 
Class.forName("oracle.jdbc.OracleDriver"); 
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); 
Statement st=con.createStatement(); 
String sql = "SELECT * from jha where num IN '"+num[i]+"'" ; 
ResultSet rs = st.executeQuery(sql); 
%> 

這引發ArrayOutOfBound異常。

+0

嘗試我的解決方案,讓知道。如果你有解決方案,那麼接受它,否則詳細說明你的問題。 – Ravi

+0

你在哪裏?至少讓我知道,無論你的決心與否? – Ravi

回答

0

嘗試,而不是這個

String[] num=(request.getParameter("num").toString()).split(",");

String[] num=request.getParameterValues("num");

===== UPDATE =====

如您指定,ArrayIndexOutOfBound , 意思是, 你正試圖訪問你的數組中的索引。而且,很明顯現在

String[] num=(request.getParameter("num").toString()).split(","); // let say array size would be 4 
int i=0; // i initialized by 0 

for(i=0;i<num.length;i++) 
{ 
     out.println("number Elements  :"+num[i]+"<br/>"); 
} 
    // when control reached here, the value of i will be 4 (which is out of index) 

,在您的查詢您試圖訪問4指標元素,這是不存在的。

"SELECT * from jha where num IN '"+num[i]+"'" // here value of i is 4 

這就是爲什麼你得到ArrayIndexOutOfBound。得到它了 ???

還有一件事,您在那裏寫的查詢適用於只檢查數組中的一個值。所以,你需要改變你的查詢。它不會顯示你,你的預期輸出。

+0

感謝蒂姆梅多拉爲迴應,但沒有工作.. –

+0

我不是蒂姆梅多拉,也請詳細說明什麼是行不通的。 – Ravi

+0

correction var______ sql查詢不接受參數//我能夠從文本框中檢索參數,但是sql不會傳遞該參數以進一步從數據庫中獲取詳細信息。 String sql =「SELECT * from jha where num IN'」+ num [i] +「'」; –