Projeto com JSP, Calculo em classe, JDBC, MySql e Bootstrap

Projeto em JSP de Classe Aluno com cálculo de média e situação Orientado a Objeto, usando o banco MySql e Bootstrap na parte web.

As libs usadas no do Projeto foram:

jstl.jar
mysql-connector-java-5.1.23-bin.jar
servlet3-api.jar
standard.jar

Classe Abstrata Pessoa:

package entity;

public abstract class Pessoa {

	private String nome;
	private String email;
	private String sexo;

	public Pessoa() {
		// TODO Auto-generated constructor stub
	}

	public Pessoa(String nome, String email, String sexo) {
		this.nome = nome;
		this.email = email;
		this.sexo = sexo;
	}

	@Override
	public String toString() {
		return nome + "," + email + "," + sexo ;
	}

	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getSexo() {
		return sexo;
	}
	public void setSexo(String sexo) {
		this.sexo = sexo;
	}
}    

Classe Aluno:

package entity;

public class Aluno extends Pessoa {

	private Integer codigo;
	private String disciplina;
	private Double nota1;
	private Double nota2;

	private String situacao="";
	private double  media=0.;

	public Aluno() {
		// TODO Auto-generated constructor stub
	}

	public Aluno(Integer codigo, String disciplina, Double nota1, Double nota2) {
		this.codigo = codigo;
		this.disciplina = disciplina;
		this.nota1 = nota1;
		this.nota2 = nota2;
	}

	
	public Aluno(Integer codigo, String nome, String email, String sexo, String disciplina, 
			Double nota1, Double nota2) {
		super(nome, email, sexo);
		this.codigo = codigo;
		this.disciplina = disciplina;
		this.nota1 = nota1;
		this.nota2 = nota2;
	}

	@Override
	public String toString() {
		return "Aluno [codigo=" + codigo + ", disciplina=" + disciplina + ", nota1=" + nota1 + ", 
                nota2=" + nota2 + ", situacao=" + situacao + ", media=" + media + "]";
	}

	public Integer getCodigo() {
		return codigo;
	}
	public void setCodigo(Integer codigo) {
		this.codigo = codigo;
	}
	public String getDisciplina() {
		return disciplina;
	}
	public void setDisciplina(String disciplina) {
		this.disciplina = disciplina;
	}
	public Double getNota1() {
		return nota1;
	}
	public void setNota1(Double nota1) {
		this.nota1 = nota1;
	}
	public Double getNota2() {
		return nota2;
	}
	public void setNota2(Double nota2) {
		this.nota2 = nota2;
	}
	public String getSituacao() {
		return situacao;
	}
	public void setSituacao(String situacao) {
		this.situacao = situacao;
	}
	public Double getMedia() {
		return media;
	}
	public void setMedia(Double media) {
		this.media = media;
	}
	
	public static void main(String[] args) {
		Aluno a = new Aluno(10, "lulu", "lulu@gmail.com", "f", "java", 8., 9.);
		System.out.println(a);
	}
}    

Script:

create database aula04;
use aula04;

create table aluno(codigo int primary key,
					nome varchar(35),
					email varchar(50)unique,
					sexo enum('m','f') not null,
					disciplina varchar(35),
					nota1 double,
					nota2 double
);

insert into aluno values(1, 'lulu', 'lulu@gmail.com', 'f', 'webdesign', 6., 5.);
insert into aluno values(2, 'belem', 'belem@gmail.com', 'm', 'java', 10., 9.9);
insert into aluno values(3, 'carol', 'carol@gmail.com', 'f', 'java', 3., 6.);

select nome, (nota1+nota2)/2 as media, if((nota1+nota2)/2 >=7, 'Aprovado', 'Reprovado') 
    as situacao from aluno;
    

Resultado no banco...

DAO:

package persistence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Dao {
	
	Connection con;
	PreparedStatement stmt;
	ResultSet rs;
	
	public void open()throws Exception{
		Class.forName("com.mysql.jdbc.Driver");
		con = DriverManager.getConnection("jdbc:mysql://localhost:3306/aula04", "root", "coti");
	}

	public void close()throws Exception{
		con.close();
	}
}

Classe AlunoDao:

package persistence;

import java.util.ArrayList;
import java.util.List;
import entity.Aluno;

public class AlunoDao extends Dao{
	
	public void create(Aluno a)throws Exception{
		open();
			stmt = con.prepareStatement("insert into aluno values(?,?,?,?,?,?,?)");
				stmt.setInt(1, a.getCodigo());
				stmt.setString(2, a.getNome());
				stmt.setString(3, a.getEmail());
				stmt.setString(4, a.getSexo());
				stmt.setString(5, a.getDisciplina());
				stmt.setDouble(6, a.getNota1());
				stmt.setDouble(7, a.getNota2());
			stmt.execute();
			stmt.close();
		close();
	}
	
	
	public List findAll()throws Exception{
		open();
		Listlista = new ArrayList();
			stmt = con.prepareStatement("select * from aluno");				
				rs = stmt.executeQuery();
				while(rs.next()){
					Aluno a = new Aluno();
					a.setCodigo(rs.getInt(1));
					a.setNome(rs.getString(2));
					a.setEmail(rs.getString(3));
					a.setSexo(rs.getString(4));
					a.setDisciplina(rs.getString(5));
					a.setNota1(rs.getDouble(6));
					a.setNota2(rs.getDouble(7));
					lista.add(a);
				}
		close();
		return lista;
	}
}    

