• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

No Suitable Driver found

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
gaugeta
Asked:
gaugeta
  • 14
  • 13
  • 11
  • +2
1 Solution
 
reijnemansCommented:
You have to make sure, the jar with the driver is on your classpath
0
 
CEHJCommented:
>>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
 
gaugetaAuthor Commented:
@reijnemans:How do i do that.
Please help...
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
gaugetaAuthor Commented:
@CEHJ:Its persent in the properties file.
I dont get it it was working perfectly before.
Please help...
0
 
CEHJCommented:
(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
 
reijnemansCommented:
0
 
CEHJCommented:
What db is it?
0
 
reijnemansCommented:
Could you also post the other lines that are show when running the application? The DriverManager gives more information :)
0
 
for_yanCommented:


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
 
for_yanCommented:

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

Somethig like that:

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

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
 
reijnemansCommented:
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
 
for_yanCommented:
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
 
gaugetaAuthor Commented:
@CEHJ:Its sybase database.

@for_yan:i never called Class.forName() method for this to work.
Please help....
0
 
for_yanCommented:
Are you running it in IDE or from command line?
Were you running it in the same way before?
0
 
gaugetaAuthor Commented:
@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
 
gaugetaAuthor Commented:
@for_yan:I run it in command line.
I used to run the same way before .
Please help...
0
 
CEHJCommented:
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
 
for_yanCommented:
type echo %CLASSPATH%
and post the classpath here
0
 
for_yanCommented:
Do you knw the name of the jar with your JDBC driver?

Is it jConnect or something?
0
 
for_yanCommented:

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
 
reijnemansCommented:
if you set the classpath as described in the link I posted, make sure you have restarted command-prompt
0
 
for_yanCommented:
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
 
CEHJCommented:
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
 
CEHJCommented:
... except that it won't show what's included from java.ext.dirs, which also needs to be checked
0
 
gaugetaAuthor Commented:
@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
 
reijnemansCommented:
Seems to be the "driver" jar that is not on the classpath
0
 
for_yanCommented:
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
 
reijnemansCommented:
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
 
gaugetaAuthor Commented:
@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
 
for_yanCommented:
So are you running it as a web application or directly from command line?
0
 
ksivananthCommented:
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
 
CEHJCommented:
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
 
reijnemansCommented:
could you post the command which you use to start de application?
0
 
reijnemansCommented:
You should have somewhere a file named like "JConnectXX.jar" that should be on the classpath
0
 
gaugetaAuthor Commented:
@CEHJ:How do i know which is the driver jar.
Please help...
0
 
reijnemansCommented:
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
 
for_yanCommented:
So how do you start your applicaion?
0
 
gaugetaAuthor Commented:
@reijnemans:I dont have JconnectXX.jar but have jconn2.jar and jtds2.jsr.
Please help...
0
 
reijnemansCommented:
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
 
gaugetaAuthor Commented:
@reijnemans:To start application i dont execute any command.
I just go the browzer and type the application url.
Please help...
0
 
reijnemansCommented:
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
 
for_yanCommented:
Are you the one who starts And manages your application server?
0
 
for_yanCommented:
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
 
CEHJCommented:
>>
@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
 
gaugetaAuthor Commented:
@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
 
CEHJCommented:
IOW your classpaths were wrong - you moved the application
0
 
CEHJCommented:
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
 
reijnemansCommented:
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
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 14
  • 13
  • 11
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now