Ola Pessoal, Não estou conseguindo fazer uma consulta, alguem consegue me ajudar por favor,
desde ja agradeço,
ate mais,
package conexao;
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;
public Conexao() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
con =
DriverManager.getConnection(
"jdbc:mysql://localhost:3306/teste",
"root",
"ivan");
stm = con.createStatement();
} 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 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;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Consulta.jsp
<%@ page contentType="text/html" language="java" import="java.sql.*"%>
<jsp:useBean id="con" class="conexao.Conexao"/>
<html>
<title>Consulta</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form method="get">
<%
con.setNome(request.getParameter("nomeField"));
boolean testa = con.consultarDados();
if (testa)
{
ResultSet temp = con.getResultado();
response.sendRedirect("http://localhost:8080/teste/pessoa.jsp?status=Consulta efetuada com sucesso&nome="+
temp.getString("nome"));
//Não gostaria de fazer a pesquisa usando todos estes parametros - +"&idade="+temp.getString("idade")+"&sexo="+temp.getString("sexo")
}else{
response.sendRedirect("http://localhost:8080/teste/pessoa.jsp?status=Registro não encontrado");
}
%>
</form>
</body>
</html>