Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use the getConnection() method?

 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am connecting to an SQL Server Database using JDBC Driver. I am using the following statements.

Connection con = null;
try
{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch (ClassNotFoundException cf)
{
System.out.println(cf.getMessage());
}

try
{
con = DriverManager.getConnectioncon = DriverManager.getConnection("jdbc dbc://localhost:1433/CCIntegration","CC","CC");
}
catch (SQLException sq)
{
System.out.println(sq.getStackTrace());
System.out.println(sq.getMessage());
}

Here I am concerend about the database URL part. My server is in the local machine and my database name is 'CCIntegration'. But when I run the above statement in the program, it says 'No suitable driver'. Can someone please help me with this basic thing?
Thank you in advance
-JEAN
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"No suitable driver" ALWAYS means that none of the drivers that have been loaded (so far) recognize the DB URL as a URL they know how to process. therefore, getting this message ALWAYS means either:
  • the driver you think is loaded isn't really
  • the URL you think should work for your driver doesn't really

  • In your case, it's option number 2, your DB URL is wrong.

    Here's an example for the SQL Server 2000 driver:
    http://support.microsoft.com/default.aspx?scid=kb;en-us;313100

    Microsoft significantly changed the DB URL format for the SQL Server 2005 driver, see:
    http://msdn2.microsoft.com/en-us/library/ms378672.aspx
     
    Jean Fore
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Stu,
    Thanks a lot for the response. I went to the site you gave me and tried whatever they have specified. (I am using SQLServer 2000). Even before using the getConnection method I am getting the exception. That means I am getting the exception in the class load. The try catch block for the
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
    throws the following exception.
    -------------------------------------------------------------------------
    java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:141)
    at UBDAO.getConnection(UBDAO.java:48)
    at UBDAO.main(UBDAO.java:39)
    Error Trace in getConnection() : com.microsoft.jdbc.sqlserver.SQLServerDriver
    -------------------------------------------------------------------

    Also I have set the class path variable properly. Do you have any idea what am I lacking in this ?
    Thanks
    -JEAN
    [ May 19, 2006: Message edited by: Jean Fore ]
     
    Anwar Mian
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello Jean:

    I had the same issue that you had. I tried the CLASSPATH way without any success. I'm using Websphere and the following is how it can be resolved.

    1. Right click the project to select the 'Properties'
    2. Highlight "Java Build Path"
    3. Select the "Labraries" tab
    4. Click on "Add External JARs"

    Then Add the following JAR files from where they are installed:

    msbase.jar
    mssqlserver.jar
    msutil.jar

    Give it a try and it should work. The JVM is supposed to find the location of the JAR files from "CLASSPATH" system variable and load them but I guess in Websphere id does not do it that way.

    Good Luck!

    Anwar
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic