Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - Driver jdbc eclipse
Forum - Java - Driver jdbc eclipse

Avatar
Antetemp0 (Normal User)
Newbie


Messaggi: 2
Iscritto: 04/06/2010

Segnala al moderatore
Postato alle 20:35
Venerdė, 04/06/2010
Ciao a tutti!!Ho istallato eclipse e postgresql.Ho scaricato il driver di postgres e l'ho messo dentro il mio classpath.Dopo di che ho creato il mio server postgres all'interno di eclipse, mettendo i dati di accesso e impostando come driver quello che ho scaricato.Ho creato una tabella clienti su postgres una classe PersistenceException,una cliente ed una dataSource :
Codice sorgente - presumibilmente Java

  1. public class Cliente
  2. {
  3.         private int id_cl;
  4.         private int codice_cliente;
  5.         private String nome;
  6.         private String cognome;
  7.         private String indirizzo;
  8.        
  9.        
  10.        
  11.        
  12.  
  13.         public int getId_cl() {
  14.                 return id_cl;
  15.         }
  16.  
  17.         public void setId_cl(int c)  {
  18.                 this.id_cl=c;
  19.         }
  20.  
  21.         public int getCodice_cliente() {
  22.                 return codice_cliente;
  23.         }
  24.  
  25.         public void setCodice_cliente(int codiceCliente) {
  26.                 codice_cliente = codiceCliente;
  27.         }
  28.  
  29.         public String getNome() {
  30.                 return nome;
  31.         }
  32.  
  33.         public void setNome(String nome) {
  34.                 this.nome = nome;
  35.         }
  36.  
  37.         public String getCognome() {
  38.                 return cognome;
  39.         }
  40.  
  41.         public void setCognome(String cognome) {
  42.                 this.cognome = cognome;
  43.         }
  44.  
  45.         public String getIndirizzo() {
  46.                 return indirizzo;
  47.         }
  48.  
  49.         public void setIndirizzo(String indirizzo) {
  50.                 this.indirizzo = indirizzo;
  51.         }
  52.  
  53.  
  54.        
  55. }


Codice sorgente - presumibilmente Java

  1. mport java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5.  
  6. public class DataSource
  7. {
  8.        
  9.         public String dbURI = "jdbc:postgresql://localhost/Prova:5432";
  10.         public String userName = "postgres";
  11.         public String password = "kaos83";
  12.        
  13.         public Connection getConnection() throws PersistenceException
  14.         {
  15.                 Connection connection;
  16.                 try
  17.                 {
  18.                         Class.forName("org.postgresql.Driver");
  19.                         connection = DriverManager.getConnection("dbURI,userName,password");
  20.                 }
  21.                 catch (ClassNotFoundException e) {
  22.                         throw new PersistenceException(e.getMessage());
  23.                 } catch(SQLException e) {
  24.                         throw new PersistenceException(e.getMessage());
  25.                 }
  26.                 return connection;
  27.         }
  28. }


Codice sorgente - presumibilmente Java

  1. ublic class PersistenceException extends Exception {
  2.  
  3.         /**
  4.          *
  5.          */
  6.         private static final long serialVersionUID = -3835068319580102263L;
  7.  
  8.         public PersistenceException(String msg){
  9.                 super(msg);
  10.         }
  11.        
  12. }


Codice sorgente - presumibilmente Java

  1. mport java.sql.*;
  2.  
  3.  
  4. public class Main
  5. {
  6. public static void main (String [] args) throws PersistenceException
  7.  
  8. {
  9.         Connection connection = null;
  10.         PreparedStatement statement = null;
  11.         DataSource ds = new DataSource();
  12.         Cliente c = new Cliente();
  13.         c.setCodice_cliente(1);
  14.         c.setCognome("rossi");
  15.         c.setId_cl(2);
  16.         c.setIndirizzo("ciao");
  17.         c.setNome("marko");
  18.        
  19.         String query ="INSERT into clienti(codice_cliente,nome,cognome,indirizzo,id_cl) VALUES (?,?,?,?,?)";
  20.         try
  21.         {
  22.                 connection=ds.getConnection();
  23.                 statement = connection.prepareStatement(query);
  24.                 statement.setInt(1, c.getCodice_cliente());
  25.                 statement.setString(2, c.getNome());
  26.                 statement.setString(3, c.getCognome());
  27.                 statement.setString(4,c.getIndirizzo());
  28.                 statement.setInt(5,c.getId_cl());
  29.                 statement.executeUpdate();
  30.         }
  31.         catch (SQLException e)
  32.         {
  33.                 throw new PersistenceException(e.getMessage());
  34.         }
  35.         finally
  36.         {
  37.                 try
  38.                 {
  39.                         if(statement!=null)
  40.                                 statement.close();
  41.                         if(connection!=null)
  42.                                 connection.close();
  43.                 }
  44.                 catch (SQLException e)
  45.                 {
  46.                         throw new PersistenceException (e.getMessage());
  47.                 }
  48.        
  49.         }
  50. }
  51. }



l'errore che mi da č questo:
Exception in thread "main" PersistenceException: org.postgresql.Driver
    at DataSource.getConnection(DataSource.java:22)
    at Main.main(Main.java:22)
cosa sbaglio?Grazie

PM Quote