Solved

No Suitable Driver found

Posted on 2011-09-22
50
307 Views
Last Modified: 2012-06-21
Hi Experts...
I was using a properties file to get a connection previously and it worked fine.
But suddenly i am getting the SQLException No Suitable Driver found exception.
The below java program was working perfectly before.
Please help....
import java.sql.*;
import java.io.*;
import java.util.*;

 /**
   This program tests that the database and the JDBC
    driver are correctly configured.
*/
class TestDB
{
    public static void main (String args[])
    {
       try
       {
          runTest();
       }
       catch (SQLException ex)
      {
          while (ex != null)
          {
             ex.printStackTrace();
             ex = ex.getNextException();
          }
       }
       catch (IOException ex)
       {
          ex.printStackTrace();
       }
    }

    /**
       Runs a test by creating a table, adding a value, showing the table contents, and
       removing the table.
    */
    public static void runTest()
       throws SQLException, IOException
    {
       Connection conn = getConnection();
       try
       {
          Statement stat = conn.createStatement();

          stat.execute("CREATE TABLE Greetings1 (Message CHAR(20))");
          stat.executeUpdate("INSERT INTO Greetings1  VALUES ('Hello, World!')");
          ResultSet result = stat.executeQuery("SELECT * FROM Greetings1 ");
          result.next();
          System.out.println(result.getString(1));
          stat.execute("DROP TABLE Greetings1");
       }
       finally
       {
          conn.close();
       }
    }

    /**
       Gets a connection from the properties specified
       in the file database.properties
       @return the database connection
    */
    public static Connection getConnection()
       throws SQLException, IOException
    {
       Properties props = new Properties();
       FileInputStream in = new FileInputStream("system.properties");
       props.load(in);
       in.close();

       String drivers = props.getProperty("dbcb.jdbcDriver");
       if (drivers != null)
          System.setProperty("jdbc.drivers", drivers);
       String url = props.getProperty("dbcb.jdbcURL");
       String username = props.getProperty("dbcb.dbUser");
       String password = props.getProperty("dbcb.dbPassword");

       return DriverManager.getConnection(url, username, password);
    }
}

Open in new window

0
Comment
Question by:gaugeta
  • 14
  • 13
  • 11
  • +2
50 Comments
 
LVL 4

Expert Comment

by:reijnemans
ID: 36578967
You have to make sure, the jar with the driver is on your classpath
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579016
>>String drivers = props.getProperty("dbcb.jdbcDriver");

If that is missing in your properties file, it will never be set. Check that it's present in the file
0
 

Author Comment

by:gaugeta
ID: 36579020
@reijnemans:How do i do that.
Please help...
0
 

Author Comment

by:gaugeta
ID: 36579032
@CEHJ:Its persent in the properties file.
I dont get it it was working perfectly before.
Please help...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579040
(Of course, if it's not set by your code, it will have to be already set in the system properties for the runtime, so check that too - hint - debug/System.out.println it)
0
 
LVL 4

Accepted Solution

by:
reijnemans earned 500 total points
ID: 36579044
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579049
What db is it?
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579061
Could you also post the other lines that are show when running the application? The DriverManager gives more information :)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579067


Try to remember if you changed anything in the code or in the project  - it cannot
stop working just by itself without anything changed
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579074

Should not you call Class.forName() before
Driver.grtConnection...

Somethig like that:

Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579088

Try to remember if you changed anything in the code or in the project  - it cannot
stop working just by itself without anything changed

It could be that the application run on another computer, where the classpath is not set. But there is nothing change in the code or project.
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579106
Should not you call Class.forName() before
Driver.grtConnection...

Somethig like that:

Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");

This is done by the DriverManger.Getconnection();
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579108
But that's what changed in the project - it was run on another computer and with different classpath - if that was the case.
The question says "suddenly" it stopped working
0
 

Author Comment

by:gaugeta
ID: 36579119
@CEHJ:Its sybase database.

@for_yan:i never called Class.forName() method for this to work.
Please help....
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579128
Are you running it in IDE or from command line?
Were you running it in the same way before?
0
 

Author Comment

by:gaugeta
ID: 36579133
@all:The exception details are given below.
Please help...
java TestDB
java.sql.SQLException: No suitable driver
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:186)
        at TestDB.getConnection(TestDB.java:76)
        at TestDB.runTest(TestDB.java:38)
        at TestDB.main(TestDB.java:15)

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36579137
@for_yan:I run it in command line.
I used to run the same way before .
Please help...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579143
First of all, i would attempt to connect to it OUTSIDE java (command line tools) using the same credentials as in your app. Let me know if you ca do that successfully
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579145
type echo %CLASSPATH%
and post the classpath here
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579149
Do you knw the name of the jar with your JDBC driver?

