Solved

No suitable driver error

Posted on 2009-05-04
22
942 Views
Last Modified: 2013-12-29
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

0
Comment
Question by:aman0711
  • 11
  • 10
22 Comments
 
LVL 16

Expert Comment

by:warturtle
Comment Utility
Are you able to print your classpath here, so that I can see what it is?
0
 
LVL 10

Author Comment

by:aman0711
Comment Utility
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
 
LVL 16

Expert Comment

by:warturtle
Comment Utility
Open a command prompt and type:


echo %classpath%

Open in new window

0
 
LVL 10

Author Comment

by:aman0711
Comment Utility
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
 
LVL 16

Assisted Solution

by:warturtle
warturtle earned 250 total points
Comment Utility
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
 
LVL 10

Author Comment

by:aman0711
Comment Utility
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
 
LVL 16

Assisted Solution

by:warturtle
warturtle earned 250 total points
Comment Utility
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
 
LVL 10

Author Comment

by:aman0711
Comment Utility
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
 
LVL 16

Expert Comment

by:warturtle
Comment Utility
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
 
LVL 10

Author Comment

by:aman0711
Comment Utility
Ohk, Sorry... stupid mistake :(

back to the same error:

java.sql.SQLException: No suitable driver
0
 
LVL 16

Expert Comment

by:warturtle
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 10

Author Comment

by:aman0711
Comment Utility
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
 
LVL 16

Assisted Solution

by:warturtle
warturtle earned 250 total points
Comment Utility
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
 
LVL 10

Author Comment

by:aman0711
Comment Utility
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
 
LVL 16

Expert Comment

by:warturtle
Comment Utility
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
 
LVL 10

Author Comment

by:aman0711
Comment Utility
No I just have this url on EE.
I am using the correct one in my code. :(
0
 
LVL 16

Expert Comment

by:warturtle
Comment Utility
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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
Comment Utility
>                         conn = DriverManager.getConnection("url","user","pwd");

should be:

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

0
 
LVL 10

Author Comment

by:aman0711
Comment Utility
wow ,, I am so sorry.. I kept you on this for such a silly mistake :-)

My apologies.

Thank you so much  
0
 
LVL 10

Author Comment

by:aman0711
Comment Utility
oops, I didnt see it was you objects :-)

Sorry... lol
0
 
LVL 16

Expert Comment

by:warturtle
Comment Utility
Hahahaa.... that was good observation, objects.
0
 
LVL 10

Author Closing Comment

by:aman0711
Comment Utility
Thank you folks :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now