How to connect Oracle to Java? This error appears "Error while registering: oracle.jdbc.driver.Oracledriver"

Asked

Viewed 69 times

-1

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class Principal {
    public static void main(String[] args) {
        Connection c;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:oracle:thin:@localhost:1521: XE";
        String usuario = "SYSTEM";
        String senha = "BANCO_OPET";
        String sql = "";

        try {;
            // 1º estabelecer a conexao
            Class.forName("oracle.jdbc.driver.OracleDriver");
            c = DriverManager.getConnection(url, usuario, senha);
            System.out.println("Conectou com sucesso");

            rs.close();
            ps.close();
            c.close();
            System.out.println("Fechou a conexão");
        } catch (SQLException e) {
            System.out.println("Erro ao conectar: " + e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println("Erro ao registrar: " + e.getMessage());
        }
    }
}

1 answer

0

Probably the driver is not in the classpath.

How to add to classpath will depend on your configuration (Eclipse, Netbeans, Intellij Idea, Maven, Gradle, files. jar added in hand, etc...).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.