Connecting to a remote SQL sever using Java

Can anyone tell me how to connect to SQL Server using Java?
mfinoccAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
0
mfinoccAuthor Commented:
Here's the sample code I'm working with...

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class SQL
{
      public static void main(String[] args) throws SQLException, ClassNotFoundException
      {
              String jdbc_url="jdbc:jtds:sqlserver://mysqlserver:1433/mydatabase";
              String jdbc_username="myname";
              String jdbc_password="mypassword";
              String jdbc_driver="net.sourceforge.jtds.jdbc.Driver";
              Class.forName(jdbc_driver);
              Connection cn=DriverManager.getConnection(jdbc_url, jdbc_username,jdbc_password);
              Statement stmt = cn.createStatement();
              ResultSet rs = stmt.executeQuery("SELECT last_name, first_name, phone FROM employees");

              while (rs.next()) {
                String x = rs.getString("last_name");
              String s = rs.getString("first_name");
              String f = rs.getString("phone");
          }
      }
}


Here are the errors generated...

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at SQL.main(SQL.java:16)
0
mfinoccAuthor Commented:
Ooops, my paste was incomplete on the errors...

Exception in thread "main" java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at SQL.main(SQL.java:16)
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You need to include the jar that contains the SQL Server JDBC driver (ie. the one containing "net.sourceforge.jtds.jdbc.Driver" class) in the classpath that you run the program with.

Do you know how to set the classpath? If not, let us know how you are running this program, ie. via an IDE like Eclipse, Netbeans, IntelliJ or on the command line, and we can help you to get it running.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.