0

I am a newbie at JAVA and SQL.

In one exercise, I need to read data from a sample SQL table in NetBeans.

In "services", I started the JAVA DB server. All good. The following message appeared:

Mon Dec 16 21:40:36 EST 2013 : Security manager installed using the Basic server security policy.
Mon Dec 16 21:40:37 EST 2013 : Apache Derby Network Server - 10.8.3.0 - (1405108) started and ready to accept connections on port 1527

But when I run this program (below), the following error occurs:

SQL Error: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/sample 0 08001

Here are the codes.. how can I make sure the right driver is found?

import java.sql.*;

public class TableReporter {

    public static void main(String[] args) {
        String data = "jdbc:derby://localhost:1527/sample";
        try (
            Connection conn = DriverManager.getConnection(
            data, "app", "APP");
            Statement st = conn.createStatement()) {

            Class.forName("org.apache.derby.jdbc.ClientDriver");

            ResultSet rec =st.executeQuery(
                    "select * " +
                    "from SYS.SYSTABLES" +
                    "order by TABLENAME");
            while(rec.next()){
                System.out.println("TABLEID:\t" + rec.getString(1));
                System.out.println("TABLENAME:\t" + rec.getString(2));
                System.out.println("TABLETYPE:\t" + rec.getString(3));
                System.out.println("SCHEMAID:\t" + rec.getString(4));
                System.out.println();
            }
        st.close();
        } catch (SQLException s){
            System.out.println("SQL Error: " + s.toString() + " "
                    + s.getErrorCode() + " " + s.getSQLState());
        } catch (Exception e) {
            System.out.println("Error: " + e.toString()
                    +e.getMessage());
                }   
    }   
}
Sylvain
  • 553
  • 3
  • 14
  • 26

5 Answers5

2

Don't you need to load the driver before trying to create the connection?

Move this statement

Class.forName("org.apache.derby.jdbc.ClientDriver");

to be the first in the try block.

2

The issue is with the jar file.

Class.forName("org.apache.derby.jdbc.ClientDriver");

Check,

  1. Whether it is installed or not.
  2. If it is installed, is there a need to update your .bashrc file.
  3. Or, have you imported the .jar file in your netBeans IDE.

If all does not works, simply write a simple JDBC program to connect to Database using notpad & run it, check what is the problem with your machine.

Aditya
  • 2,299
  • 5
  • 32
  • 54
  • I had to add the JavaDB library in "Projects".. right-click on "Librabries".. but now when I run it I get this error message about the userid and password: SQL Error: java.sql.SQLNonTransientConnectionException: Connection authentication failure occurred. Reason: userid or password invalid. 40000 08004 – Sylvain Dec 17 '13 at 15:22
  • check out this link, might help you out, http://stackoverflow.com/questions/6172930/sqlnontransientconnectionexception-no-current-connection-in-my-application-whi – Aditya Dec 18 '13 at 03:06
0

Try this changes in your code

Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection(
        data, "app", "APP");
        Statement st = conn.createStatement()) {
0

You need load a jar of derby driver.

Class.forName("org.apache.derby.jdbc.ClientDriver");

0

I had the same problem although am providing

Class.forName("org.apache.derby.jdbc.ClientDriver");

But I discovered my properties file that I am loading the db username and the password from is not in the correct format. it should be like this:

dburi=jdbc:derby://localhost:1527/dbname
user=username
pass=xxxx

and in the code:

CachedRowSetImpl crs = new CachedRowSetImpl();
Properties pr = Properties();
crs.setUrl(pr.getProperty("dburi"));
crs.setUsername(pr.getProperty("user"));
crs.setPassword(pr.getProperty("pass"));
Duaa Zahi
  • 357
  • 2
  • 9