2009-09-03 19 views
2

Je suis un débutant avec jdbc ... J'ai un problème d'exécuter ce code:java.sql.SQLException: Aucun pilote approprié trouvé pour jdbc: derby:

Ce code utilise appache derby et afin de faire ça marche j'ai commencé le serveur derby ..

 java -jar "C:\Program Files\Sun\JavaDB\lib\derbyrun.jar" server start 

Et puis a commencé le programme

 java -classpath derbyclient.jar -jar TestDB.jar 

Je mis le chemin de la classe C : \ Program Files \ Sun \ JavaDB \ lib \ derby.jar

Et je suis toujours obtenir cette exception

java.sql.SQLException: Aucun pilote approprié trouvé pour jdbc: derby: // localhost: 1527/ BookDB; create = true à java.sql. DriverManager.getConnection (DriverManager.java:602) à java.sql.DriverManager.getConnection (DriverManager.java:185) à TestDB.getConnection (TestDB.java:63) à TestDB.runTest (TestDB.java:20) at TestDB.main (TestDB.java:11)

import java.sql.*; 
import java.io.*; 
import java.util.*; 


class TestDB 
{ 
    public static void main(String args[]) 
    { 
     try 
     { 
     runTest(); 
     } 
     catch (SQLException ex) 
     { 
     for (Throwable t : ex) 
      t.printStackTrace(); 
     } 
     catch (IOException ex) 
     { 
     ex.printStackTrace(); 
     } 
    } 

    public static void runTest() throws SQLException, IOException 
    { 
     Connection conn = getConnection(); 
     try 
     { 
     Statement stat = conn.createStatement(); 

     stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))"); 
     stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')"); 

     ResultSet result = stat.executeQuery("SELECT * FROM Greetings"); 
     if (result.next()) 
      System.out.println(result.getString(1)); 
     result.close(); 
     stat.executeUpdate("DROP TABLE Greetings"); 
     } 
     finally 
     { 
     conn.close(); 
     } 
    } 

    public static Connection getConnection() throws SQLException, IOException 
    { 
     Properties props = new Properties(); 
     FileInputStream in = new FileInputStream("database.properties"); 
     props.load(in); 
     in.close(); 

     String drivers = props.getProperty("jdbc.drivers"); 
     if (drivers != null) System.setProperty("jdbc.drivers", drivers); 
     String url = props.getProperty("jdbc.url"); 
     String username = props.getProperty("jdbc.username"); 
     String password = props.getProperty("jdbc.password"); 

     return DriverManager.getConnection(url, username, password); 
    } 
} 

Répondre

5

Lorsque vous appelez la commande java avec les paramètres -jar et -classpath, le paramètre -classpath est ignoré. Voir le documentation for the Java launcher.

Vous pouvez utiliser:

Unix/Linux:

java -classpath derbyclient.jar:TestDB.jar TestDB 

Fenêtres:

java -classpath derbyclient.jar;TestDB.jar TestDB 

ou faire un manifeste qui ajoute derbyclient.jar au classpath.

+0

désolé .. mais je l'ai maintenant !! Exception dans le fil "principal" java.lang.NoClassDefFoundError: TestDB –

+0

bien .. désolé encore simonn ça marche .. J'ai fait une petite erreur !! –

2

Lorsque vous utilisez -jar, -classpath est ignoré. De l'java command tool docs:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

Soit utiliser -classpath sans -jar et spécifiez le type contenant la principale méthode explicitement, ou faire référence jar file manifest le fichier jar derby.