[color=red][/color][size=18][/size]
Olá pessoal, espero que tenha alguém que me ajude pois não conseguir resolver o problema pelo qual estou passando.
estou Desenvolvendo um projeto basico q envolve JSP e um Banco de Dados Oracle.
Neste projeto tem uma pagina JSP de busca aluno por RA, nela digita-se o RA e o objetivo seria de em uma pagina JSP de resultado apresentar
o RA digitado e o nome do aluno referente a este RA que foi buscado no banco de dados. minha conexão funciona perfeitamente creio eu, mas não conseguir fazer
com que apresenta-se o resultado da busca. Não sei se é meu dao e a pagina JSP de resultado que está errado ou a logica inteira. segue meus codigos, espero que tenha alguem que me ajude pois ja procurei em varios foruns e não obtive resposta.
JSP DE BUSCA
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Buscar Aluno</title>
</head>
<body>
<form action="BuscaRA" method="post">
<h1> Digite O RA para pesquisa</h1>
<center>RA:<input type="text" name="alucod"/><br/>
<input type="submit" value ="Buscar"/>
</center>
</form>
</body>
</html>
JSP DE RESULTADO DA BUSCA ( MAS CONCERTEZA ESTÁ ERRADA POIS SÓ APRESENTA O RA NÃO TRAS O NOME PESQUISADO NO BANCO)
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Aluno</title>
</head>
<body>
RA: ${aluno.alucod}<br/>
Nome:${aluno.alunom}<br/>
</body>
</html>
CLASSE DE CONEXÃO COM O BANCO DE DADOS
package br.com.italo.processo.conectionfactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public Connection getConnection() throws SQLException {
System.out.println("Conectando ao banco");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e){
throw new SQLException(e);
}
return DriverManager.getConnection("jdbc:oracle:thin:######:DBWAE","NOME DO BANCO","SENHA");
}
}
–DAO QUE POSSUE A LÓGICA DE BUSCA
package br.com.italo.processo.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.italo.processo.conectionfactory.ConnectionFactory;
import br.com.italo.processo.modelo.Aluno;
public class AlunoDAO {
private Connection connection;
public AlunoDAO() {
try{
this.connection = new ConnectionFactory().getConnection();
} catch(SQLException e){
throw new RuntimeException(e);
}
}
public void adiciona(Aluno aluno){
String sql = "INSERT INTO ACALU ( ALUCOD, ALUNOM) VALUES (?,?)";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setLong(1, aluno.getAlucod());
stmt.setString(2, aluno.getAlunom());
stmt.execute();
stmt.close();
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Aluno> getLista(){
try {
List<Aluno> alunos = new ArrayList<Aluno>();
PreparedStatement stmt = this.connection.prepareStatement("SELECT FROM ACALU");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Aluno aluno = new Aluno();
aluno.setAlucod(rs.getLong("alucod"));
aluno.setAlunom(rs.getString("alunom"));
alunos.add(aluno);
}
rs.close();
stmt.close();
return alunos;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void busca(Aluno aluno) {
String sql = "SELECT * FROM ACALU WHERE ALUCOD = ?";
try {
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setLong(1, aluno.getAlucod());
//stmt.setString(2, aluno.getAlunom());
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
package br.com.italo.processo.modelo;
public class Aluno {
private Long alucod;
private String alunom;
public Long getAlucod() {
return alucod;
}
public void setAlucod(Long alucod) {
this.alucod = alucod;
}
public String getAlunom() {
return alunom;
}
public void setAlunom(String alunom) {
this.alunom = alunom;
}
}
SERVLET DE BUSCA
package br.com.italo.processo.servlet;
import java.io.IOException;
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 br.com.italo.processo.dao.AlunoDAO;
import br.com.italo.processo.modelo.Aluno;
/**
* Servlet implementation class BuscaRA
*/
@WebServlet("/BuscaRA")
public class BuscaRA extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
//buscando os paramentros do request
Long alucod = Long.parseLong(request.getParameter("alucod"));
String alunom = request.getParameter("alunom");
Aluno aluno = new Aluno();
aluno.setAlucod(alucod);
aluno.setAlunom(alunom);
AlunoDAO dao = new AlunoDAO();
dao.busca(aluno);
HttpSession session = request.getSession();
session.setAttribute("aluno", aluno);
RequestDispatcher rd = request.getRequestDispatcher("/resultado.jsp");
rd.forward(request, response);
}
/**
* @see HttpServlet#HttpServlet()
*/
public BuscaRA() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}