0
My print report is only working in the local application, using Hibernate connection, when I try to run on the server does not work due to lack of connection to the database, how can I use a connection without using the Hibernate and that works in the Web Application?
Reporteriomb.java.
public void imprimirVenda() {
Map<String, Object> parametros = new HashMap<>();
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
RelatorioVenda executor = new RelatorioVenda("/impressao/imp_venda.jasper",
response, parametros, "Impressão da Venda.pdf", this.vendaMB.getVenda().getIdvenda());
Session session = manager.unwrap(Session.class);
session.doWork(executor);
facesContext.responseComplete();
}
Reporteriovenda.java
public class RelatorioVenda implements Work {
private final String caminhoRelatorio;
private final HttpServletResponse response;
private final Map<String, Object> parametros;
private final String nomeArquivoSaida;
private final Long id;
public RelatorioVenda(String caminhoRelatorio,
HttpServletResponse response, Map<String, Object> parametros,
String nomeArquivoSaida, Long id) {
this.caminhoRelatorio = caminhoRelatorio;
this.response = response;
this.parametros = parametros;
this.nomeArquivoSaida = nomeArquivoSaida;
this.id = id;
}
@Override
public void execute(Connection connection) throws SQLException {
try {
String relatorioStream = servlet.getRealPath(this.caminhoRelatorio);
parametros.put("idvenda", id );
JasperPrint print = JasperFillManager.fillReport(relatorioStream, this.parametros, connection);
Exporter<ExporterInput, PdfReportConfiguration, PdfExporterConfiguration, OutputStreamExporterOutput> exportador = new JRPdfExporter();
exportador.setExporterInput(new SimpleExporterInput(print));
exportador.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
response.setContentType("application/pdf");
exportador.exportReport();
} catch (Exception e) {
throw new SQLException("Erro ao executar relatório ", e);
}
}
}
Someone helps me make another connection to the Database. I thank you already
Friend your problem is connecting to the bank the problem is not in the report, you need to analyze your classes that generate the connection, put the connection class ai, such as url you use to connect to the database
– Denis Benjamim
This is the problem that I am unable to solve, trying to make a connection, passing the url, user and password, but giving error
– Guilherme Costa
If you don’t add your connection class it is difficult to help you. In my view Voce is clicking on the server application the url of the local database on your machine and not the database on the server, so I wanted to see your connection class
– Denis Benjamim
I solved the problem with this bank connection.
public Connection getConnection() throws ClassNotFoundException {
 try {
 Class.forName("com.mysql.jdbc.Driver");
 System.out.println("Conectando ao banco");
 return DriverManager.getConnection("jdbc:mysql://localhost:3306/teste", "root", "123");
 } catch (SQLException e) {
 System.out.println("Não conectado");
 throw new RuntimeException(e);
 }
 }
.– Guilherme Costa
After all what was the problem? I am seeing q the URL is still from a local PC and not from a server
– Denis Benjamim
But it served so
– Guilherme Costa