How to use Mysql in Java EE?

Asked

Viewed 592 times

0

I have a java (desktop) application that does some operations in writing the logs to a table in Mysql, I need to do a page that will show me this log.

I did some tests, but did not succeed. I am 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) {
    %>
        <br />
        <%=req.getCtrnro()%>,
        <%=req.getEnvio()%>:
        <%=req.getRetorno()%>
        <hr />
    <%
    }
    %>
</body>
</html>

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

Requisicoesdao.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>
  • The error says the driver is missing, already checked it?

  • @Henrique already tried to import manually, and even by Maven. I edited the topic and includes pom.xml. The error continues :(

  • Which version of JAVA And which is the application server?

  • @LR10 JAVA 8 - version 1.8.0_131, I am using Tomcat 9 as apache, I am running on my computer on localhost to develop.

  • @Pedrodaher, have you checked in . m2 if mavem is downloading the driver correctly ? your mavem is configured correctly ?

1 answer

2


You need to add to "Dependency Maven".

right click on your project and choose estates.

click on Deployment Assembly.

inserir a descrição da imagem aqui

click on add

inserir a descrição da imagem aqui

click on Java Build Path Entries

select Maven Dependencies

click on Finish.

Deploy the project again.

This is also applicable for the non-Maven project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.