Assim que terminei a carreira de Java WEB fui por em prática os conceitos que aprendi. E ao tentar criar o CRUD para minha aplicação WEB me deparei com um comportamento estranho. Não consigo abrir uma conexão com o banco HSQLDB através de uma servlet, porém se uso uma classe com um main a conexão funciona normalmente. Detalhes abaixo.
codigo da Servlet:
package br.com.jsantos.aboutmepage.web;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.jsantos.aboutmepage.dao.ConnectionPool;
@WebServlet(urlPatterns = "/acess")
public class Acess extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try(Connection con = new ConnectionPool().getConnection()){
System.out.println("Conexão criada com sucesso!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
codigo do ConnectionPool:
package br.com.jsantos.aboutmepage.dao;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.hsqldb.jdbc.JDBCPool;
public class ConnectionPool {
private DataSource dataSource;
public ConnectionPool() {
JDBCPool pool = new JDBCPool();
pool.setUrl("jdbc:hsqldb:hsql://localhost/new-db");
pool.setUser("SA");
pool.setPassword("");
this.dataSource = pool;
}
public Connection getConnection() throws SQLException {
Connection connection = dataSource.getConnection();
return connection;
}
}
Erro ao executar a Servlet - HTTP Status 500 - Servlet execution threw an exception:
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: org/hsqldb/jdbc/JDBCPool
br.com.jsantos.aboutmepage.dao.ConnectionPool.<init>(ConnectionPool.java:16)
br.com.jsantos.aboutmepage.web.Acess.doPost(Acess.java:24)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.ClassNotFoundException: org.hsqldb.jdbc.JDBCPool
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
br.com.jsantos.aboutmepage.dao.ConnectionPool.<init>(ConnectionPool.java:16)
br.com.jsantos.aboutmepage.web.Acess.doPost(Acess.java:24)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.68 logs.