0

I'm getting an error java.sql.SQLException No suitable driver for jdbc:derby:books when I try to run a file from command line. In Eclipse, everything works fine. I read a book "Java, How To Program" Deitel&Deitel and the file is an example from it. When I try to compile program from command line it shows no error, but the problem is with running. Please help

public class DisplayAuthors {
 public static void main(String args[]) {
  final String DATABASE_URL = "jdbc:derby:books";        
  final String SELECT_QUERY =                            
     "SELECT authorID, firstName, lastName FROM authors";
  String user="deitel";
  String password="deitel";


  try ( 

    Connection connection = DriverManager.getConnection(      
     DATABASE_URL, user,password);                     
     Statement statement = connection.createStatement();       
     ResultSet resultSet = statement.executeQuery(SELECT_QUERY)) {


     ResultSetMetaData metaData = resultSet.getMetaData();
     int numberOfColumns = metaData.getColumnCount();     

     System.out.printf("Table Authors database books:%n%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();
     } 
  }
  catch (SQLException sqlException) {
     sqlException.printStackTrace();
  }                                                   
} 
}

Command line execution:

javac DisplayAuthors.java
java DisplayAuthors 
Dubas
  • 2,855
  • 1
  • 25
  • 37
rj555
  • 1
  • 2
  • 3
    Please add the command used to execute in the command line. Maybe you are missing to put the classpath to the Derby jar or other required dependencies – Dubas Sep 09 '19 at 11:02
  • first is javac DisplayAuthors.java, but then java DisplayAuthors – rj555 Sep 09 '19 at 11:10
  • When you use `java DisplayAuthors` you aren't specifying a classpath, so the driver is not available. You need to use `java -cp .; DisplayAuthors` (on Linux or MacOS, use `:` instead of `;` to separate classpath entries). – Mark Rotteveel Sep 09 '19 at 11:24
  • I've tried java -cp .; DisplayAuthors and it displays 'Cannot find particular file', but this file exists – rj555 Sep 09 '19 at 11:39
  • @rj638 and what did you put in the place for ``? You saying "but the file exists" is irrelevant, the computer can't find it and it's your fault. – Kayaman Sep 09 '19 at 11:43

2 Answers2

1

You are running in the command line your class file without the dependencies.

In your eclipse IDE you maybe have a derby.jar or similar dependencies and eclipse adds all automatically to the execution. Is required to add all the dependencies when you are executing directly from the command line.

If you are note creating a runnable jar with dependencies in MANIFEST.MF and you are trying to execute the class directly is required to add the -cp parameter with the path to all the dependencies:

Example:

java -cp Derby.jar;. DisplayAuthors

Summing that the Derby.jar and your class are in the same place and there is no more dependencies to add.

More information about:

Java Command line (Oracle Java9 SE)

Differences between "java -cp" and "java -jar"?

Dubas
  • 2,855
  • 1
  • 25
  • 37
0

java.sql.SQLException. No suitable driver for jdbc

The above error jumps when JDBC DriverManager can't find any suitable driver for the given connection URL. Either the JDBC driver isn't loaded at all before connecting the DB, or the connection URL is wrong.

The URL should be like this,

jdbc:derby://localhost:1527/dbname;create=true;

or

jdbc:derby:books;create=true;

Use create=true if you want the database to be created if it doesn't exist.

And finally, check that Derby JAR file is on the classpath. If you can't find it, then you can download the JAR from here and add to the project Library folder.

For Apache Derby, the driver class name is org.apache.derby.jdbc.ClientDriver. So put that as follows,

Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection connection = DriverManager.getConnection(DATABASE_URL, user, password);  

Make sure your URL, username and the password is correct, and try to run your code.

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
  • 2
    Loading drivers using `Class.forName` is not necessary in normal Java applications, and hasn't been necessary for almost 15 years. Explicitly loading the driver is only necessary if the driver is not on the initial classpath, but on a context classpath (eg in a web application when the driver is included in the WAR instead of on the application server classpath). – Mark Rotteveel Sep 09 '19 at 11:23
  • @MarkRotteveel Thank you for that information. I also didn't know that before you tell. Because I see more people telling everywhere that need to add `Class.forName`. – Hasitha Jayawardana Sep 09 '19 at 11:30