Java.sql.SQLException: No suitable driver found for jdbc: derby:

I am starting with jdbc ... I have a problem running this code:

This code uses the appache derby app and for it to work I first started the derby server.

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

      

And then she started the program

      java -classpath derbyclient.jar -jar TestDB.jar

      

I am setting the class path to C: \ Program Files \ Sun \ JavaDB \ lib \ derby.jar

And I always get this exception

java.sql.SQLException: No suitable driver found for jdbc: derby: // localhost: 1527 / BOOKDB, create = true in java.sql.DriverManager.getConnection (DriverManager.java:602) in java.sql.DriverManager.getConnection ( DriverManager.java:185) at TestDB.getConnection (TestDB.java:63) at 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);
   }
}

      

+2


source to share


2 answers


When a command is called java

with parameters -jar

and, the -classpath

parameter is -classpath

ignored. See the documentation for running Java .

You can use:

Unix / Linux:

java -classpath derbyclient.jar:TestDB.jar TestDB

      



Window:

java -classpath derbyclient.jar;TestDB.jar TestDB

      

or make a manifest that adds derbyclient.jar to the classpath.

+5


source


When you use -jar

is -classpath

ignored. From java

doc
:

When you use this option, the JAR file is the source for all custom classes and other custom classpath parameters are ignored.



Use -classpath

without -jar

and explicitly specify the type containing the main method, or make the jar file manifest refer to the derby jar file.

+2


source







All Articles