0

I have to create a very simple batch Java application (an application that run into shell) and that perform some query on an Oracle database using JDBC and I never done it.

I am following this tutorial: http://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html

So I have done in this way to allocate a new Connection object for my application:

import java.sql.*;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello World !!!");

        String partitaIVA = args[0];
        String nomePDF = args[1];

        Connection conn = null;
        Statement  stmt = null;

        try {
            // Step 1: Allocate a database "Connection" object
            conn = DriverManager.getConnection("jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB

        } catch(SQLException ex) {
            ex.printStackTrace();
        }
    }
}

The problem is that when I run the application it seems that the SQLException is thrown because enter into the catch block and print the following error message in the console:

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:eme1
    at java.sql.DriverManager.getConnection(DriverManager.java:602)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at Main.main(Main.java:16)

Why? What cause this problem and how can I fix this issue? What am I missing?

Tnx

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • the supplied link looks like it has some discrepancy, look at the last section of the tutorial, it says, first step is to register driver(which is the prefered step), while the example provided doesn't register any Driver, it directly asking for connection !! – ppuskar Feb 11 '15 at 10:27

3 Answers3

1

You need to have an appropriate ojdbc.jar in you classpath. E.g. see Oracle JDBC ojdbc6 Jar as a Maven Dependency

The problem should be that the linked tutorial describes how to connect to a Mysql, but you're trying to connect to an oracle - therefore you need the oracle driver in your classpath.

Community
  • 1
  • 1
Alexander
  • 2,925
  • 3
  • 33
  • 36
1

How about following oracle's documentation for its Driver. http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html

In the above code, you didn't registered the driver class.

 Class.forName ("oracle.jdbc.OracleDriver");
ppuskar
  • 773
  • 6
  • 9
0

If you are following instructions from the given link. In chapter 2.2 you have instructions how to instal MySql drivers. If you are using an Oracle database, then you'll need to instal Oracle JDBC drivers.

If you don't want affect your JDK installation like in tutorial, you can load driver dynamicaly

d-sauer
  • 1,485
  • 1
  • 15
  • 20