Link to home
Start Free TrialLog in
Avatar of chanbrothers
chanbrothers

asked on

Java Connection to MSSQL

How do I connect to MSSQL database in java??
Avatar of msterjev
msterjev

ASKER CERTIFIED SOLUTION
Avatar of msterjev
msterjev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
JdbcOdbc Driver is SLOOOOOWWWWWWWWWWWWWW....

Use the driver that MYSQL Provides...

http://www.mysql.com/downloads/api-jdbc-stable.html

I believe they have examples of how to use them on the site.
First you need a driver, the mysql driver is good.
Put the jdbc driver (a .jar file) in this directory: javahomepath/jre/lib/ext/

(When you run your code you may run into some problems, how you deal with them depends on if you are writing an applet or an app.)
If you are writing an app you may have to configure your classpath. Go to startmenu->run and type:
 java -classpath ./<drivername>.jar;.<App.name>.

If you are running an applet you don't have to set up the classpath, but you still have to make sure that the java plug-in is pointing to the jre where your .jar file is located by opening the java plug-in admin program from the control panel. Under "advanced" you should be able to point the plug-in to the jre where your jar-files are located.

Here is a sample code that should connect you to your db:

     
     public void DbClient()
     {     username = new String("****");
          password = new String("****");

          databaseURL = new String("jdbc:mysql://<YourDatabasename>:1433;");
          databaseName = new String("<Yourdatabasename>");
          //Load driver
          try
          {
               Class.forName("org.gjt.mm.mysql.Driver");

          }
          catch (ClassNotFoundException ex)
          {
               System.out.println(ex);
          };
          System.out.println("JDBC driver loaded");
          try
          {
               con = DriverManager.getConnection(databaseURL),username,password);
          }
          catch (java.sql.SQLException ex)
          {
               System.out.println("Error " + ex.getErrorCode() +ex.getMessage());
               //System.exit(0);

          };

     }

Good luck
Avatar of chanbrothers

ASKER

Can I use the MYSQL driver to connect to MS-SQL database?? If I am not wrong, the MYSQL driver connects to the mySQL database. I will need ODBC to connect to MS-SQL rite?
Actually I think the mysql driver mentioned is a driver that is compatible with both, but I wouldn't recommend it risking it. I would down microsofts own Microsoft SQL Server 2000 Driver. Thats the one I use anyway.
Beware that this driver contains three .jar files that need to be copied to the location I mentioned before. They are called msbase.jar, mssqlserver.jar and msutil.jar.
Sorry, I misread.

I would look for type 3 or type 4 drivers. (I don't know what MS's is).

You can find a certified one here as well:

http://www.bcs-corp.com/download.shtml
What's up with you? I've provided reference to the Microsoft SQL JDBC driver usage, and ready to test ODBC example without downloading extra drivers? MySQL? Are you kidding?
Types 3 and 4 beat the bridge driver in performance any day.

My mistake about MySQL.
chanbrothers:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.