Guest User

Untitled

a guest
Oct 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.73 KB | None | 0 0
  1. <%-- IMPORTS DO SISTENA --%>
  2. <%@page import="java.util.ArrayList" %>
  3. <%@page import="java.util.List" %>
  4. <%@page import="thoth.modelo.bean.AreaCurso" %>
  5. <%@page import="thoth.modelo.dao.AreaCursoDao" %>
  6.  
  7. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  8. <!doctype html>
  9. <html>
  10. <head>
  11.  
  12. <meta charset="utf-8">
  13. <meta http-equiv="X-UA-Compatible" content="IE-edge">
  14. <title>Gerenciador de &Aacute;reas do Curso</title>
  15.  
  16. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
  17. <link href="bootstrap/css/estilo.css" rel="stylesheet">
  18. <link href="bootstrap/js/javascript.js" rel="stylesheet">
  19. <link href="bootstrap/imagem/imagem_thoth.jpeg" rel="icon">
  20.  
  21. <style>
  22. /* PÕE A BARRA DE ROLAGEM VERTICAL NA TABELA LOCAL */
  23. .table-overflow {
  24. max-height:400px;
  25. overflow-y:auto;
  26. }
  27. </style>
  28.  
  29. <!-- SCRIPT RESPONSÁVEL POR APAGAR OS CAMPOS -->
  30. <script id="apaga_campos">
  31. document.getElementById('cx_codigo').value=''; // Limpa o campo
  32. document.getElementById('cx_nome').value=''; // Limpa o campo
  33. </script>
  34.  
  35. </head>
  36. <body>
  37.  
  38. <!-- CABEÇALHO DA PÁGINA -->
  39. <nav class="navbar navbar-dark bg-success">
  40. <div class="col-sm-5">
  41. <img src="bootstrap/imagem/imagem_thoth_logo.jpeg" height="80" width="80" class="img-circle img-responsive">
  42. </div>
  43. <div class="col-sm-7" >
  44. <h1>&Aacute;rea do Curso</h1>
  45. </div>
  46. </nav>
  47.  
  48. <div class="container-fluid">
  49.  
  50. <div class="col-sm-6"> <!-- COLUNA 1 DA PÁGINA -->
  51.  
  52. <h2>&Aacute;reas do Curso</h2> <!-- TÍTULO -->
  53.  
  54. <form action="AreaCurso_Cadastro_Controle" method="post">
  55.  
  56. <div class="form-group"> <!-- agrupará e vai separar o label do campo e dos demais -->
  57. <!-- campo código -->
  58. <label for="cx_codigo">C&oacute;digo:</label>
  59. <input class="form-control" type="number" name="cx_codigo" id="cx_codigo" size="4" maxlength="4" placeholder="0" disabled>
  60.  
  61. </div>
  62.  
  63. <div class="form-group"> <!-- agrupará e vai separar o label do campo e dos demais -->
  64. <!-- campo nome -->
  65. <label for="cx_nome">Nome:</label>
  66. <input class="form-control" type="text" name="cx_nome" id="cx_nome" size="50" maxlength="50" placeholder="Informe a descrição da área" autofocus required>
  67. </div>
  68.  
  69. <div class="form-group"> <!-- agrupará e vai separar o label do campo e dos demais -->
  70. <!-- botão salvar-->
  71. <input type="submit" value="Salvar" id="salvar" name="salvar" class="btn btn-success btn-lg">
  72. <!-- campo limpar -->
  73. <input type="reset" value="Novo" onclick="apaga_campos" id="novo" name="novo" class="btn btn-secondary btn-lg">
  74. </div>
  75.  
  76. </form>
  77.  
  78. <div class="alert alert-success">
  79. <h3>${mensagem}</h3>
  80. </div>
  81.  
  82. </div>
  83.  
  84. <div class="col-sm-6"> <!-- COLUNA 2 DA PÁGINA -->
  85.  
  86. <h1>Lista de &Aacute;reas de Cursos do Sistema</h1>
  87.  
  88. <div class="table-overflow">
  89. <table class="table table-striped table-bordered table-hover"> <!-- TABELA -->
  90. <thead class="success"> <!-- O CABEÇALHO DA TABELA -->
  91.  
  92. <td class="success">C&oacute;digo</td>
  93. <td class="success">Descri&ccedil;&atilde;o</td>
  94. <td class="success"></td> <!-- BOTÃO ALTERAR -->
  95. <td class="success"></td> <!-- BOTÃO EXCLUIR -->
  96.  
  97. </thead>
  98.  
  99. <tbody>
  100.  
  101. <%-- SCRIPTLET - DECLARAÇÃO DO MÉTODO DAO RESPONSÁVEL POR LISTAR AS AREAS DO CURSO --%>
  102. <%
  103. List<AreaCurso> listaArea = new ArrayList<AreaCurso>();
  104. AreaCursoDao areaDao = new AreaCursoDao();
  105.  
  106. listaArea = areaDao.listarAreaCurso();
  107.  
  108. for( AreaCurso area: listaArea ){
  109. %>
  110.  
  111. <tr> <!-- LINHA DA TABELA -->
  112. <td><%= area.getCodigo() %></td>
  113. <td><%= area.getNome() %></td>
  114. <td>
  115. <input type="button" value="Alterar" id="alterar" name="alterar" onclick="location='http://localhost:8080/Thoth/TelaAreaCursoAlterar.jsp?codigoAlterar=<%= area.getCodigo() %>'" class="btn btn-secondary btn-sm">
  116. </td>
  117. <td>
  118. <input type="button" value="Excluir" id="excluir" name="excluir" onclick="location='http://localhost:8080/Thoth/AreaCurso_Excluir_Controle?cd=<%= area.getCodigo() %>'" class="btn btn-danger btn-sm">
  119. </td>
  120. </tr>
  121.  
  122. <%
  123. }
  124. %>
  125.  
  126. </tbody>
  127.  
  128. </table>
  129. </div>
  130.  
  131. </div>
  132.  
  133. </div>
  134.  
  135. <!-- FIM DO CÓDIGO -->
  136. <script src="bootstrap/js/jquery.js"></script>
  137. <script src="bootstrap/js/bootstrap.min.js"></script>
  138. <script src="bootstrap/js/script.js"></script>
  139. </body>
  140. </html>
  141.  
  142. package thoth.modelo.bean;
  143.  
  144. public class AreaCurso {
  145. private Integer codigo;
  146. private String nome;
  147.  
  148. /**
  149. * @return the codigo
  150. */
  151. public Integer getCodigo() {
  152. return codigo;
  153. }
  154.  
  155. /**
  156. * @param codigo the codigo to set
  157. */
  158. public void setCodigo(Integer codigo) {
  159. this.codigo = codigo;
  160. }
  161.  
  162. /**
  163. * @return the nome
  164. */
  165. public String getNome() {
  166. return nome;
  167. }
  168.  
  169. /**
  170. * @param nome the nome to set
  171. */
  172. public void setNome(String nome) {
  173. this.nome = nome;
  174. }
  175.  
  176. public String toString(){
  177. return nome;
  178. }
  179.  
  180. }
  181.  
  182. package thoth.modelo.dao;
  183.  
  184. import java.sql.Connection;
  185. import java.sql.PreparedStatement;
  186. import java.sql.ResultSet;
  187. import java.sql.SQLException;
  188. import java.util.ArrayList;
  189. import java.util.List;
  190. import thoth.modelo.bean.AreaCurso;
  191. import thoth.modelo.conexao.Conexao;
  192.  
  193. public class AreaCursoDao {
  194.  
  195. private Connection conn = null; // Prepara a conexão com o banco de Dados
  196. private PreparedStatement ps = null; // Prepara o ambiente ou caminho para a banco de dados
  197. private ResultSet rs = null; // Envia as funções sql da aplicação para o banco
  198. private String sql = "";
  199. private List<AreaCurso> listaArea = new ArrayList<>();
  200.  
  201. /**
  202. * Método responsável por criar o salvar as ÁREA DO CURSO.
  203. * @param area
  204. */
  205. public void salvar( AreaCurso area )
  206. {
  207. conn = Conexao.getConexao(); // Abri a conexão
  208.  
  209. sql = "INSERT INTO public.areacurso(area_nome)VALUES (?)";
  210.  
  211. try {
  212. ps = conn.prepareStatement(sql);
  213.  
  214.  
  215. ps.setString(1, area.getNome() ); // Parâmetro é igual a um, pois ele só tem uma INTERROGAÇÃO
  216.  
  217. ps.execute(); // Executa ou commit o código SQL
  218.  
  219. conn = Conexao.fecharConexao();
  220.  
  221. } catch (SQLException ex) {
  222. System.out.println("ERRO AO SALVAR - DAO AREACURSO");
  223. }
  224.  
  225. }
  226.  
  227. /**
  228. * Método responável por Pesquisar a AREACURSO do banco de dados.
  229. * @param codigo
  230. * @return
  231. */
  232. public AreaCurso pesquisarAreaCurso( int codigo )
  233. {
  234.  
  235. conn = Conexao.getConexao();
  236.  
  237. sql = "SELECT * FROM public.areacurso WHERE area_cod = ?";
  238.  
  239. AreaCurso area = null;
  240.  
  241. try {
  242. ps = conn.prepareStatement(sql);
  243.  
  244. ps.setInt(1, codigo);
  245. rs = ps.executeQuery();
  246.  
  247. if(rs.next()){
  248.  
  249. area = new AreaCurso();
  250.  
  251. area.setCodigo( rs.getInt("area_cod") );
  252. area.setNome(rs.getString("area_nome"));
  253. }
  254.  
  255. conn = Conexao.fecharConexao();
  256.  
  257. ps.close();
  258. rs.close();
  259.  
  260. } catch (SQLException ex) {
  261. System.out.println("Erro ao Pesquisar - DAO AREACURSO, MOTIVO: "+ex);
  262. }
  263.  
  264. return area;
  265.  
  266. }
  267.  
  268. /**
  269. * Método responsável por listar todas as AREACURSO do banco de dados
  270. * @return
  271. */
  272. public List<AreaCurso> listarAreaCurso(){
  273.  
  274. conn = Conexao.getConexao();
  275.  
  276. sql = "SELECT * FROM public.areacurso";
  277.  
  278. AreaCurso area = null;
  279.  
  280. try {
  281. ps = conn.prepareStatement(sql);
  282.  
  283. rs = ps.executeQuery();
  284.  
  285. while(rs.next()){
  286.  
  287. area = new AreaCurso();
  288.  
  289. area.setCodigo( rs.getInt("area_cod") );
  290. area.setNome( rs.getString("area_nome") );
  291.  
  292. listaArea.add(area);
  293. }
  294.  
  295. conn = Conexao.fecharConexao();
  296.  
  297. ps.close();
  298. rs.close();
  299.  
  300. } catch (SQLException ex) {
  301. System.out.println("ERRO AO LISTAR - DAO AREACURSO");
  302. }
  303.  
  304. return listaArea;
  305.  
  306. }
  307.  
  308. /**
  309. * Método responsável por alterar dados no Banco
  310. * @param area
  311. */
  312. public void alterar( AreaCurso area ) throws SQLException
  313. {
  314.  
  315. conn = Conexao.getConexao(); // Abri a conexão
  316.  
  317. sql = "UPDATE public.areacurso SET area_nome=? WHERE area_cod = ?";
  318.  
  319. ps = conn.prepareStatement(sql);
  320.  
  321. ps.setString(1, area.getNome()); // Parâmetro é igual a um, pois ele só tem uma INTERROGAÇÃO
  322. ps.setInt(2, area.getCodigo()); // Parâmetro é igual a um, pois ele só tem uma INTERROGAÇÃO
  323.  
  324. ps.executeUpdate(); // Executa ou commit o código SQL
  325.  
  326. conn = Conexao.fecharConexao();
  327.  
  328. }
  329.  
  330. /**
  331. * Método responsável por excluir dados do banco de dados
  332. * @param area
  333. */
  334. public void excluir( AreaCurso area ) throws SQLException
  335. {
  336.  
  337. conn = Conexao.getConexao(); // Abri a conexão
  338.  
  339. sql = "DELETE FROM public.areacurso WHERE area_cod = ?";
  340.  
  341. ps = conn.prepareStatement(sql);
  342.  
  343. ps.setInt(1, area.getCodigo()); // Parâmetro é igual a um, pois ele só tem uma INTERROGAÇÃO
  344.  
  345. ps.execute(); // Executa ou commit o código SQL
  346.  
  347. conn = Conexao.fecharConexao();
  348.  
  349. }
  350.  
  351. }
  352.  
  353. package thoth.modelo.conexao;
  354.  
  355. import java.sql.Connection;
  356. import java.sql.DriverManager;
  357. import java.sql.SQLException;
  358.  
  359. public class Conexao {
  360.  
  361. static Connection con = null;
  362.  
  363. private static final String URL = "jdbc:postgresql://localhost/Thoth";
  364. private static final String USER = "postgres";
  365. private static final String SENHA = "123";
  366.  
  367. public static Connection getConexao(){
  368.  
  369. try {
  370.  
  371. Class.forName("org.postgresql.Driver");
  372. con = DriverManager.getConnection(URL, USER, SENHA);
  373. // System.out.println("Conexão ok !");
  374.  
  375. } catch (Exception e) {
  376. System.out.println("Erro na conexao !" +e.getMessage());
  377. }
  378. return con;
  379. }
  380.  
  381. public static Connection fecharConexao()
  382. {
  383. try{
  384. con.close();
  385. }catch(SQLException e){
  386. System.out.println("Erro ao fechar Conexao !" +e.getMessage());
  387. }
  388.  
  389. return con;
  390. }
  391.  
  392. public static void main(String[] args) {
  393. Conexao.getConexao();
  394.  
  395. }
  396.  
  397. }
  398.  
  399. CREATE DATABASE Thoth;
  400.  
  401. CREATE TABLE areacurso(
  402. area_cod SERIAL NOT NULL,
  403. area_nome VARCHAR(5) NOT NULL,
  404. PRIMARY KEY(area_cod)
  405. );
Add Comment
Please, Sign In to add comment