I made a simple program in which will connect to ms sql...
then, after running the codes something is error!
can you help me to solve this out...

here is my code:

import java.sql.*;

public class connect_sql {

public static void main(String args[]) {
Connection con = null;

try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433","ben","");
if(!con.isClosed())
System.out.println("Successfully connected to MSSQL server...");

       } 
catch(Exception e) 
        {
System.err.println("Exception: " + e.getMessage());
         } 
finally {
try 
      {
if(con != null)
con.close();
      } 
catch(SQLException e) {}
           }
}
}

and here is the error msg:

Exception: com.microsoft.jdbc.sqlserver.SQLServerDriver

what should I do so that I can connect to sql...
can you pls help me here...
-tnx

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Add an e.printStackTrace() to your catch block and post the entire error message.

It's probably that you don't have the sqlserver driver jarfile on your classpath.

.,I tried to put that..then this error occurs

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at JCustomer.main(JCustomer.java:10)
Exception: com.microsoft.jdbc.sqlserver.SQLServerDriver

.,may I know whats the problem with this one...

Have you included this mysql-connector-java-5.0.7-bin.jar jar file?
If not use the above first and if it doesn't work try the below

I think that the problem is with value you use for:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
At first try to write: e.printStackTrace(); inside the catch(). You will get a better view of the exception. I think that the problem is with the driver you are using inside the Class.forName() method,
try this: com.mysql.jdbc.Driver.
and this: jdbc:mysql://localhost:1433/nameOfTheDB

..I am using ms sql server,then is it necessary to put these
mysql-connector-java-5.0.7-bin.jar jar...
I thought mssql is difference than mysql...
well, I tried it also but still it doesnt connect in the database...

No, not the mysql connector jarfile, the sqlserver connector jarfile. That error means that you don't have it included on your classpath. Add it to your classpath.

Have you included this mysql-connector-java-5.0.7-bin.jar jar file?

He's not using MySQL.

If not use the above first and if it doesn't work try the below

I think that the problem is with value you use for:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

That is correct for MS SQL Server.

At first try to write: e.printStackTrace(); inside the catch(). You will get a better view of the exception.

He just did that.

I think that the problem is with the driver you are using inside the Class.forName() method,

No, it's only that he doesn't have the driver on the classpath.

try this: com.mysql.jdbc.Driver.
and this: jdbc:mysql://localhost:1433/nameOfTheDB

That would work if he were using MySQL, and it were running on the port 1433.


Sorry, don't mean to be mean, but that entire post seems to be entirely offbase. ;)

He's not using MySQL.
That is correct for MS SQL Server.

He just did that.

No, it's only that he doesn't have the driver on the classpath.

That would work if he were using MySQL, and it were running on the port 1433.

Sorry, don't mean to be mean, but that entire post seems to be entirely offbase. ;)

I didn't mean that he should take exactly this: mysql-connector-java-5.0.7-bin.jar. This is the jar I am using. I meant that he should take whatever he has installed in his computer. As far the port I used what he was using in his mail.
And I told him first to use the jar file and then if it didn't work to change the driver.

As far as my post was being little off was because by the time it took me to write it, you guys have already posted two threads before I submit.

And I don't think you were mean. Your comments said what needed to be said and nothing more.

.,...I got it already guys!
I made some changes i my driver connection..!
so far it works now!!!
tnx for sharing mhe your knowledge guys!!
:)

This article has been dead for over six months. Start a new discussion instead.