No suitable driver found (Mas o driver esta la !)

Asked

Viewed 60 times

0

Guys, I made a first application through the Caelum site in Java EE, a calendar, and all right, all right, fine, I added contacts to MYSQL quietly in the Java part of the workbook, the JDBC driver worked all right and so on... But when I went to Java EE, the program works perfectly, except when I click on "record", it says that no driver was found, however, I added the driver in the same way as in the Java application, by the Build path-add to build path, I don’t know what the problem is, here are the links of the lessons:

http://www.caelum.com.br/apostila-java-web/servlets/ http://www.caelum.com.br/apostila-java-web/bancos-de-dados-e-jdbc/

and my code... is updated now that the problem has been solved. Simply add the Try/catch method to the Add-On class after Class.forname which was added to the Connectionfactory class !

package minha.agenda.ozzy;

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

public class ConnectionFactory{
    public Connection getConnection () throws ClassNotFoundException{
        try{
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection(
                        "jdbc:mysql://localhost/bDados", "gabrielDados", "25120107");
        } catch(SQLException e){
            throw new RuntimeException(e);
        }
    }
}

package minha.agenda.ozzy;

import java.util.Calendar;

public class Contato {
    private Long id;
    private String nome;
    private String email;
    private String end;
    private Calendar dataNascimento;

    public Long getId(){
        return id;
    }
    public void setId(Long id){
        this.id = id;
    }

    public String getNome(){
        return nome;
    }
    public void setNome(String nome){
        this.nome = nome;
    }

    public String getEmail(){
        return email;
    }
    public void setEmail(String email){
        this.email = email;
    }

    public String getEndereco(){
        return end;
    }
    public void setEndereco(String end){
        this.end = end;
    }

    public Calendar getDataNascimento(){
        return dataNascimento;
    }
    public void setDataNascimento(Calendar dataNascimento){
        this.dataNascimento = dataNascimento;
    }
}

package minha.agenda.ozzy;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.SQLException;

public class ContatoDao {
    private Connection connection;

    public ContatoDao() throws ClassNotFoundException{
        this.connection = new ConnectionFactory().getConnection();
    }
    public void adiciona(Contato contato){
        String sql = "insert into contatos"+
                    "(nome, email, endereco, dataNascimento)"+
                    "values (?,?,?,?)";

        try{
            PreparedStatement stmt = connection.prepareStatement(sql);

            stmt.setString(1,contato.getNome());
            stmt.setString(2,contato.getEmail());
            stmt.setString(3,contato.getEndereco());
            stmt.setDate(4, new Date(
                        contato.getDataNascimento().getTimeInMillis()));

            stmt.execute();
            stmt.close();
        } catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}

package minha.agenda.ozzy;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

@WebServlet("/adicionaContato")
public class AdicionaContatoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service (HttpServletRequest request,
                            HttpServletResponse response)
                            throws IOException, ServletException{

        PrintWriter out = response.getWriter();

        String nome = request.getParameter("nome");
        String email = request.getParameter("email");
        String end = request.getParameter("end");
        String dataEmTexto = request.getParameter("dataNascimento");
        Calendar dataNascimento = null;

        try {
            Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dataEmTexto);
            dataNascimento = Calendar.getInstance();
            dataNascimento.setTime(date);
        } catch (ParseException e){
            out.println("Erro de conversão de data");
            return;
        }

        Contato contato = new Contato();
        contato.setNome(nome);
        contato.setEmail(email);
        contato.setEndereco(end);
        contato.setDataNascimento(dataNascimento);

        ContatoDao dao = null;
        try{
            dao = new ContatoDao();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        dao.adiciona(contato);

        out.println("<html>");
        out.println("<body>");
        out.println("Contato" + contato.getNome() + "adicionado com sucesso!");
        out.println("</body>");
        out.println("</html>");
    }
}

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1> Adiciona Contatos </h1>

    <form action ="adicionaContato">
        Nome: <input type="text" name="nome" /><br />
        Email: <input type="text" name="email" /><br />
        Endereço: <input type="text" name="end" /><br />
        Data de Nascimento: <input type="text" name="dataNascimento" /><br />
        <input type="submit" value="Gravar" />
    </form>
</body>
</html>
  • Try to add the following code just before the line return DriverManager..., inside the Try: Class.forName("com.mysql.jdbc.Driver");

  • I tried, man, but I had to change some codes of the other classes and... it didn’t work either !

  • What made you have to change other classes? It was the above code I asked you to add?

  • Are you using a Web Container? Which one? If you are using, does the Mysql jar not need to be in a Container folder for it to load into ClassLoader?

  • 2

    From what I saw in the booklet, it uses Tomcat, you need to place the Mysql Connector jar inside the Tomcat lib folder (http://stackoverflow.com/questions/1610277/tomcat-does-not-recognize-the-mysql-jar-library).

  • @Math, yes man, as soon as I changed the Connectionfactory I had to change Contact and Contact...

  • @Wakim, the worst is that I’ve tried this too but... Unsuccessful!

  • @Math, I was able to solve the error I was giving and your solution turned out ! What is Class.forname ? what he does, could you please explain to me ? Thank you !

  • @user3632011 I am marking your question as duplicate of a similar one. I will try to elaborate better my answer there because it is not very deep, but it will have to be a little later because I am working today.

Show 4 more comments
No answers

Browser other questions tagged

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