0

Quando executo o programa da o seguinte erro:

erro HTTP Status 500 - Internal Server Error.

java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:XE

só da esse erro quando tento acessar o banco na minha aplicação web, mas na minha aplicação Java SE eu consigo acessar sem problemas.

SERVLET

package pacote;


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/www")
public class AdicionaContatoServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException{
    super.init(config);
    log("INICIANDO SERVLET");
}
public void destroy() {
    super.destroy();
    log("DESTRUINDO SERVLET");
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    String nome = request.getParameter("nome");
    String email = request.getParameter("email");
    String endereco = request.getParameter("endereco");
    ContatoDao dao = new ContatoDao();
    Contato contato = new Contato();
    contato.setNome(nome);
    contato.setEmail(email);
    contato.setEndereco(endereco);
    dao.addContato(contato);
    //RequestDispatcher rd = request.getRequestDispatcher("/PrimeiroJsp.jsp");
    //rd.forward(request, response);
}
}

ConnectionFactory

package pacote;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class ConnectionFactory {
static String url = "jdbc:oracle:thin:@localhost:1521:XE";
static String user = "system";
static String password = "1234";
public Connection getConnection() {
    try {
        return DriverManager.getConnection(url, user, password);
    }
    catch(SQLException e) {
        throw new RuntimeException(e);
    }
}

public void closeConnection(Connection connection) {
    try {
        connection.close();
    }
    catch(SQLException e) {
        throw new RuntimeException(e);
    }
}
}

ContatoDao

package pacote;


import pacote.Contato;
import pacote.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class ContatoDao {
Connection connection = null;
ConnectionFactory cf = new ConnectionFactory();

public void addContato(Contato contato) {
    connection = cf.getConnection();
    String sql = "INSERT INTO CONTATO VALUES (?,?,?)";
    try {
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, contato.getNome());
        stmt.setString(2, contato.getEmail());
        stmt.setString(3, contato.getEndereco());
        stmt.execute();
        stmt.close();
    }
    catch(SQLException e) {
        throw new RuntimeException(e);
    }
    finally {
        cf.closeConnection(connection);
    }
}

public void delContato(Contato contato) {
    try {
        connection = new ConnectionFactory().getConnection();
        String sql = "DELETE FROM CONTATO WHERE NOME = ?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, contato.getNome());
    }
    catch(SQLException e) {
        throw new RuntimeException(e);
    }
    finally {
        cf.closeConnection(connection);
    }
}

public void attContato(Contato contato) {
    try {
        connection = new ConnectionFactory().getConnection();
        String sql = "UPDATE CONTATO SET NOME = ?, EMAIL = ?, ENDERECO = ? WHERE NOME = ?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, contato.getNome());
        stmt.setString(2, contato.getEmail());
        stmt.setString(3, contato.getEndereco());
        stmt.setString(4, contato.getNome());
    }
    catch(SQLException e) {
        throw new RuntimeException(e);
    }
    finally {
        cf.closeConnection(connection);
    }
}

public List<Contato> getLista() {
    List<Contato> contatos = new ArrayList<>();
    try {
        connection = new ConnectionFactory().getConnection();
        String sql = "SELECT * FROM CONTATO WHERE NOME = ?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        Contato contato = new Contato();
        while(rs.next()) {
            contato.setNome(rs.getString("nome"));
            contato.setEndereco(rs.getString("endereco"));
            contato.setEmail(rs.getString("email"));
            stmt.execute();
            contatos.add(contato);
        }
        stmt.close();
        rs.close();
    }
    catch(SQLException e) {
        throw new RuntimeException(e);
    }
    finally {
        cf.closeConnection(connection);
        return contatos;
    }
    
}
}

1 Resposta 1

0

Este erro acontece geralmente pelo motivo de o seu servidor de aplicações não conter a biblioteca do driver de forma nativa. Portanto, você deverá adicioná-la manualmente.

Caso você esteja utilizando o Wildfly, será preciso fazer as configurações mencionadas acima. Aqui tem um material pra auxiliar com essa configuração.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .