Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tolls

Thread: Need help to connect my java application to sql server 2005

  1. #1
    Amy1177 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Need help to connect my java application to sql server 2005

    Dear all,

    I am trying to connect my java application to sql server 2005
    I am running the application on eclipse,and also I have added specified sqljdbc.jar file
    but still it is giving this error : SQLException: No suitable driver found for jdbc

    Here is my code. Can someone please help?

    import java.sql.*;
    public class conection
    {

    public static void main(String a[]) throws ClassNotFoundException, SQLException
    {

    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:8080/DatabaseName=user" ,"ab","***");
    Statement st=con.createStatement();
    String sql="select * from user1";
    ResultSet rs=st.executeQuery(sql);
    while(rs.next())
    {
    System.out.println("Name: "+rs.getString(1));
    System.out.println("Address : "+rs.getString(2));
    }
    if(st!=null)
    st.close();
    if(con!=null)
    con.close();

    }
    catch(SQLException sqle)
    {
    System.out.println("Sql exception "+sqle);
    }
    }

    // TODO Auto-generated method stub

    }



    Error:
    Sql exception java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:8080/DatabaseName=user

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Need help to connect my java application to sql server 2005

    Java Code:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    You should be loading the sqlserver driver, not the JDBC/ODBC bridge.

  3. #3
    Amy1177 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help to connect my java application to sql server 2005

    @Tolls: Thank you for looking into it.
    I tried to use sqlserver driver as suggested

    Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver").

    But now it is giving an error as :

    java.lang.ClassNotFoundException: com.sqlserver.jdbc.SQLServerDriver


    So any suggestions for this please?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Need help to connect my java application to sql server 2005

    Is the class (or the jar containing it) on the classpath?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Need help to connect my java application to sql server 2005

    And I take it one of those is a typo on your part, as the error does not match the code:

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

    java.lang.ClassNotFoundException: com.sqlserver.jdbc.SQLServerDriver
    DarrylBurke likes this.

  6. #6
    Amy1177 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help to connect my java application to sql server 2005

    Thank you for your inputs, somehow it is now working fine, however I have a new issue now.
    The code mentioned below works perfectly fine for the java page throwing no exceptions but for jsp it is throwing java.lang.ClassNotFoundException.
    Any help/suggestions please?


    <html><head></head> <body> <%@ page import="java.sql.*" %> <%try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver"); %> <% Connection conn = DriverManager.getConnection("jdbc:sqlserver://"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from IIP"); while(rs.next()) { System.out.println("ID:"+rs.getInt(1)); System.out.println("SEQNO:"+rs.getInt(2)); System.out.println("Client:"+rs.getString(3)); System.out.println("ClientError:"+rs.getString(4)) ; }} catch(Exception e) {System.out.println(e);} %> </body> </html> Output : java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Need help to connect my java application to sql server 2005

    That code is unreadable.
    My first suggestion is to use [code] tags [/code] and ensure the formatting is reasonable.

    My second would be that the driver jar file is not in the WEB-INF/lib directory where it should be.

  8. #8
    Amy1177 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help to connect my java application to sql server 2005

    I tried your suggestion but it is still throwing the same error:

    java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

    Here is the code again:

    <%@ page import="java.sql.*" %>
    <%
    try
    {
    System.out.println("In try");
    Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");
    System.out.println("After Class.forname");
    String url = "jdbc:sqlserver://localhost;databaseName=cab";
    Connection con = DriverManager.getConnection(url);
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT *FROM bm");

    System.out.println("Connected");

    while(rs.next())
    {
    System.out.println(rs.getString("CLIENT"));
    }
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
    //,"sa","SQL1423#3"
    %>


    output :

    In try
    java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver


    Am I doing something wrong?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Need help to connect my java application to sql server 2005

    Yes.
    Your driver jar is not in the WEB-INF/lib directory of your server.
    If it's a Tomcat server that would be:
    <tomcat-installation-dir>/webapps/<your-app>/WEB-INF/lib

    A similar situation applies to other Java web-app servers.

  10. #10
    Amy1177 is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help to connect my java application to sql server 2005

    Yes it worked ...thnx a ton :)

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Need help to connect my java application to sql server 2005

    No problem.

Similar Threads

  1. Replies: 0
    Last Post: 09-20-2011, 09:29 AM
  2. Replies: 3
    Last Post: 12-13-2010, 01:42 PM
  3. Replies: 4
    Last Post: 09-07-2010, 06:51 AM
  4. Replies: 3
    Last Post: 03-28-2010, 06:55 AM
  5. Connect to SQL server 2005
    By comp in forum JDBC
    Replies: 1
    Last Post: 03-25-2009, 11:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •