Solved

No suitable driver error

Posted on 2009-05-04
22
949 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
ID: 24297421
Are you able to print your classpath here, so that I can see what it is?
0
 
LVL 10

Author Comment

by:aman0711
ID: 24297491
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
ID: 24297616
Open a command prompt and type:


echo %classpath%

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Author Comment

by:aman0711
ID: 24297640
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
ID: 24297724
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
ID: 24297818
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
ID: 24298237
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
ID: 24298580
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
ID: 24298614
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
ID: 24298628
Ohk, Sorry... stupid mistake :(

back to the same error:

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

Expert Comment

by:warturtle
ID: 24298654
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
 
LVL 10

Author Comment

by:aman0711
ID: 24298671
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
ID: 24298768
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
ID: 24298890
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
ID: 24299029
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
ID: 24299042
No I just have this url on EE.
I am using the correct one in my code. :(
0
 
LVL 16

Expert Comment

by:warturtle
ID: 24299140
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
ID: 24299450
>                         conn = DriverManager.getConnection("url","user","pwd");

should be:

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

0
 
LVL 10

Author Comment

by:aman0711
ID: 24299652
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
ID: 24299655
oops, I didnt see it was you objects :-)

Sorry... lol
0
 
LVL 16

Expert Comment

by:warturtle
ID: 24299657
Hahahaa.... that was good observation, objects.
0
 
LVL 10

Author Closing Comment

by:aman0711
ID: 31577654
Thank you folks :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 91
starter POM and spring-boot-starter,  spring-boot-web 2 56
Spring Framework HTTPSession management 1 36
map related example 6 49
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

810 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