No suitable driver error

Hi Experts,
                  I have the following code that parses an XML file and stores it in an Oracle DB.

                 Whenever I am trying to run this code, I am getting the error:

               java.sql.SQLException: No suitable driver
      at java.sql.DriverManager.getConnection(DriverManager.java:592)
      at java.sql.DriverManager.getConnection(DriverManager.java:196)

                I have kept Xerces Jar and ojdbc14.jar in my classpath.
package com.si.keyNote;
 
import java.io.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.sql.*;
import javax.sql.*;
import java.util.*;
public class AgentAttributesReader {
	 
	  String url = "jdbc:oracle:thin:@abc.xyz.com:1522:omft01";
	  String user = "user";
	  String pwd = "pass";
	  
 
     public static void main(String[] args) {
    try {
      String xmlfile ="C:"+File.separator+"KeyNoteDump"+File.separator+"XMLparser"+File.separator+"web"+File.separator+"20090312_0000.xml";
      
      File file = new File(xmlfile);
      if (file.exists()){
        //SAX-implementation:
        SAXParserFactory factory = SAXParserFactory.newInstance();
        // Create SAX-parser
        SAXParser parser = factory.newSAXParser();
        //System.out.println("Slno:\t" +"Name:\t" + "Value:");
        //Define a handler
        SaxHandler handler = new SaxHandler();
        // Parse the xml document
        parser.parse(file, handler);
      }
      else{
        System.out.println("File not found!");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  private static class SaxHandler extends DefaultHandler {
      
      
    public void startElement(String uri, String localName, String qName,
                    Attributes attrs) throws SAXParseException,SAXException {
          String Sql="";
          Connection conn = null;
          try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection("url","user","pwd");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  //
 catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  
 
      /* GETS ALL THE ATTRIBUTES OF AGENT_META_DATA */
       if (qName.equals("AGENT_META_DATA")) {
               
                
                try{
 
               // System.out.println( conn );
                PreparedStatement pstmt = null;
                
                
 
                Sql= "insert into apm.agent_meta_data1 (AGENT_ID, AGENT_INSTANCE_ID, IP_ADDR, BACKBONE, CITY, COUNTRY, Weight) values (?,?,?,?,?,?,?)";
 
                pstmt = conn.prepareStatement(Sql);
                
                pstmt.setInt(1,new Integer(attrs.getValue("agent_id")));
                pstmt.setInt(2,new Integer(attrs.getValue("instance_id")));
                pstmt.setString(3,attrs.getValue("ip"));
                pstmt.setString(4,attrs.getValue("backbone"));
                pstmt.setString(5,attrs.getValue("city"));
                pstmt.setString(6,attrs.getValue("country"));
                pstmt.setString(7,attrs.getValue("weight"));
                pstmt.execute();
 
                pstmt.close();
                conn.close();
                conn = null;
 
                //System.out.println( "theconnection is closed "+conn );
                }catch(Exception sq){
                sq.getMessage();
                }
 
            }/**/
 
   
 
 
     
    }
 
   
  }
}

Open in new window

LVL 10
aman0711Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
>                         conn = DriverManager.getConnection("url","user","pwd");

should be:

                        conn = DriverManager.getConnection(url,"user","pwd");

0
 
warturtleCommented:
Are you able to print your classpath here, so that I can see what it is?
0
 
aman0711Author Commented:
Hi Warturtle,
                          I am very new to all this. Could you please tell me how??
     
                        will this code help me getting classpath



                        import java.net.URL;
import java.net.URLClassLoader;
 
public class PrintClasspath {
    public static void main(String[] args) {
 
        //Get the System Classloader
        ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
 
        //Get the URLs
        URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
 
        for(int i=0; i< urls.length; i++)
        {
            System.out.println(urls[i].getFile());
        }       
 
    }
} 

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
warturtleCommented:
Open a command prompt and type:


echo %classpath%

Open in new window

0
 
aman0711Author Commented:
Please correct me if I am wrong, i am doing all this RAD 7.0 , so running echo %classpath% on command prompt, wont fetch anything useful, right?

           
0
 
warturtleConnect With a Mentor Commented:
Yes, it might not have the correct values then. Because there was no IDE specified so I was under the impression that you are doing it in TextPad or something. Have a look at the below instructions then (got them from Rational Developer website):

 Adding a JAR file to the build path

You can add a JAR file stored either in the workbench or anywhere in your file system to the build class path.

To add a JAR to your build class path follow these steps:

   1. Select the project, and from its pop-up menu, select Properties. In the Properties dialog, select the Java Build Path page.
      Click the Libraries tab.
      You can now either add a JAR file which is contained in your workspace or which is somewhere else:
          * to add a JAR file which is inside your workspace click the Add JARs button
          * to add an external JAR file click the Add External JARs button
   2. In the dialog that appears, select the JAR file that you want to add. Note that you can add multiple JARs at once.
0
 
aman0711Author Commented:
Hi wasturtle,

                         Thanks for the quick response. I already did the steps you suggested me. both files are in classpath.

                         I have attached the snap shot of folder hierarchy.

project-folder.PNG
0
 
warturtleConnect With a Mentor Commented:
Change this in your code:

from: Class.forName("oracle.jdbc.driver.OracleDriver");

to: Class.forName("oracle.jdbc.OracleDriver");

Oracle has upgraded the interfaces after 9i, so that might be the reason that this is not working.

Try that and let me know, if it works.
0
 
aman0711Author Commented:
Hmmm..,
                    It didnt work. This time a different error.


 java.lang.ClassNotFoundException: oracle.jdbc.OracleDrive
    at java.lang.Class.forNameImpl(Native Method)
    at java.lang.Class.forName(Class.java:130)
    at com.si.keyNote.AgentAttributesReader$SaxHandler.startElement(AgentAttributesReader.java:51)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
0
 
warturtleCommented:
You are missing an alpabet 'r' at the end of the statement it seems. Because its a classnotfoundexception on oracle.jdbc.OracleDrive

Add r or copy and paste this:

Class.forName("oracle.jdbc.OracleDriver");




0
 
aman0711Author Commented:
Ohk, Sorry... stupid mistake :(

back to the same error:

java.sql.SQLException: No suitable driver
0
 
warturtleCommented:
Can you please tell me what version of Oracle are you using? 10g Release 1 or Release 2 and what is the JDK version number that you are using?
0
 
aman0711Author Commented:
Not sure, about  Release 1 or 2.
this is what i see in my tool

Oracle
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning and Data Mining options
Oracle JDBC driver
9.2.0.4.0

Current profile: generic
Current facade: oracle


and Jdk 1.4
0
 
warturtleConnect With a Mentor Commented:
I am going to suggest that you download the correct version of Oracle driver from the Oracle website, because the Oracle driver version is 9.2.0.4.0 and it seems to be valid only for Oracle 9i.

http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html

Alternatively, you can browse within the Oracle directory and look for the ojdbc14.jar inside it and use that in your program.
0
 
aman0711Author Commented:
I am sorry warturtle,
                                      I gave you enough trouble. Downloaded a fresh copy of ojdbc.jar and still getting the error :(

  java.sql.SQLException: No suitable driver
0
 
warturtleCommented:
Don't worry about that... I just saw that the url is "jdbc:oracle:thin:@abc.xyz.com:1522:omft01" is that the real url or you need to put the name of the server that you are trying to access??

Try that and let me know, how it goes.
0
 
aman0711Author Commented:
No I just have this url on EE.
I am using the correct one in my code. :(
0
 
warturtleCommented:
Hmm... the only thing that I can think of is the URL itself. Try this new format:

jdbc:oracle:thin://abc.xyz.com:1522/omft01

0
 
aman0711Author Commented:
wow ,, I am so sorry.. I kept you on this for such a silly mistake :-)

My apologies.

Thank you so much  
0
 
aman0711Author Commented:
oops, I didnt see it was you objects :-)

Sorry... lol
0
 
warturtleCommented:
Hahahaa.... that was good observation, objects.
0
 
aman0711Author Commented:
Thank you folks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.