Community discussion forum

Connectimg to MSSQL Server from a java class

  • 4 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("jdbcdbc:MyDB");
       if (connection != null) {
         System.out.println();
         System.out.println("Successfully connected");
         System.out.println();
         //String dbUrl = "jdbcdbc: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).

We'd love to hear what you think! Submit ideas or give us feedback