Visualizzazione dei risultati da 1 a 3 su 3

Discussione: struts iterate su db

  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213

    struts iterate su db

    ciao a tutti!
    ho questa classe con query:
    codice:
    public ActionForward lista(ActionMapping mapping, ActionForm form,
    		HttpServletRequest request, HttpServletResponse response)
    		throws Exception
    {
    	DataSource dataSource;
    	Connection myConnection = null;
    	try
    	{
    		dataSource = getDataSource(request,"STRUTS_DB1");
    		myConnection = dataSource.getConnection();
    	
    		String select = "select Nome, Cognome from utente";
    		PreparedStatement ps = myConnection.prepareStatement(select);			
    		ResultSet rst = ps.executeQuery();
    		rst.close();
    		ps.close();
    	}
    e nel struts-config.xml:
    codice:
    	<action
    		path="/utenti"
    		name="buttonform"
    		scope="request"
    		type="struts.actions.Loginaction"
    		input="/success.jsp"
    	>
    		<forward name="utenti" path="/utenti.jsp"/>
    	</action>
    ora nella pagina utenti.jsp mi dovrei far stampare la select in una tabella utilizzando iterate, come posso fare?

  2. #2
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213
    ora ho modificato la classe in questo modo,anche se devo aggiungere un oggetto utente e nn solo il nome:
    codice:
    Vector utenti = new Vector();
    	DataSource dataSource;
    	Connection myConnection = null;
    	try
    	{
    		dataSource = getDataSource(request,"STRUTS_DB1");
    		myConnection = dataSource.getConnection();
    			
    		String select = "select Nome, Cognome from utente";
    		PreparedStatement ps = myConnection.prepareStatement(select);		
    		ResultSet rst = ps.executeQuery();
    			
    		LoginForm datiUt=new LoginForm();
    				
    		while(rst.next()){
    					
    		String nome = rst.getString(1);
    		System.out.println(nome);
    									
    		datiUt.setNome(nome);
    		String Nome = (String)datiUt.getNome();
    		utenti.add(Nome);
    		}

  3. #3
    Utente di HTML.it
    Registrato dal
    Oct 2006
    Messaggi
    213
    ho risolto, grazie cmq a tutti, se a qualcuno può interessare posto il codice, questa è la classe:
    codice:
    public ActionForward lista(ActionMapping mapping, ActionForm form,
    		HttpServletRequest request, HttpServletResponse response)
    		throws Exception
    {
    	System.out.print("ciao");
    	Vector utenti = new Vector();
    	DataSource dataSource;
    	Connection myConnection = null;
    	try
    	{
    		dataSource = getDataSource(request,"STRUTS_DB1");
    		myConnection = dataSource.getConnection();
    		String select = "select Nome, Cognome from utente";
    		PreparedStatement ps = myConnection.prepareStatement(select);			ResultSet rst = ps.executeQuery();
    					
    		while(rst.next()){
    			LoginForm datiUt=new LoginForm();
    			String nome = rst.getString(1);
    			System.out.println(nome);
    			String cognome = rst.getString(2);
    			System.out.println(cognome);
    			datiUt.setNome(nome);
    			datiUt.setCognome(cognome);
    			utenti.add(datiUt);
    			request.setAttribute("lista", utenti);
    		}
    		rst.close();
    		ps.close();
    	}
    	catch(Exception sqle)
    	{
    		sqle.printStackTrace();
    	}
    	finally
    	{			
    		try
    		{
    			myConnection.close();
    		}
    		catch(Exception sqlex)
    		{
    			sqlex.printStackTrace();
    		}
    	}	
    	return mapping.findForward("utenti");			
    }
    nel .xml va questo:
    codice:
    	<action
    		path="/utenti"
    		parameter="list"
    		type="struts.actions.LoginAction"
    	>
    		<forward name="utenti" path="/utenti.jsp"/>
    	</action>
    la pagina utenti.jsp:
    codice:
    	<logic:iterate id="utenti" name="lista">
    	<tr>
    		<td>
    			<bean:write name="utenti" property="nome" />
    		</td>
    		<td>
    			<bean:write name="utenti" property="cognome" />
    		</td>
    	</tr>
    	</logic:iterate>
    spero possa servire....

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.