0

libraries used

java db driver - derby.jar

java db driver - derbyclient.jar

java db driver - derbynet.jar

jdk 1.8(default)

SQL Statement

SELECT * FROM NAME.TABLE;

Code:

package database;    
import java.sql.Connection;    
import java.sql.Date;    
import java.sql.DriverManager;    
import java.sql.ResultSet;    
import java.sql.SQLException;    
import java.sql.Statement;

public class DataBase {

    public static void main(String[] args) {    
        //TODO code application logic here    
        Connection myconObj = null;    
        Statement mystatObj = null;    
        ResultSet myresObj = null;    
        String query = "Select * from name.table";    
        try {    
            myconObj = DriverManager.getConnection("jdbc:derby//localhost:1527/database", "username", "paasword");    
            mystatObj = myconObj.createStatement();    
            myresObj = mystatObj.executeQuery(query);  

            while (myresObj.next()) {    
                int id = myresObj.getInt("ID");    
                String name = myresObj.getString("Name");    
                Date date = myresObj.getDate("DateOfBirth");    
                String phone=myresObj.getString("Phone");    
                System.out.println(id +  "\t   "+ name   +"\t   " + date+ "\t   "+ phone);    
            }
        }
        catch (SQLException e) {    
            e.printStackTrace();    
        }    
    }    
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

0

Either JDBC driver is not loaded or the JDBC URL is malformed. Since the connection URL looks fine, the issue should be with loading the driver. For Derby database, the driver class name is org.apache.derby.jdbc.ClientDriver. Use following line before connecting to DB.

Class.forName("org.apache.derby.jdbc.ClientDriver");
Chathura Buddhika
  • 2,067
  • 1
  • 21
  • 35
  • 1
    That hasn't been necessary since Java 6 (with a JDBC 4 or higher driver), although this does help to explicitly confirm the driver is not on the classpath. – Mark Rotteveel Nov 21 '17 at 08:28