Hello, A BIG ERROR of my Programming Life i think. ANYYONE CAN HELP PLEASE ?
I am trying to do a Java & Database Connection through Netbeans 7.4. Everything is going fine except a Error since 2 hours :(
I googled each and tried everything including Classpath etc etc. and other threads on internet. When i create a new CONNECTION from services TAB and CLICK > TEST CONNECTION. CONNECTION WAS ESTABLISHED. When i build the project that was Okay. But
at Run there is an error.:
run:
Loading Driver...
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
Goodbye!
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at dbjavaconnection.DBJavaConnection.main(DBJavaConnection.java:25)
BUILD SUCCESSFUL (total time: 0 seconds)
I Am using MS SQL SERVER 2012. Driver Version 4.0. Os : WINDOWS 8. and the code is:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dbjavaconnection;
/**
*
* @author Abrar
*/
import java.sql.*;
public class DBJavaConnection {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Connection conn = null;
Statement stmt = null;
try{
System.out.println("Loading Driver...");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:sqlserver://LENOVO\\MSSQLSERVER;databaseName=studentdata");
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Student";
System.out.println("Executing statement...");
try (ResultSet rs = stmt.executeQuery(sql)) {
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
} }
stmt.close();
conn.close();
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}
}
Driver Path: C:\Program Files\sqljdbc_4.0\enu\sqljdbc4.jar