Is it jConnect or something?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579157

you need to check that the jar with the driver is in the classpat and the folder with this jar is accessible (say if ot was on mapped drive, which is now disconnected)

0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579167
if you set the classpath as described in the link I posted, make sure you have restarted command-prompt
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579171
check the CLASSPATH right at the same dos prompt from where you are running your application

Do you know how to paste stuff from dos command line, so that we could see the classpath
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579179
The way to check the classpath is to place the following as the first line of main
System.out.println(System.getProperty("java.class.path"));

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579189
... except that it won't show what's included from java.ext.dirs, which also needs to be checked
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:gaugeta
ID: 36579203
@for_yan:This is the output got from echoing the classpath.
Please help...
/dun/d1s0/webapps/WEB-INF/classes/com/ora/jsp/sql:/sigma/app_code/classes:/dun/d1sig0/webapps/WEB-INF/classes:/dun/d1s0/webapps/WEB-INF/classes/CsvToTRFCreation:/dun/d1sig0/webapps/WEB-INF/lib/jcommon-1.0.16.jar:/dun/d1s0/webapps/WEB-INF/lib/jfreechart-1.0.13.jar

Open in new window

0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579209
Seems to be the "driver" jar that is not on the classpath
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579214
so why is there WEB-INF - is it a WEB application?
How do you run this app?

Do you run it with

java className

command?
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579222
so why is there WEB-INF - is it a WEB application?
How do you run this app?

Do you run it with

java className

command?

and what is the value of the "dbcb.jdbcDriver" -property.
0
 

Author Comment

by:gaugeta
ID: 36579283
@reijnemans:The value is dbcb.jdbcDriver=com.sybase.jdbc2.jdbc.SybDriver.
But my web-application is able to perform database operations.I dont get why.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579294
So are you running it as a web application or directly from command line?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 36579299
seems like classpath issue mentioned by @reijnemans, possibilities

1. either the folder containing driver class is cleared
2. or classpath changed omitting the driver jar!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579300
Put the driver jar into WEB-INF/lib if it's a webapp. Otherwise put the full path to the jar, including the jar file itself, in the classpath
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579305
could you post the command which you use to start de application?
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579310
You should have somewhere a file named like "JConnectXX.jar" that should be on the classpath
0
 

Author Comment

by:gaugeta
ID: 36579322
@CEHJ:How do i know which is the driver jar.
Please help...
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579328
quote:
@CEHJ:How do i know which is the driver jar.
Please help...

See my previous post the driver jar should be named as JConnectXX, but this is probably already in the WEB-INF/lib as the you said the web appilication could make a connection with the DB?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579334
So how do you start your applicaion?
0
 

Author Comment

by:gaugeta
ID: 36579339
@reijnemans:I dont have JconnectXX.jar but have jconn2.jar and jtds2.jsr.
Please help...
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579350
Probably this are the libaries that are use to start the application.

Please tell us how you start the application, which command do you execute, this is crucial to found a solution for your problem?
0
 

Author Comment

by:gaugeta
ID: 36579359
@reijnemans:To start application i dont execute any command.
I just go the browzer and type the application url.
Please help...
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36579362
So it's the web app :)

Could you copy jconn2.jar and jtds2.jsr in the WEB-INF/lib dir and restart you application/server
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579363
Are you the one who starts And manages your application server?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36579369
If your server is managed by other oeople you shoild go to thgem and show your error - they changed something on the sevre side and that's why it does not work
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579422
>>
@CEHJ:How do i know which is the driver jar.
Please help...
>>

Check out the docs for your db as regards jdbc. Get the latest jar(s) and put them in WEB-INF/lib as i mentioned earlier
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579437
0
 

Author Comment

by:gaugeta
ID: 36579494
@all:Its totally wierd but i solved the problem.
I just shifted the java program into another location which is somewhere inside WEB-INF and all was resolved.
But if i set all classpaths to the old location from which i was previously running the java application i still get No Suitable Driver message.
Please help...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579499
IOW your classpaths were wrong - you moved the application
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36579512
To tell you exactly what went wrong, i'd need to see the directory listing of the tree that encompasses the old location AND the new one. e.g. (attach 'listing.txt' to your post)
find /foo/bar/parent_directory_of_both | tee /tmp/listing.txt

Open in new window

0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36586069
Hi gaugeto,

I suppose your problem is solved or is there still a problem with connection (because this topic is still open)?

regards,

reijnemans
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 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

19 Experts available now in Live!

Get 1:1 Help Now