Tire o ponto e virgula do final e coloque '
 
"SELECT Nome_cliente FROM Cliente WHERE Nome_cliente='"+aux+"'");
        
----- Original Message -----
Sent: Saturday, August 26, 2000 8:07 PM
Subject: [java-list] Duvidas com servlet

Saudações Javanesas!!
 
 
Vê se vocês podem me ajudar:
estou tentando desenvolver um servlet que pega um nome (fulano) em um textfield de uma página html e faz uma consulta em um banco de dados access para ver se existe, se sim ele diz "olá fulano", se não ele diz "voce deve se cadastrar primeiro fulano".  Estou mandan abaixo o código do servlet.  Ele compila sem problemas, não gera erro de SQL mas,eu não sei como pegar o resultado desta consulta para saber se é true ou false (se o fulano existe no BD ou não).  
Qualquer dica é bem vinda.  segue o fonte:
 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
 
public class SelectFromServlet extends HttpServlet {
   private Statement statement = null;
   private Connection connection = null;
   private String URL = "jdbc:odbc:meubd";
 
   public void init( ServletConfig config )
      throws ServletException
   {
      super.init( config );
  
      try {
         Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
         connection =
            DriverManager.getConnection( URL, "", "" );
      }
      catch ( Exception e ) {
         e.printStackTrace();
         connection = null;
      }
   }
 
   public void doPost( HttpServletRequest req,
                       HttpServletResponse res )
      throws ServletException, IOException
   {
      String anome;
  
     
      anome = req.getParameter( "NOME" );
     
     
      PrintWriter output = res.getWriter();
      res.setContentType( "text/html" );
     
     
     boolean success = selectFromDB(anome);
 
         if (success)
              output.println("Hello "+anome);
         else
              output.println("Cadastre-se Primeiro");
     
     
   
   
           
     output.close();
 

   }

    private boolean selectFromDB( String aux )
   {
   PrintWriter out;
      try {
        
         statement = connection.createStatement();
         statement.execute("SELECT Nome_cliente FROM Cliente WHERE Nome_cliente='"+aux+"';");
         statement.close();          
                            
      }
      catch ( Exception e ) {
         System.err.println(
            "ERROR: Problems with adding new entry" );
         e.printStackTrace();
         return false;
      }
  return true;   
       
   }  
 

   public void destroy()
   {
      try {
         connection.close();
      }
      catch( Exception e ) {
         System.err.println( "Problem closing the database" );
      }
   }
}
 
---------------------------------- 
[]'s
Prudente Aguiar.

Reply via email to