Error trying to access JAVA WEB database

Asked

Viewed 107 times

0

When I run the program the following error:

HTTP Status 500 error - Internal Server Error.

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

this error when I try to access the database in my web application, but in my Java SE application I can access without problems.

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);
    }
}
}

Contactodao

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 answer

0

This error usually happens because your application server does not contain the driver library natively. Therefore, you should add it manually.

If you are using Wildfly, you will need to make the settings mentioned above. Here is a material to assist with this configuration.

Browser other questions tagged

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