Classe CalculoAluno:

package control;

import entity.Aluno;

public class CalculoAluno {
	
	public static void calcularMediaAluno(Aluno a){
		a.setMedia((a.getNota1() + a.getNota2())/2);
	}
	
	public static void gerarSituacaoAluno(Aluno a){
		a.setSituacao((a.getMedia()>=7)?"Aprovado":"Reprovado");
	}
	
	public static void main(String[] args) {
		Aluno a = new Aluno(1, "lulu", "lulu@gmail.com", "f", "java", 7., 9.);
		CalculoAluno.calcularMediaAluno(a);
		CalculoAluno.gerarSituacaoAluno(a);
		System.out.println("Media: " + a);
	}
}    

Servlet:

package control;

import java.io.IOException;
import java.util.List;
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 entity.Aluno;
import persistence.AlunoDao;

@WebServlet("/Controle")
public class Controle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Controle() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cmd = request.getParameter("cmd");
		if(cmd.equalsIgnoreCase("gravar")){
			gravar(request, response);	
		}else if(cmd.equalsIgnoreCase("consultar")){
			consultar(request, response);
		}
	}

	protected void gravar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Aluno a = new Aluno();
		response.setContentType("text/html");
		try {
			a.setCodigo(new Integer(request.getParameter("codigo")));
			a.setNome(request.getParameter("nome"));
			a.setEmail(request.getParameter("email"));
			a.setSexo(request.getParameter("sexo"));
			a.setDisciplina(request.getParameter("disciplina"));
			a.setNota1(new Double(request.getParameter("nota1")));
			a.setNota2(new Double(request.getParameter("nota2")));
			CalculoAluno.calcularMediaAluno(a);
			CalculoAluno.gerarSituacaoAluno(a);
			//response.getWriter().print("Nome: " + a.getNome() + " Media: " + a.getMedia());
		  new AlunoDao().create(a); //grava relacionado ...
		  	request.setAttribute("msg","Dados Gravados");
		  	request.getRequestDispatcher("index.jsp").forward(request, response);
		} catch (Exception ex) {
			ex.printStackTrace();
			//response.getWriter().print("Erro: " + ex.getMessage());
			request.setAttribute("msg","Error:" + ex.getMessage());
			request.getRequestDispatcher("index.jsp").forward(request, response); 
		}
	}
	
	protected void consultar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Listlista= new AlunoDao().findAll();
			request.setAttribute("msg", lista);
			request.getRequestDispatcher("consulta.jsp").forward(request, response);
		} catch (Exception ex) {
			request.setAttribute("msg","Error:" + ex.getMessage());
			request.getRequestDispatcher("consulta.jsp").forward(request, response);
		}
	}
}    

ManagerBean:

package manager;

import java.util.ArrayList;
import java.util.List;
import entity.Aluno;
import persistence.AlunoDao;

public class ManagerBean {

	private List listaAluno;

	public List getListaAluno() {
		listaAluno = new ArrayList<>();
		try {
			listaAluno = new AlunoDao().findAll();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return listaAluno;
	}

	public void setListaAluno(List listaAluno) {
		this.listaAluno = listaAluno;
	}
}    

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index</title>

<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
<script type="text/javascript" src="js/bootstrap.js"></script>

</head>

<body>

	<h2 class="well">Web01 com Bootstrap</h2>
	<br />

	<div class="container">
		<div class="col-sm-8 col-sm-offset-2">
			<div class="panel panel-primary">
				<div class="panel-heading">
					<div class="panel-title">Cadastro do Aluno
					</div>	<!--  FECHA PANEL-TITLE -->
				</div>	<!-- FECHA PANEL-HEADING -->

				<div class="panel-body">
				<form method="post" action="Controle?cmd=gravar" >		

					<div class="form-group">
					Codigo: <input 	type="number" 
									name="codigo" 
									value="" 
									size="10"
									placeholder="Digite o codigo" 
									required="required"
									title="Entre com o numero" 
									class="form-control"> 
					</div><!-- FECHA FORM-GROUP -->
					
					<div class="form-group">	
					Nome: <input 	type="text" 
									name="nome" 
									value="" 
									size="35" 
									placeholder="Digite o nome" 
									title="Entre com o Nome com Letras"
									required="required"
									class="form-control">
					</div><!-- FECHA FORM-GROUP -->
					
