How to use MySql in Java EE?



  • I have an application in java (desktop) that does some operations in recording the logs in a table in MySql, I need to make a page that will show me that log.

    I did some tests, but I didn't succeed. I'm using Tomcat 9 and eclipse to run.

    inserir a descrição da imagem aqui


    Index. jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="banco.RequisicoesDAO" %>
    <%@ page import="classes.Requisicoes" %>
    <%@ page import="java.util.List" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>DashBoard API</title>
    </head>
    <body>
    <h1>Teste</h1>
    <%
        RequisicoesDAO dao = new RequisicoesDAO();
        List<Requisicoes> reqs = dao.getRequisicoes(0);
    
    for (Requisicoes req : reqs) {
    %&gt;
        &lt;br /&gt;
        &lt;%=req.getCtrnro()%&gt;,
        &lt;%=req.getEnvio()%&gt;:
        &lt;%=req.getRetorno()%&gt;
        &lt;hr /&gt;
    &lt;%
    }
    %&gt;
    

    </body>
    </html>


    Requirements. java:

    package classes;

    public class Requisicoes {

    private int unncod;
    private int ctrnro;
    
    private int code;
    private String envio;
    private String retorno;
    
    public Requisicoes() {}
    
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getEnvio() {
        return envio;
    }
    public void setEnvio(String envio) {
        this.envio = envio;
    }
    public String getRetorno() {
        return retorno;
    }
    public void setRetorno(String retorno) {
        this.retorno = retorno;
    }
    
    public int getUnncod() {
        return unncod;
    }
    
    public void setUnncod(int unncod) {
        this.unncod = unncod;
    }
    
    public int getCtrnro() {
        return ctrnro;
    }
    
    public void setCtrnro(int ctrnro) {
        this.ctrnro = ctrnro;
    }
    

    }


    Required. java:

    package banco;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import classes.Requisicoes;

    public class RequisicoesDAO {
    private Connection conn;
    private PreparedStatement stmt;

    public RequisicoesDAO() throws SQLException, ClassNotFoundException {
        this.conn = DriverManager.getConnection("jdbc:mysql://DOMINIO:3306/BANCO", "USER", "SENHA");
    }
    
    private void conecta()  throws SQLException {
        this.conn = DriverManager.getConnection("jdbc:mysql://DOMINIO:3306/BANCO", "USER", "SENHA");
    }
    
    public List&lt;Requisicoes&gt; getRequisicoes(int processo) throws Exception{
        List&lt;Requisicoes&gt; reqs = new ArrayList&lt;Requisicoes&gt;();
        String tabela = getTable(processo);
        String sql = "SELECT * FROM "+tabela+" WHERE data &gt;= CURDATE()";
        System.out.println(sql);
    
    
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
    
        if (rs.next()) {
            Requisicoes req = new Requisicoes();
            if(processo == 2 ) { 
                req.setUnncod(2);
                req.setCtrnro(rs.getInt("operacional"));
                int code = rs.getString("status").equals("OK") ? 200 : 500; 
                req.setCode(code);
                req.setEnvio(rs.getString("jsonEnv"));
                req.setRetorno(rs.getString("jsonRet"));
            }else {
                req.setUnncod(rs.getInt("unncod"));
                req.setCtrnro(rs.getInt("ctrnro"));
                req.setCode(rs.getInt("status"));
                req.setEnvio(rs.getString("envio"));
                req.setRetorno(rs.getString("retorno"));
            }
    
            reqs.add(req);
            ps.close();     
    
        }
        return reqs;
    }
    
    private String getTable(int processo) {
        switch(processo) {
        case 0: 
            return "tableUm";
        case 1:
            return "tableDois";
        case 2:
            return "tableTres";
        }
    
        return null;
    }
    

    }


    pom. xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>DashBoard-API</groupId>
    <artifactId>DashBoard-API</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>DashBoard-API</name>
    <description>DashBoard API Java</description>

    &lt;properties&gt;
        &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
    &lt;/properties&gt;
    
    &lt;dependencies&gt;
    
        &lt;!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
            &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
            &lt;version&gt;4.0.0&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
    
        &lt;!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;mysql&lt;/groupId&gt;
            &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
            &lt;version&gt;6.0.6&lt;/version&gt;
        &lt;/dependency&gt;
    
    &lt;/dependencies&gt;
    

    </project>



  • You need to add "Maven dependency".

    right click your project and choose properties.

    click Deployment Assembly.

    inserir a descrição da imagem aqui

    click add

    inserir a descrição da imagem aqui

    click Java Build Path Entries

    select Maven Dependencies

    click Finish.

    Deploy the project again.

    This is also applicable for the non-Maven project.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2