Error to read the Oracle JDBC driver

0

Good morning.

Create a class in the Eclipse-Kepler IDE to feed a Database with a text file, in the project manage the ojdbc14 JDK 1.7 driver for the connection. Inside eclipse everything works correctly , but when compiling it with Maven and executing the .jar it sends me the following error:

"Not suitable driver found for jdbc: oracle: thin: @ xxx.xxx.xxx.xxx: xxxx: BDNAME Failed to read the java.lang.NullPointerException file. "

I occupy a class for the connection:

public class DespachadorBD {

    public static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String DB_CONNECTION = "jdbc:oracle:thin:@192.xxx.xxx.xxx:XXXX:DBNAME";
    public static final String DB_USER = "User";
    public static final String DB_PASSWORD = "Pass";    
}
...
...

private static Connection getDBConnection() {

                Connection dbConnection = null;
                try {
                    Class.forName(DB_DRIVER);
                } catch (ClassNotFoundException e) {
                    System.out.println(e.getMessage());
                }
                try {
                    dbConnection = DriverManager.getConnection(
                                    DB_CONNECTION, DB_USER,DB_PASSWORD);
                    return dbConnection;
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
                return dbConnection;
            }
}

Thank you in advance for your support and comments. Greetings.

    
asked by Fer Marty 17.11.2017 в 20:17
source

1 answer

0

I was looking for another way and I was able to solve the conflict of the exception of Driver Oracle JDBC .

I share the league that helped me with the problem, I made some corrections and additions for my class and I also share another league with the theory.

The conflict has to do with the ClassLoader and you can review the theory here and the method that I took to load ojdbc you can check it in the league .

Finally, I share the class I occupy, adapting it to a method and sending it to call where necessary.

package com;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.SQLException;

public class PruebaCambioClassPath {

    public void main(String[] args) {
        try {
            driver();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }


    }

    public void driver() throws SQLException {

        // Comprobamos que en el classpath no existe el driver de mysql.
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("no se encuentra com.mysql.jdbc.Driver");
        }

        // Cambiamos el classpath, anadiendo un nuevo jar al ClassLoader.
        try {
            // Se obtiene el ClassLoader y su metodo addURL()
            URLClassLoader classLoader = ((URLClassLoader) ClassLoader
                    .getSystemClassLoader());
            Method metodoAdd = URLClassLoader.class.getDeclaredMethod("addURL",
                    new Class[] { URL.class });
            metodoAdd.setAccessible(true);

            // La URL del jar que queremos anadir
            URL url = new URL(
                    "file:///C:/Users/Usuario/.m2/repository/com/oracle/10.2.0.5.0/oracle-10.2.0.5.0.jar");

            // Se invoca al metodo addURL pasando esa url del jar
            metodoAdd.invoke(classLoader, new Object[] { url });
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Se comprueba que ahora si se puede acceder a esa clase.
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println("ya se encuentra com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Pues no, sigue sin estar accesible");
        }

    }
}
    
answered by 21.11.2017 в 19:15