Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [Java]java.sql.SQLSyntaxErrorException con query select

    Ciao a tutti.
    Sto creando una java web application per la gestione delle fatture.
    L'applicazione prevede, oltre all'inserimento, anche la visualizzazione della lista delle fatture inserite da un determinato utente.
    Nel richiedere la lista delle fatture ottengo il seguente messaggio di errore:
    "java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.fattura where fkutente = 2' at line 1"

    e la lista che voglio visualizzare in una pagina jsp è ovviamente vuota.
    Di seguito il codice:

    Servlet


    codice:
       import java.io.IOException;import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import it.techwin.bean.Fattura;
    import it.techwin.dao.ManipolazioneDao;
    
    /**
     * Servlet implementation class ListaFatture
     */
    @WebServlet("/ListaFattureCb")
    public class ListaFattureCb extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public ListaFattureCb() {
            super();
    
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    		HttpSession session = request.getSession();
    		
    		String username = (String)session.getAttribute("username");
    		String password = (String)session.getAttribute("password");
    		Integer fkUtente = (Integer) session.getAttribute("idUtente");
    		System.out.println(fkUtente);
    		
    		List<Fattura> fatture = new ArrayList<Fattura>();
    		
    		if(username == null || password == null){
    			
    			RequestDispatcher rd = request.getRequestDispatcher("/jsp/errorcb.jsp");
    			rd.forward(request, response);
    			
    		}else{
    			
    			try {
    				fatture = ManipolazioneDao.listaFatture(fkUtente);
    			} catch (SQLException e) {
    
    				e.printStackTrace();
    			}
    			request.setAttribute("fatture", fatture);
    			RequestDispatcher rd = request.getRequestDispatcher("/jsp/listafatturecb.jsp");
    			rd.forward(request, response);
    		}
    	}
    
    }

    DAO
    codice:
       public static List<Fattura> listaFatture(int fkUtente) throws SQLException{		
    		Connection conn = ConnessioneDb.getConnection();
    		PreparedStatement ps = null;
    		ResultSet rs = null;
    		
    		List<Fattura> fatture = new ArrayList<Fattura>();
    		
    		String queryList = "select idfattura, dataemissione, numeroemissione, nomeditta, nomecliente"
    						+ "from fattura_tw.fattura where fkutente = ?";
    		
    		try{
    			
    			ps = conn.prepareStatement(queryList);
    			ps.setInt(1, fkUtente);
    			System.out.println(fkUtente);
    			
    			rs = ps.executeQuery();
    			
    			while(rs.next()){
    				
    				Fattura fattura = new Fattura();
    				fattura.setIdFattura(rs.getInt(1));
    				fattura.setDataEmissione(rs.getDate(2));
    				fattura.setNumeroEmissione(rs.getInt(3));
    				fattura.setNomeDitta(rs.getString(4));
    				fattura.setNomeCliente(rs.getString(5));
    				
    				fatture.add(fattura);
    			}
    			
    		}catch(SQLException e){
    			
    			e.printStackTrace();
    			
    		}finally{
    			
    			if(ps != null){
    				
    				ps.close();
    			}
    			
    			if(conn != null){
    				
    				conn.close();
    			}
    			
    			
    		}
    		
    		return fatture;
    		
    	}
    ​Cosa c'è di sbagliato nel mio codice?

  2. #2
    Ho risolto! Era solo una questione di spazi nella stringa della query.
    Scusate il disturbo.

Tag per questa discussione

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 © 2024 vBulletin Solutions, Inc. All rights reserved.