Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 23
  1. #1

    [Java] JDBC No suitable driver found

    ciao a tutti
    su invito del moderatore apro un nuovo topic

    Ho questo problema
    java.sql.SQLException: No suitable driver

    quando eseguo il seguente codice preso dal libro deitel


    codice:
    // Fig. 28.23: DisplayAuthors.java
    // Displaying the contents of the authors table.
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    
    public class DisplayAuthors
    {
       // database URL
       static final String DATABASE_URL = "jdbc:mysql://localhost/books";
    
       // launch the application
       public static void main( String args[] )
       {
          Connection connection = null; // manages connection
          Statement statement = null; // query statement
          ResultSet resultSet = null; // manages results
    
          // connect to database books and query database
          try
          {
             // establish connection to database
             connection = DriverManager.getConnection(
                DATABASE_URL, "deitel", "deitel" );
    
             // create Statement for querying database
             statement = connection.createStatement();
    
             // query database
             resultSet = statement.executeQuery(
                "SELECT authorID, firstName, lastName FROM authors" );
    
             // process query results
             ResultSetMetaData metaData = resultSet.getMetaData();
             int numberOfColumns = metaData.getColumnCount();
             System.out.println( "Authors Table of Books Database:\n" );
    
             for ( int i = 1; i <= numberOfColumns; i++ )
                System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
             System.out.println();
    
             while ( resultSet.next() )
             {
                for ( int i = 1; i <= numberOfColumns; i++ )
                   System.out.printf( "%-8s\t", resultSet.getObject( i ) );
                System.out.println();
             } // end while
          }  // end try
          catch ( SQLException sqlException )
          {
             sqlException.printStackTrace();
          } // end catch
          finally // ensure resultSet, statement and connection are closed
          {
             try
             {
                resultSet.close();
                statement.close();
                connection.close();
             } // end try
             catch ( Exception exception )
             {
                exception.printStackTrace();
             } // end catch
          } // end finally
       } // end main
    } // end class DisplayAuthors
    ho eseguito il seguente comando nella cartella che contiene DisplayAuthors

    >java -classpath .;c:\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar DisplayAuthors

    dopo aver avviato il server con il comando mysqld

    insomma dopo aver seguito tutta la procedura del libro ma niente da fare

    Ho visto altre discussioni che in passato hanno affrontato tale problema..
    grazie a chiunque vorrà aiutarmi

  2. #2
    Ciao,
    Il messaggio, come si evince, ti dice che jdbc non è riuscito a trovare un driver da usare per. La tua connessione. È come potrebbe farlo se non effettui il caricamento di tale "driver" - "classe".
    Aggiungi questa istruzione prima di effettuare la connessione:
    codice:
    Class.forName("com.mysql.jdbc.Driver");

  3. #3
    se includo l'istruzione mi da errore; poi sul libro c'è scritto
    In past versions of Java, programs were required to load an appropriate database driver
    before connecting to a database. JDBC 4.0 and higher support automatic driver dis-
    covery—you’re no longer required to load the database driver in advance. To ensure that
    the program can locate the database driver class, you must include the class’s location in
    the program’s classpath when you execute the program. For MySQL, you include the file
    mysql-connector-java-5.1.14-bin.jar (in the C:\mysql-connector-java-5.1.14
    directory) in your program’s classpath, as in:
    java -classpath .;c:\mysql-connector-java-5.1.14\mysql-connector-
    java-5.1.14-bin.jar DisplayAuthors

  4. #4
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Domanda stupida: Il file mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar esiste?
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  5. #5
    si esiste, l'ho scaricato 5 volte, e scompattato in C:\mysql-connector-java-5.1.14;

    il libro parla chiaro ho rifatto tutto non si sa quante volte , ho copiato anche in

    C:\Program Files (x86)\Java\jdk1.5.0_09\jre\lib\ext (il connector j) così è sufficiente scrivere


    java DisplayAuthors per eseguire

    ho provato anche altre versioni del connector;

    mi rimane provare solo a cambiare le variabili d'ambiente ma il libro dice che non è necessario


    C:\Users\io\Documents\fondinf\src\jdbc>java DisplayAuthors
    java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getConnection(DriverManager .java:545)
    at java.sql.DriverManager.getConnection(DriverManager .java:171)
    at DisplayAuthors.main(DisplayAuthors.java:27)
    java.lang.NullPointerException
    at DisplayAuthors.main(DisplayAuthors.java:61)

  6. #6
    Originariamente inviato da testasemidura
    se includo l'istruzione mi da errore; poi sul libro c'è scritto
    In past versions of Java, programs were required to load an appropriate database driver
    before connecting to a database. JDBC 4.0 and higher support automatic driver dis-
    covery—you’re no longer required to load the database driver in advance. To ensure that
    the program can locate the database driver class, you must include the class’s location in
    the program’s classpath when you execute the program. For MySQL, you include the file
    mysql-connector-java-5.1.14-bin.jar (in the C:\mysql-connector-java-5.1.14
    directory) in your program’s classpath, as in:
    java -classpath .;c:\mysql-connector-java-5.1.14\mysql-connector-
    java-5.1.14-bin.jar DisplayAuthors
    Questo lo so anche io....
    Se ti da errore quella istruzione e suppongo che sia un classNotFoundException vuol dire che tale classe non si trova nel classpath quindi vuol dire che il jar di mysql non è stato caricato.
    Prova a rivedere la variabile di sistema classpath e path.
    Controlla che la jvm linkata sia la stessa con la quale stai eseguendo il programma

  7. #7
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,326
    Dalle specifiche di JDBC 4:
    When your
    application attempts to connect the database for the first time,
    DriverManager automatically loads the driver found in
    the application CLASSPATH. This is one of the great
    additions in this version of JDBC.
    Lasciando perdere la variabile d'ambiente PATH che non c'azzecca nulla e lasciando anche perdere la variabile d'ambiente CLASSPATH che fa più danni che altro, tutto ciò che devi verificare è che il driver sia "nel classpath" della tua applicazione (ovvero, sia indicato nel MANIFEST del tuo JAR o, comunque, caricato con l'opzione -cp del comando Java).
    JDBC 4 non cerca di certo il driver per tutti i tuoi hard disk, ma solo nel classpath della tua applicazione (come è ovvio che sia).


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  8. #8
    ho fatto di tutto, vi posto il manifest perchè non so più cosa fare..

    codice:
    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.7.1
    Created-By: 1.5.0_22-b03 (Sun Microsystems Inc.)
    Built-By: mysqldev
    Bundle-Vendor: Sun Microsystems Inc.
    Bundle-Classpath: .
    Bundle-Version: 5.1.14
    Bundle-Name: Sun Microsystems' JDBC Driver for MySQL
    Bundle-ManifestVersion: 2
    Bundle-SymbolicName: com.mysql.jdbc
    Export-Package: com.mysql.jdbc;version="5.1.14";uses:="com.mysql.jdbc.
     log,javax.naming,javax.net.ssl,javax.xml.transform,org.xml.sax",com.m
     ysql.jdbc.jdbc2.optional;version="5.1.14";uses:="com.mysql.jdbc,com.m
     ysql.jdbc.log,javax.naming,javax.sql,javax.transaction.xa",com.mysql.
     jdbc.log;version="5.1.14",com.mysql.jdbc.profiler;version="5.1.14";us
     es:="com.mysql.jdbc",com.mysql.jdbc.util;version="5.1.14";uses:="com.
     mysql.jdbc.log",com.mysql.jdbc.exceptions;version="5.1.14",com.mysql.
     jdbc.exceptions.jdbc4;version="5.1.14";uses:="com.mysql.jdbc",com.mys
     ql.jdbc.interceptors;version="5.1.14";uses:="com.mysql.jdbc",com.mysq
     l.jdbc.integration.c3p0;version="5.1.14",com.mysql.jdbc.integration.j
     boss;version="5.1.14",com.mysql.jdbc.configs;version="5.1.14",org.gjt
     .mm.mysql;version="5.1.14"
    Import-Package: javax.net,javax.net.ssl;version="[1.0.1, 2.0.0)";resol
     ution:=optional,javax.xml.parsers, javax.xml.stream,javax.xml.transfo
     rm,javax.xml.transform.dom,javax.xml.transform.sax,javax.xml.transfor
     m.stax,javax.xml.transform.stream,org.w3c.dom,org.xml.sax,org.xml.sax
     .helpers;resolution:=optional,javax.naming,javax.naming.spi,javax.sql
     ,javax.transaction.xa;version="[1.0.1, 2.0.0)";resolution:=optional,c
     om.mchange.v2.c3p0;version="[0.9.1.2, 1.0.0)";resolution:=optional,or
     g.jboss.resource.adapter.jdbc;resolution:=optional,org.jboss.resource
     .adapter.jdbc.vendor;resolution:=optional
    
    Name: common
    Specification-Title: JDBC
    Specification-Version: 4.0
    Specification-Vendor: Sun Microsystems Inc.
    Implementation-Title: MySQL Connector/J
    Implementation-Version: 5.1.14
    Implementation-Vendor-Id: com.mysql
    Implementation-Vendor: Oracle

  9. #9
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,326
    Quello che hai postato sembra essere il MANIFEST del Connector-J, ovvero del driver...
    Tu quello non lo devi nemmeno toccare... ciò che devi verificare è il MANIFEST della tua applicazione.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  10. #10
    Ma dimmi una cosa non è che per caso hai decompresso il connector...jar di mysql ?
    E cerchi di settare il classpath alla classi contenute in questo jar ?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.