					<div class="form-group">	
					Email: <input 	type="email" 
									name="email" 
									value="" 
									size="50" 
									placeholder="Digite o email" 
									title="Entre com o Email"
									required="required"
									class="form-control">
					</div><!-- FECHA FORM-GROUP -->
	
					<div class="form-group">	
					Sexo:
							<input type="radio" name="sexo" value="m" checked="checked">Masculino
							<input type="radio" name="sexo" value="f">Feminino
					</div><!-- FECHA FORM-GROUP -->
				
					<div class="form-group">	
					Disciplina:	<select class="form-control" name="disciplina">
						<option>Escolha a Disciplina</option>
						<option value="java">Java</option>
						<option value="oracle">Oracle</option>
						<option value="php">PHP</option>
						<option value="ocjp">OCJP</option>
						<option value="scrum">Scrum</option>
								</select>
					</div><!-- FECHA FORM-GROUP -->
				
				
					<div class="form-group">	
					Nota1: <input 	type="number" 
									name="nota1" 
									value="" 
									size="20" 
									placeholder="Digite a nota1" 
									title="Entre com a Nota1"
									required="required"
									class="form-control">
					</div><!-- FECHA FORM-GROUP -->
 
					<div class="form-group">	
					Nota2: <input 	type="number" 
									name="nota2" 
									value="" 
									size="20" 
									placeholder="Digite a nota2" 
									title="Entre com a Nota2"
									required="required"
									class="form-control">
					</div><!-- FECHA FORM-GROUP -->
				
				<div class="panel panel-footer">
  					<button type="submit" class="btn btn-success">
						<span class="glyphicon glyphicon-floppy-disk"></span> Gravar Cliente
    				</button>
    				
 				<a href="consulta.jsp" class="btn btn-warning">
 				<span class="glyphicon glyphicon-search"></span> Consulta</a> 
 				<br/>
 				<h4 class="well">${msg}</h4>
				</div><!-- FECHA PANEL-FOOTER -->				
				
				</form>	

				</div>	<!-- FECHA PANEL-BODY -->
			</div>	<!-- FECHA PANEL-PRIMARY -->
		</div>	<!-- FECHA COL-SM -->
	</div><!-- FECHA CONTAINER -->


	<div class="panel panel-footer">
		<blockquote class="blockquote-reverse">
			<footer>Website by JavaWebDeveloper Sábado</footer>
		</blockquote>
	</div><!-- FECHA PANEL-FOOTER-->
	
	
</body>
</html>    

Resultado do Sistema...

consulta.jsp

<%@ page import="java.util.*, entity.*, control.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<jsp:useBean id="mb" class="manager.ManagerBean" scope="request"></jsp:useBean>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<link rel="stylesheet" href="css/bootstrap.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>

<title>Consulta</title>
</head>
<body>

<div class="container">
	<div class="well">
		<h2>Consulta de Alunos</h2>		
		<br/>	
		 
<form method="post" action="Controle?cmd=consultar">

<a href="index.jsp" class="btn btn-primary">
<span class="glyphicon glyphicon-home"></span> Home</a>
 
<button type="submit" value="Consultar" class="btn btn-warning">
<span class="glyphicon glyphicon-search"></span> Consultar    
</button>
<br/>
<br/>

 <!-- só aparece a tabela na volta -->
 <%
 if (request.getAttribute("msg")!=null){
 %>
 
 <div class="panel panel-info"> 
  <div class="panel-heading"> </div>
  
  <table class="table table-hover">
    <tr>
	   	<td><b>Codigo</b></td>
	    <td><b>Nome</b></td>  
	    <td><b>Email</b></td>
	    <td><b>Sexo</b></td>
	    <td><b>Disciplina</b></td>
	    <td><b>Nota1</b></td>
	    <td><b>Nota2</b></td>
	    <td><b>Excluir</b></td>
  	</tr>
  	
<c:forEach items="${msg}" var="linha">
<tr>
    <td>${linha.codigo}</td>  
    <td>${linha.nome}</td>
    <td>${linha.email}</td>
    <td>${linha.sexo}</td>
    <td>${linha.disciplina}</td>
    <td>${linha.nota1}</td>
    <td>${linha.nota2}</td>
    <td><a href="Controle?cmd=excluir&codigo=${linha.codigo}" class="btn btn-danger">
    	<span class="glyphicon glyphicon-trash"></span>
    </a></td>
</tr>
</c:forEach>
  </table>
</div>


<%
 }
%>
<p/>
${msg2}
</form>

	</div><!--  FECHA WELL -->
</div><!-- FECHA CONTAINER -->

<div class="container">
	<div class="panel panel-footer" >
		<blockquote class="blockquote-reverse">
			<footer>Website by JavaWebDeveloper Sabado</footer>	 
		</blockquote>
	</div><!-- FECHA PANEL FOOTER -->
</div>

</body>
</html>    

Digitando os dados...

Mensagem de Dados gravados...

Consultando...

Clicando em Consultar...