No suitable driver found for jdbc:postgresql - LINUX [RESOLVIDO]

0 respostas
guilherme.thcarlos

Edit: eu consegui resolver o problema, para futuros viajantes a solução foi a seguinte:

- extrai o arquivo .JAR (postgresql-9.1-901.jdbc4.jar) para a pasta do meu programa, e foi gerado a pasta org (dentro dela a pasta postgresql).
- em seguida defini o CLASSPATH (CLASSPATH=/endereco/da/pasta/do/seu/programa) para a pasta principal do meu programa e exportei o classpath (export CLASSPATH)
- por fim alterei a linha final private [color=red]String DRV = "sun.jdbc.odbc.JdbcOdbcDriver";[/color] para [color=blue]final private String DRV = "org.postgresql.Driver";[/color]

e o programa funcionou pefeitamente!

:D

--

Galera, beleza?

Estou criando uma aplicação Swing e quero fazer uma conexão com um banco de dados local (PostgreSQL) entretanto estou tendo o seguinte erro: no suitable driver found for jdbc:postgresql://localhost:5432/ e estou utilizando o GNU/Linux Debian.

Não estou usando nenhuma IDE para construir o programa e todos os arquivos estão dentro da pasta Loja

Segue o código da classe ConnDB, que creio onde esteja o problema:

import java.sql.*;
import javax.swing.*;

public class ConnDB
{
  final private String DRV = "sun.jdbc.odbc.JdbcOdbcDriver";
//   final private String DRV = "org.postgresql.Driver";
  private String url = "jdbc:postgresql://localhost:5432/LojaVirtual";
  private String userName = "postgres";
  private String password = "";
  private Connection conn;
  private Statement st;
  
  public ConnDB(String url, String userName, String password)
  {
    this.url = url;
    this.userName = userName;
    this.password = password;
  }
  
  public boolean conecta()
  {
    boolean result = true;
    try
    {
      Class.forName(DRV);
      conn = DriverManager.getConnection(url,userName,password);
    }
    catch(java.lang.ClassNotFoundException erroClass)
    {
      mensagemErro(erroClass.getMessage());
      result = false;
    }
    catch(SQLException erroSQL)
    {
      mensagemErro(erroSQL.getMessage());
      result = false;
    }
    return result;
  }
  
  public void desconecta()
  {
    boolean result = true;
    try
    {
      conn.close();
    }
    catch(SQLException erroSQL)
    {
      mensagemErro(erroSQL.getMessage());
      result = false;
    }
    
  }
    
    public int regCount(String qry)
    {
      int result = 0;
      try
      {
	st = conn.createStatement();
	ResultSet rs = st.executeQuery(qry);
	rs.next();
	result = rs.getInt(1);
	st.close();
      }
      catch(SQLException sqlex)
      {
	mensagemErro(sqlex.getMessage());
      }
      return result;
    }
    
    public int exeQuery(String qry)
    {
      int result = 0;
      try
      {
	st = conn.createStatement();
	result = st.executeUpdate(qry);
	st.close();
      }
      catch(SQLException sqlex)
      {
	mensagemErro(sqlex.getMessage());
      }
      return result;
    }
    
    public String getColumn(String qry)
    {
      String result = "";
      try
      {
	st = conn.createStatement();
	ResultSet rs = st.executeQuery(qry);
	rs.next();
	result = rs.getString(1);
	st.close();
      }
      catch(SQLException sqlex)
      {
	mensagemErro(sqlex.getMessage());
      }
      return result;
    }
    
    public void getColumn(String qry, String[]reg)
    {
      try
      {
	System.out.println(qry);
	st = conn.createStatement();
	ResultSet rs = st.executeQuery(qry);
	rs.next();
	for(int i = 1; i <= reg.length; i++)
	  reg[i-1] = rs.getString(i);
	st.close();
      }
      catch(SQLException sqlex)
      {
	mensagemErro(sqlex.getMessage());
      }
    }
    
    public void getColumn(String qry, String[][] reg, int lin)
    {
      try
      {
	System.out.println(qry);
	st = conn.createStatement();
	ResultSet rs = st.executeQuery(qry);
	for(int i = 1; i <= lin; i++)
	{
	  if(rs.next())
	    for(int n = 1; n <= reg[n].length; n++)
	      reg[i-1][n-1] = rs.getString(n);
      }
      st.close();
      }
      catch(SQLException sqlex)
      {
	mensagemErro(sqlex.getMessage());
      }
    }
    
    private void mensagemErro(String msg)
    {
      JOptionPane.showMessageDialog(
	null,
	msg,
	"Erro",
	JOptionPane.ERROR_MESSAGE);
    }
}

Se alguém puder me ajudar, agradeço muito!

PS: quando seto o driver como classpath ('CLASSPATH=/usr/share/java/postgresql-9.1-901.jdbc4.jar' e em seguinda o comando 'export CLASSPATH') eu obtenho o seguinte erro:

Exception in thread "main" java.lang.NoClassDefFoundError: Loja
Caused by: java.lang.ClassNotFoundException: Loja
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Loja.  Program will exit.
Criado 3 de dezembro de 2011
Respostas 0
Participantes 1