Menu

ClassNotFoundException

Help
ultramods
2005-06-28
2012-08-15
  • ultramods

    ultramods - 2005-06-28

    I am trying to connect to sql server for the first time using the sourceforge driver but keept getting the error:ClassNotFoundException. I have set the class path in the system properties (windows 2003) as: Classpath=C:\db\src\db\jtds-1.1.jar;C:\db\build\classes\jtds-1.1.jar.

    I have set two classpaths as I am using netbeans which has created the direcotry structures: C:\db\src\ for .Java files and C:\db\build\classes for .class files, I didnt know which directory to place the jar file in so I placed it in both.

    I have also tried running the class file and setting the classpath from the command line, however i stil get the same error.

    Here is the code i have been using:

    import java.io.;
    import java.net.
    ;
    import java.sql.;
    import java.util.
    ;

    public class Main {

    public static void main(String args[]) {
    
        String url= "jdbc:jtds:sqlserver://213.0.218.0:1433/ultramodsLive;user=myUsername;password=myPassword";
    
                Connection con;
        String createString;
        createString = "select * from tblAuthor";
        Statement stmt;
    
        try {
            Class.forName ("net.sourceforge.jtds.jdbc.Driver");
    
        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException:"); 
            System.err.println(e.getMessage());
        }
    
        try {
    
            con = DriverManager.getConnection(url);
    
            stmt = con.createStatement();                           
                stmt.executeUpdate(createString);
                        ResultSet rs = stmt.executeQuery(createString);
    
            stmt.close();
            con.close();
    
        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }
    }
    

    }

    I also read in a previous forum post that the jar file should also be placed in the external lib for the jdk, is this true. I have placed it in C:\jdk\jdk\jre\lib\ext\jtds-1.1.jar, however I still get the same error.

    Any help would be greatly appreciated as I don't know what else to try.

     
    • Alin Sinpalean

      Alin Sinpalean - 2005-06-28

      When you run your app from the command line use something like:

      java -cp C:\db\src\db\jtds-1.1.jar Main

      To run it from NetBeans (or any other IDE) there's an IDE specific way of adding jar files to the classpath. I can't help you on that as I have no idea how it's done in NetBeans. And don't place the jar in the lib\ext folder; as you can see it doesn't always work and it may cause other issues.

      Alin.

       
    • ultramods

      ultramods - 2005-06-28

      Thank you that worked.

      For anyone with similar problem: add a classpath in Netbeans as follows:
      right click on project icon in project window (top left) then select properties. in new window select Libraries from 'categories' menu, then select ' compile' tab, then add jar file.

       
      • kostaspapas

        kostaspapas - 2006-03-31

        I have a problem you may know the solution. I use "Neatbeans 5.0" With "Sun Java System Application Server" I have already

        1) "add a classpath in Netbeans as follows:
        right click on project icon in project window (top left) then select properties. in new window select Libraries from 'categories' menu, then select ' compile' tab, then add jar file. "

        2) Put the jtds-1.2.jar in C:\Exercises\NetBeans\AppAskiseis\build\web\WEB-INF\lib
        on my PC

        3) I wrote this simple code in JSP

        <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
        <%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>

        <sql:setDataSource
        var ="something"
        driver="net.sourceforge.jtds.jdbc.Driver"
        url="jdbc:jtds:sqlserver://192.168.2.221/infoshop"
        user="clroot"
        password="clroot"
        />

        <sql:query var="test" dataSource="something">
        SELECT Code FROM Material
        </sql:query>

        <html>
        <head>
        <title>SQL Tag Out Examples</title>
        </head>

        <body>
        kostas ole
        </body>
        </html>

        AND I GET the error message
        "Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver""

        Do I have to do something else?

         
    • Paul Hastings

      Paul Hastings - 2005-07-12

      any ideas why eclipse won't pick up the jTDS.jar even after i added it as an external jar? won't compile, no matter where i place it.

      thanks.

       
      • Alin Sinpalean

        Alin Sinpalean - 2005-07-12

        That's very much an Eclipse issue (or rather an Eclipse configuration issue). I think you would be better off asking this question on an Eclipse forum rather than here (I'm not using Eclipse, for example, so I'm not familiar with it).

        Alin.

         
    • Kamal

      Kamal - 2005-12-30

      Hi,

      I have a Similar problem: but in my case it is ALMOST Working, but i am getting the exception:
      java.sql.SQLException: Network error IOException: Connection refused: connect
      at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:372)
      at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
      at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:178)
      at java.sql.DriverManager.getConnection(DriverManager.java:525)
      at java.sql.DriverManager.getConnection(DriverManager.java:171)
      at com.webmethods.jdbc.qa.Connect.getConnection(Connect.java:39)
      at com.webmethods.jdbc.qa.Connect.displayDbProperties(Connect.java:58)
      at com.webmethods.jdbc.qa.Connect.main(Connect.java:98)Error Trace in getConnection() : Network error IOException: Connection refused: connect
      Error: No active Connection

      Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
      at java.net.Socket.connect(Socket.java:507)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:289)
      at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:250)
      at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:297)
      ... 7 more

      My Source code is:

      public class Connect {
      private java.sql.Connection con = null;

      private final String url = &quot;jdbc:jtds:sqlserver://&quot;;
      
      private final String serverName = &quot;My IP Address&quot;;
      
      private final String portNumber = &quot;1433&quot;;
      
      private final String databaseName = &quot;test&quot;;
      
      private final String userName = &quot;My User Name&quot;;
      
      private final String password = &quot;My Password&quot;;
      
      // Informs the driver to use server a side-cursor,
      // which permits more than one active statement
      // on a connection.
      private final String selectMethod = &quot;cursor&quot;;
      
      // Constructor
      public Connect() {
      }
      
      private String getConnectionUrl() {
          return url + serverName + &quot;:&quot; + portNumber + &quot;;databaseName=&quot;
                  + databaseName + &quot;;selectMethod=&quot; + selectMethod + &quot;;&quot;;
      }
      
      private java.sql.Connection getConnection() {
          try {
              Class.forName(&quot;net.sourceforge.jtds.jdbc.Driver&quot;);
      
              con = java.sql.DriverManager.getConnection(getConnectionUrl(),
                      userName, password);
              if (con != null)
                  System.out.println(&quot;Connection Successful!&quot;);
          } catch (Exception e) {
              e.printStackTrace();
              System.out.println(&quot;Error Trace in getConnection() : &quot;
                      + e.getMessage());
          }
          return con;
      }
      
      /*
       * Display the driver properties, database details
       */
      
      public void displayDbProperties() {
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;
          try {
              con = this.getConnection();
              if (con != null) {
                  dm = con.getMetaData();
                  System.out.println(&quot;Driver Information&quot;);
                  System.out.println(&quot;\tDriver Name: &quot; + dm.getDriverName());
                  System.out
                          .println(&quot;\tDriver Version: &quot; + dm.getDriverVersion());
                  System.out.println(&quot;\nDatabase Information &quot;);
                  System.out.println(&quot;\tDatabase Name: &quot;
                          + dm.getDatabaseProductName());
                  System.out.println(&quot;\tDatabase Version: &quot;
                          + dm.getDatabaseProductVersion());
                  System.out.println(&quot;Avalilable Catalogs &quot;);
                  rs = dm.getCatalogs();
                  while (rs.next()) {
                      System.out.println(&quot;\tcatalog: &quot; + rs.getString(1));
                  }
                  rs.close();
                  rs = null;
                  closeConnection();
              } else
                  System.out.println(&quot;Error: No active Connection&quot;);
          } catch (Exception e) {
              e.printStackTrace();
          }
          dm = null;
      }
      
      private void closeConnection() {
          try {
              if (con != null)
                  con.close();
              con = null;
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      
      public static void main(String[] args) throws Exception {
          Connect myDbTest = new Connect();
          myDbTest.displayDbProperties();
      }
      

      }

      Can anyone help me ? i am running in eclipse, and the MSSQL Server is on Windows XP , on my desktop.
      Both the MSSQL Server and the JDBC Driver are running on the same machine (My Windows XP Desktop), do i need to do something to allow incoming connections to MSSQL Server ?

      Thanks in Advance.

      -Kamal.

       
      • Alin Sinpalean

        Alin Sinpalean - 2006-01-03

        Please read the FAQ ( http://jtds.sourceforge.net/faq.html ) before asking for help. And don't hijack someone else's thread, start a new one, especially if you have a different problem than the OP.

        Alin.

         
      • David D. Kilzer

        David D. Kilzer - 2006-01-03

        The local instance of SQL Server may only be binding to localhost or 127.0.0.1. Try using 127.0.0.1 instead of the IP address of your XP box to see if that works.

        BTW, the selectMethod parameter has NO EFFECT on jTDS. This RFE contains an old patch that has the effect of always enabling cursors at the cost of performance. It probably won't apply cleanly to the source in CVS, though.

        Make jTDS work with IBM WebSphere 4
        http://sourceforge.net/tracker/index.php?func=detail&aid=1201617&group_id=33291&atid=407764

        Dave

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.