Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java Connection to MSSQL

Posted on 2003-03-04
12
Medium Priority
?
2,085 Views
Last Modified: 2011-10-03
How do I connect to MSSQL database in java??
0
Comment
Question by:chanbrothers
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 80 total points
ID: 8063728
Or use jdbc:odbc bridge as follows (just change the server name, here is soft):

import java.sql.*;

public class Test
{
     public static void main(String[] args) throws Exception
     {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          Connection con=DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=soft;Uid=SA;Pwd=;Database=northwind","","");
          CallableStatement st=con.prepareCall("sp_tables");
          ResultSet rs=st.executeQuery();
          while(rs.next())
          {
               System.out.println(rs.getString(3));
          }
          con.close();
     }
}

Also, if need change Uid with yours user name, and PWD with yours password! This works!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:jcaldwel
ID: 8064370
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.
0
 

Expert Comment

by:RayGun
ID: 8065050
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
0
 

Author Comment

by:chanbrothers
ID: 8068864
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?
0
 

Expert Comment

by:RayGun
ID: 8070198
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.
0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8072437
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
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8073503
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?
0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8073578
Types 3 and 4 beat the bridge driver in performance any day.

My mistake about MySQL.
0
 

Expert Comment

by:CleanupPing
ID: 9058945
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question