Reputation: 143
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!
Upvotes: 2
Views: 5841
Reputation: 608
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
Upvotes: 0
Reputation: 2257
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.
Upvotes: 1
Reputation: 64
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.
Upvotes: 1