Click here to Skip to main content
15,885,948 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While connecting with SQL server database which is located at server computer from local computer, I am getting ClassNotFoundException. I wrote following code to connect with SQL server in java:-
Java
import java.sql.*;

public class ConnectDatabase {

Connection dbConnection = null;

String dbName = "Radiocity_Central";
String serverip="192.168.69.189";
String serverport="1433";
String url = "jdbc:sqlserver://192.168.69.189:1433;instance=MSRS11.MSSQLSERVER;Da tabaseName=Radiocity_Central;integratedSecurity=tr ue";
String userName = "sa"; 
String password = "airwaves";
final String driverName = "com.microsoft.sqlserver.jdbc.Radiocity_Centra l";
Statement statement = null;
ResultSet rs = null;
int updateQuery = 0;

public Connection getConnection() {

System.out.println(url);
try{
Class.forName(driverName).newInstance();

dbConnection = DriverManager.getConnection(url,userName,password) ;
System.out.println(DriverManager.getDrivers());

statement = dbConnection.createStatement();

/*String QueryString = "select Id from openview.dbo.OV_MS_Message where OriginalServiceId like '{FaultDn[1]}'";

updateQuery = statement.executeUpdate(QueryString);

if(updateQuery!=0){
System.out.println("success" + updateQuery);
}*/
statement.close();
dbConnection.close();
}
catch (ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return dbConnection;

}

public static void main(String[] args) {

ConnectDatabase cDB = new ConnectDatabase();
cDB.getConnection();

}

}

The runtime exception which I am getting is given below:-
jdbc:sqlserver://192.168.69.189:1433;instance=MSRS11.MSSQLSERVER;Da tabaseName=Ra
diocity_Central;integratedSecurity=true
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.Radiocity_Central

at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at ConnectDatabase.getConnection(ConnectDatabase.java :22)
at ConnectDatabase.main(ConnectDatabase.java:52)


Please help me to solve this problem.
Posted
Updated 29-Sep-13 2:04am
v2
Comments
Shubhashish_Mandal 30-Sep-13 2:56am    
did you add the proper jar so that proper driver class (com.microsoft.sqlserver.jdbc.Radiocity_Central) can be loaded.
Sushil 07101990 2-Oct-13 7:22am    
How to add it & what is the use of same?
Shubhashish_Mandal 3-Oct-13 4:33am    
Java provides you abstraction to use different vendor specific driver to connect the database. So java leave the responsibility to the vendor how they want to connect. So each vendor has different implementation for each driver. In case of you , choose Microsoft JDBC Driver for Sql Server to connect the DB. And for that purpose you have to provide that driver class explicitly to java. That is the reason you have to add the proper vendor specific jar.

You have to add this jar into your project class path.

Get rid of the line
Java
Class.forName(driverName).newInstance();

It does nothing but throw that exception.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Sushil 07101990 3-Oct-13 5:39am    
After Commenting Class.forName(driverName).newInstance() I am getting following Runtime Exception:-

jdbc:sqlserver://192.168.69.189:1433;instance=MSRS11.MSSQLSERVER;DatabaseName=Ra
diocity_Central;integratedSecurity=true
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://192.168.69.
189:1433;instance=MSRS11.MSSQLSERVER;DatabaseName=Radiocity_Central;integratedSe
curity=true
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at ConnectDatabase.getConnection(ConnectDatabase.java:23)
at ConnectDatabase.main(ConnectDatabase.java:51)

What to do now ?
Fredrik Bornander 3-Oct-13 5:43am    
You need to make sure you include the .jar file containing the jdbc drivers when running you application.
Sushil 07101990 3-Oct-13 12:52pm    
I don't know how to implement the same. Can you explain the same shortly ?
Fredrik Bornander 4-Oct-13 2:41am    
Read this; http://technet.microsoft.com/en-us/library/ms378956.aspx
Since I have not created dsn in my local computer that is why it showing this problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900