Connectimg to MSSQL Server from a java class

java , sql server United States
  • 15 years ago

    HI,
    I have a problem with JDBC , where i want to connect to a MSSQL Server 2000 Database stored in my local computer via a simple java class.And i have the required driver too but have a problem how to do it, Here's the code i use and this gives the following error message...



    F:\j2sdk1.4.2_01\bin>javac Db.java


    F:\j2sdk1.4.2_01\bin>java Db
    Exception in thread "main" java.lang.NoClassDefFoundError: Db



    I have already set the classpath to all the three jar files of the driver but this was the result! pls help me.. if there's any tutorials for this matter pls send the link too.




    import java.sql.*;


    public class Db {
     public static void main(String[] args) {
       try {


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


        Connection connection = DriverManager.getConnection("jdbc:odbc:MyDB");
       if (connection != null) {
         System.out.println();
         System.out.println("Successfully connected");
         System.out.println();
         //String dbUrl = "jdbc:odbc:MyDB";


      }



     }catch(ClassNotFoundException e)
     {System.err.println(e);}
     catch (SQLException t)
     {
       System.err.println(t);}
     }
     }




    when i use the code
    "
    DriverManager.registerDriver(new   com.microsoft.jdbc.sqlserver.SQLServerDriver());"


    to get the connection it gave ....



    F:\j2sdk1.4.2_01\bin>javac Db.java
    Db.java:7: package com.microsoft.jdbc.sqlserver does not exist
         DriverManager.registerDriver(new   com.microsoft.jdbc.sqlserver.SQLServerD
    river());
                                                                        ^
    1 error

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie