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!

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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 <absolute path to sqljdbc4.jar>;. transfer on Windows
Just tried: java -cp C:\Program Files\......\enu\sqljdbc4.jar;. transfer ..still the same error. transfer class is containing the main method.
Ah! you need to make transfer a public class ... wonder, how I missed that earlier :)
Okay my bad. Finally got past the cannot find main class error! I was tried running java -cp <absolute path to sqljdbc4.jar>;.transfer instead of java -cp <absolute path to sqljdbc4.jar>;.<space>transfer.... monstrous disaster caused by the missing space! Thanks a ton Pat! Making the class "public" and adding main method with classpath did it!
|
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.

7 Comments

Not using eclipse. I tried that, got error: could not find or load main class file transfer
May be there is some configuration issue. JVM is not able to find your .class file(Transfer.class)
it compiles the program. no isues there. While trying to get it to run i get the error.
You got the same exception(Driver ClassNotFound)
Okay, nw the error is different. java.sql.SQLException: No suitable driver found for jdbc:......databaseName:DataDB
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.