2

I'm fairly new to database management. I'm just trying to connect to the database and retrieve and display a table in the command prompt. The database is not on my computer. I am fairly certain that the url is the problem. The code:

import java.io.*;
import java.sql.*;

class transfer{

//driver and DB URLs
final static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
final static String DB_SQL = "jdbc:microsoft:sqlserver://localhost:1433;" + "database=DataDB;" + "user=sa;" + "password=1234";

//Database Username and password
final static String user1 = "sa";
final static String pass1 = "1234";

static ResultSet rs;        
public static void main(String args[]) throws SQLException, ClassNotFoundException{

    Connection conn_sql = null;
    Statement stmt_sql = null;
    //Statement stmt_ora = null;

//Register JDBC driver      
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Open Connection   
    System.out.println("Connecting to SQL database...");
    conn_sql = DriverManager.getConnection(DB_SQL, user1, pass1);

//Execute Query
    String sql_query;

    System.out.println("Creating statement for SQL...");
    stmt_sql = conn_sql.createStatement();
    sql_query = "Select * from attendancesummary";
    rs = stmt_sql.executeQuery(sql_query);
    System.out.println("SQL table details");

    System.out.println("Creating statement for SQL...");

    while(rs.next()){
    //Retrieve data
        int cno = rs.getInt("CardNo");
        int in_time = rs.getInt("entry");
        int out_time = rs.getInt("Exittm");
        String name = rs.getString("Name"); 
        int date = rs.getInt("TrDate");

    //Display data
        System.out.print("Employee ID: "+cno);  
        System.out.print("\tName: "+name);
        System.out.print("\tDate:"+date);
        System.out.print("\tEntry: "+in_time);
        System.out.print("\tExit: "+out_time);
    }       

}

}

The database name is DataDB and the table that I want to retrieve and display is attendancesummary. I have set my path as "C:\Program Files\Java\jdk1.8.0_11\bin";"C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar"

The code compiles fine.. but when I run it, I get the following error:

Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at java.net.URLClassLoader$1.run at java.net.URLClassLoader$1.run at java.security.AccessController.doPrivileged at java.net.URLClassLoader.findClass at java.lang.ClassLoader.loadClass at sun.misc.Launcher$AppClassLoader.loadClass at java.lang.ClassLoader.loadClass at java.lang.Class.forname0 at java.lang.Class.forname at transfer.main

I am truly lost. Any help would be appreciated!

Pribhat
  • 143
  • 1
  • 6
  • possible duplicate of [ClassNotFoundException when connecting to MS SQLServer using JDBC](http://stackoverflow.com/questions/9407986/classnotfoundexception-when-connecting-to-ms-sqlserver-using-jdbc) And your JDBC url is wrong, you might want to read [Building the Connection URL](http://msdn.microsoft.com/en-us/library/ms378428%28v=sql.110%29.aspx) – Mark Rotteveel Jul 26 '14 at 07:22

3 Answers3

1

Add sqljdbc4.jar in your classpath.

If you are using Eclipse then you can right click on project -> properties -> java build path. Go to libraries and click on add external jar. Then add the jdbc driver jar.

Hope this helps.

hcl
  • 64
  • 5
  • Not using eclipse. I tried that, got error: could not find or load main class file transfer – Pribhat Jul 25 '14 at 11:47
  • May be there is some configuration issue. JVM is not able to find your .class file(Transfer.class) – hcl Jul 25 '14 at 11:54
  • it compiles the program. no isues there. While trying to get it to run i get the error. – Pribhat Jul 25 '14 at 11:55
  • You got the same exception(Driver ClassNotFound) – hcl Jul 25 '14 at 12:02
  • Okay, nw the error is different. java.sql.SQLException: No suitable driver found for jdbc:......databaseName:DataDB – Pribhat Jul 25 '14 at 12:08
  • You said that database in not on your computer. Then you need to give IP address of the computer and port no where DB is located. DB_SQL = "jdbc:microsoft:sqlserver://IP_ADDRESS:1433;" + "database=DataDB;" + "user=sa;" + "password=1234"; – hcl Jul 25 '14 at 12:14
  • I am trying that only now. conn_sql = DriverManager.getConnection("jdbc:microsoft:sqlserver://X.X.X.X:1433; database=DataDB;"+"user=sa;"+"password=1234");.......still the same error. It does print the SOP : Creating statement for SQL... – Pribhat Jul 25 '14 at 12:23
1

It means that sqljdbc4.jar is missing from your CLASSPATH while running the code. If you are running this from command line then add the path to sqljdbc4.jar in -cp switch of java command.

If you are running from eclipse, then add sqljdbc4.jar in your build path.

Pat
  • 2,223
  • 4
  • 19
  • 25
  • I tried: java -cp -sqljdbc4.jar transfer.. got the following error Error: Could not find or load main class file transfer Also, i am running this from cmd prompt. – Pribhat Jul 25 '14 at 11:45
  • You need to add your class with main method as well in the CLASSPATH. For example, if you are currently running from the same directory as your class then use `java -cp ;. transfer` on Windows – Pat Jul 25 '14 at 11:48
  • Just tried: java -cp C:\Program Files\......\enu\sqljdbc4.jar;. transfer ..still the same error. transfer class is containing the main method. – Pribhat Jul 25 '14 at 11:50
  • Ah! you need to make `transfer` a `public class` ... wonder, how I missed that earlier :) – Pat Jul 25 '14 at 15:33
  • I made transfer as public. Tried to run: java -cp ;.transfer....... I get: Error: Could not find or load main class Files\Microsoft. Know what this means?? – Pribhat Jul 28 '14 at 04:59
  • 1
    Okay my bad. Finally got past the cannot find main class error! I was tried running java -cp ;.transfer instead of java -cp ;.transfer.... monstrous disaster caused by the missing space! Thanks a ton Pat! Making the class "public" and adding main method with classpath did it! – Pribhat Jul 28 '14 at 05:23
0

You have to add the JDBC driver to your project class path: example if you are using Eclipse put the jar in the 'lib' folder

nourhero
  • 568
  • 5
  • 12