Link to home
Start Free TrialLog in
Avatar of aman0711
aman0711

asked on

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

Avatar of warturtle
warturtle
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you able to print your classpath here, so that I can see what it is?
Avatar of aman0711
aman0711

ASKER

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

Open a command prompt and type:


echo %classpath%

Open in new window

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?

           
SOLUTION
Avatar of warturtle
warturtle
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
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");




Ohk, Sorry... stupid mistake :(

back to the same error:

java.sql.SQLException: No suitable driver
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?
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
No I just have this url on EE.
I am using the correct one in my code. :(
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

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wow ,, I am so sorry.. I kept you on this for such a silly mistake :-)

My apologies.

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

Sorry... lol
Hahahaa.... that was good observation, objects.
Thank you folks :)