?
Solved

Java Connection to MSSQL

Posted on 2003-03-04
12
Medium Priority
?
2,076 Views
Last Modified: 2011-10-03
How do I connect to MSSQL database in java??
0
Comment
Question by:chanbrothers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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