How to use MySql in Java EE?

0

I have an application in java (desktop) that does some operations on writes the logs to a table in MySql, I need to make a page that will display this log.

I did some testing, but I did not succeed. I'm using Tomcat 9 and eclipse to run.

Index.jsp:

<%@pagelanguage="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) {
    %>
        <br />
        <%=req.getCtrnro()%>,
        <%=req.getEnvio()%>:
        <%=req.getRetorno()%>
        <hr />
    <%
    }
    %>
</body>
</html>

Requisitions.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;
    }
}

REQUIRESDAO.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<Requisicoes> getRequisicoes(int processo) throws Exception{
        List<Requisicoes> reqs = new ArrayList<Requisicoes>();
        String tabela = getTable(processo);
        String sql = "SELECT * FROM "+tabela+" WHERE data >= 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>

    <properties>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

    </dependencies>

</project>
    
asked by anonymous 27.12.2017 / 17:32

1 answer

1

You need to add the " Maven dependency ".

Right click on your project and choose Properties .

click Deployment Assembly .

clickadd

click Java Build Path Entries

Select Maven Dependencies

click Finish .

Deploy the project again.

This is also applicable for the non-maven project.

    
09.01.2018 / 17:26