Mudança para DataSource gerou No suitable driver

2 respostas
L

Tentei alterar uma classe que funcionava a conexão direta (exemplo 1) por outra utilizando DataSource (exemplo 2), mas apresentou erro no “conn.createStatement()” de “No suitable driver”.
Alguém sabe dizer porque?

[color=brown]Exemplo: 1

public static Connection getConnection() throws Exception

  {

    try

    {[/color][color=red]

      DriverManager.registerDriver(new com.mysql.jdbc.Driver());

      conn = DriverManager.getConnection(jdbc:mysql://localhost:3307/DevAcc?user=root&password=root”);    }[/color][color=brown]    catch (SQLException se)

    {

      throw new Exception(se);

    }

    return conn;

  }
public ArrayList findAll() throws Exception

  {

    String command ;

    ArrayList resultCommand = new ArrayList();

    command = select +

              "       ID_PROJETO " +

              "      ,COD_PROJETO " +

              "      ,DES_TITLE " +

              "      ,DES_PROJETO " +

              "      ,DES_LOCAL " +

              "      ,DES_PROPERTY " +

              "      ,DES_BEAN " +

              "      ,DES_BUSINESS " +

              "      ,DES_DATABASE " +

              "      ,DES_DAO " +

              "      ,DES_UI " +

              "      ,DES_HTML " +

              "from PROJETO ";
resultCommand = this.executeArray(command);

    return resultCommand;

  }

  public ArrayList executeArray(String newComando) throws Exception
  {

result = new ArrayList();

    try

    {

      // Conecta no banco de dados

      Connection conn = ConexaoMySQL.getConnection();

      Statement rs = conn.createStatement();

      ResultSet rset = rs.executeQuery(newComando);

      this.result = this.montaArray(rset);;

// Fecha Connection e CallableStatement

      ConexaoMySQL.closeConnection();

      rs.close();

      rset.close();

      this.codErro = 0;

      return this.result;

    }

    catch(SQLException se)

    {

      this.codErro = Integer.toString(se.getErrorCode());

      this.msgErro = se.getMessage();

      // Fecha Connection e CallableStatement

      ConexaoMySQL.closeConnection();

      this.result = result;

      return this.result;

    }

  }

=========================================

Exemplo: 2

public static Connection getConnection() throws Exception

  {

    try

    {

      [/color][color=red]DataSource ds = null;

      DriverManager.registerDriver(new com.mysql.jdbc.Driver());

      InitialContext ic = new InitialContext();

      ResourceBundle resource = ResourceBundle.getBundle(Teste);

      ds = (DataSource) ic.lookup(resource.getString(Teste.datasource));

      conn = ds.getConnection();[/color][color=brown]    }

    catch (SQLException se)

    {

      throw new Exception(se);

    }

    return conn;

  }
public ArrayList findAll() throws Exception

  {

    String command ;

    ArrayList resultCommand = new ArrayList();

    command = select +

              "       ID_PROJETO " +

              "      ,COD_PROJETO " +

              "      ,DES_TITLE " +

              "      ,DES_PROJETO " +

              "      ,DES_LOCAL " +

              "      ,DES_PROPERTY " +

              "      ,DES_BEAN " +

              "      ,DES_BUSINESS " +

              "      ,DES_DATABASE " +

              "      ,DES_DAO " +

              "      ,DES_UI " +

              "      ,DES_HTML " +

              "from PROJETO ";
resultCommand = this.executeArray(command);

    return resultCommand;

  }

  public ArrayList executeArray(String newComando) throws Exception
  {

result = new ArrayList();

    try

    {

      // Conecta no banco de dados

      Connection conn = ConexaoMySQL.getConnection();

[/color][color=red]

      Statement rs = conn.createStatement();[/color][color=brown]

      ResultSet rset = rs.executeQuery(newComando);

      this.result = this.montaArray(rset);;

// Fecha Connection e CallableStatement

      ConexaoMySQL.closeConnection();

      rs.close();

      rset.close();

      this.codErro = 0;

      return this.result;

    }

    catch(SQLException se)

    {

      this.codErro = Integer.toString(se.getErrorCode());

      this.msgErro = se.getMessage();

      // Fecha Connection e CallableStatement

      ConexaoMySQL.closeConnection();

      this.result = result;

      return this.result;

    }

  }

=========================================[/color]

2 Respostas

R

Para resolver este problema você deve adicionar o driver de conexão com o MySQL em seu CLASSPATH.

Espero ter ajudado.

L

Sua resposta ajudou a encontrar a solução.
O problema estava no Classpath, mas do JDeveloper (0C4J) e não do projeto.
Coloquei o Driver na área dele e funcionou.

Valeu, obrigado.

Criado 6 de novembro de 2006
Ultima resposta 8 de nov. de 2006
Respostas 2
Participantes 2