Acesso ao Banco

1 resposta
vjfenix

Estou fazendo um consulta no Banco com JSP porém está me dando o erro… alguém pode ajudar??

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 8 in the jsp file: /consultar.jsp

con cannot be resolved

5: 

6: 

7: <%

8:       con.setNome(request.getParameter(nomeField));

9:       boolean testa = con.consultarDados();

10:

11:       if (testa)
An error occurred at line: 9 in the jsp file: /consultar.jsp

con cannot be resolved

6: 

7: <%

8:       con.setNome(request.getParameter(nomeField));

9:       boolean testa = con.consultarDados();

10:

11:       if (testa)

12:       {
An error occurred at line: 13 in the jsp file: /consultar.jsp

ResultSet cannot be resolved to a type

10:

11:       if (testa)

12:       {

13:          ResultSet temp = con.getResultado();

14:          response.sendRedirect(“<a href="http://localhost:8080/test/pessoa.jsp?status=Consulta">http://localhost:8080/test/pessoa.jsp?status=Consulta</a> efetuada com sucesso&nome=”+

15:             temp.getString(“nome”)+"&idade="+temp.getString(“idade”)+"&sexo="+temp.getString(“sexo”));

16:       }else{
An error occurred at line: 13 in the jsp file: /consultar.jsp

con cannot be resolved

10:

11:       if (testa)

12:       {

13:          ResultSet temp = con.getResultado();

14:          response.sendRedirect(“<a href="http://localhost:8080/test/pessoa.jsp?status=Consulta">http://localhost:8080/test/pessoa.jsp?status=Consulta</a> efetuada com sucesso&nome=”+

15:             temp.getString(“nome”)+"&idade="+temp.getString(“idade”)+"&sexo="+temp.getString(“sexo”));

16:       }else{
Stacktrace:

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)

org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)

org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)

javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.
Apache Tomcat/5.5.23


Consulta <% con.setNome(request.getParameter("nomeField")); boolean testa = con.consultarDados();
if (testa)   
  {            
     ResultSet temp = con.getResultado();   
     response.sendRedirect("http://localhost:8080/test/pessoa.jsp?status=Consulta efetuada com sucesso&nome="+   
        temp.getString("nome")+"&idade="+temp.getString("idade")+"&sexo="+temp.getString("sexo"));   
  }else{   
     response.sendRedirect("http://localhost:8080/test/pessoa.jsp?status=Registro não encontrado");   
  }

%>

**********************************************************************************

package br.com.uniban.bean;

import java.sql.*;

public class Conexao {

public Connection con;

public Statement stm;

public ResultSet res = null;

private String nome = null;

private int idade = 0;

private String sexo = null;

private String sit = “”;

public Conexao() {

try {

Class.forName(org.gjt.mm.mysql.Driver);

con = DriverManager.getConnection(

jdbc:mysql://localhost:3306/test”, “root”, “”);

stm = con.createStatement();

sit = Passou ;

} catch (Exception e) {

System.out.println(não foi possível conectar ao banco

+ e.getMessage());

}

}
public void setNome(String nome) {
	this.nome = nome;
}

public void setIdade(int idade) {
	this.idade = idade;
}

public void setSexo(String sexo) {
	this.sexo = sexo;
}

public String getNome() {
	return nome;
}

public int getIdade() {
	return idade;
}

public String getSexo() {
	return sexo;
}

public String getSit() {
	return sit;
}

public void setSit(String sit) {
	this.sit = sit;
}

public void inserirDados() {

	try {
		String query = "insert into pessoa(nome,idade,sexo) values(\""
				+ nome + "\"," + idade + ",\"" + sexo + "\")";
		stm.executeUpdate(query);

	} catch (SQLException e) {
		System.out.println("Erro na inserção:" + e.getMessage());
	}

}

public boolean alterarDados() {

	boolean testa = false;

	try {
		String query = "update pessoa " + "set idade = " + idade + ", "
				+ "sexo = \"" + sexo + "\" " + "where nome = \"" + nome
				+ "\"";

		int linhas = stm.executeUpdate(query);

		if (linhas > 0)
			testa = true;
		else
			testa = false;

	} catch (SQLException e) {
		System.out.println("Erro na inserção:" + e.getMessage());
	}

	return testa;
}

public boolean excluirDados() {

	boolean testa = false;

	try {
		String query = "delete from pessoa where nome='" + nome + "'";
		int linhas = stm.executeUpdate(query);

		if (linhas > 0)
			testa = true;
		else
			testa = false;

	} catch (SQLException e) {
		System.out.println("Erro na exclusão:" + e.getMessage());
	}

	return testa;
}

public boolean consultarDados() {

	boolean testa = false;

	try {
		String query = "select * from pessoa where nome='" + nome + "'";
		res = stm.executeQuery(query);

		if (res.next()) {
			testa = true;
		} else {
			testa = false;
		}

	} catch (SQLException e) {
		System.out.println("Erro na inserção:" + e.getMessage());
	}

	return testa;
}

public void setConsulta() {

	try {
		res = stm.executeQuery("select * from pessoa");
	} catch (SQLException e) {
		e.printStackTrace();
	}

}

public ResultSet getResultado() {
	return res;
}

}

1 Resposta

lelodois

Falta vc importar

ex

<%@ page contentType=“text/html; charset=UTF-8” language=“java” import=“java.sql.,beans.com.opm3.,java.util.*” errorPage=“erroAplicacao.jsp”%>

fmz

Criado 30 de outubro de 2007
Ultima resposta 30 de out. de 2007
Respostas 1
Participantes 2