2013-04-24 14 views
0

如何從SQL數據庫(特別是從列)中檢索數據以便在我的JSP文件中下拉列表框?但我想從一個.java文件中的函數和JSP列表框中檢索這些數據,所以順序是:如何從函數中檢索數據到我的jsp文件中的下拉列表框中

我的java文件中的一個函數,用於從sql DB檢索數據(特別是從列)並在我的jps列表框中顯示它?

這將在Java文件

這裏的功能是我的代碼:

public String getc_Condicion(){ 
    String query = ""; 

    String c_condicion = new String(); 
     query = "select C_Condicion_Beca from Bca_Condicion_Beca"; 
     try{ 
      st = con.createStatement(); 
      rs = st.executeQuery(query); 

      while (rs.next()){ 
       c_condicion = rs.getString("C_Condicion_Beca"); 
       } 
     } 
     catch(SQLException e){ 
     System.out.println("Error al obtener los datos: "+e.getMessage()); 

     } 

    return c_condicion; 
} 

回答

0

你在找這個?儘管如此,這可能不是最好的設計。

public String[] getcDataCondicion(){ 
    String query = ""; 

    String[] cDataCondicion= new String[100]; 
     query = "select C_Condicion_Beca from Bca_Condicion_Beca"; 
     try{ 
      st = con.createStatement(); 
      rs = st.executeQuery(query); 
      int i=0; 
      while (rs.next()){ 
       cDataCondicion[i] = rs.getString("C_Condicion_Beca"); 
       i++; 
       } 
     } 
     catch(SQLException e){ 
     System.out.println("Error al obtener los datos: "+e.getMessage()); 

     } 

    return cDataCondicion; 
} 

<% 

    YourClass c= new YourClass(); 
String[] result=c.getcDataCondicion(); 
%> 

<Select> 
<% 
    for(int i=0;i<result.length;i++){ 
%> 
<Option>result[i]</ Option> 
<% } %> 
</select> 
相關問題