Using jTDS to connect to a Windows SQL DB from AIX

The jTDS drivers may be easier to configure than freeTDS in your unix environment to allow database updates to windows. You have to compile the source yourself, but you would be doing in java rather than C. Here is a link to sourceforge:

jTDS sourceforge
If this doesn’t work, I have a version of the jar file that it would compile and also the subdirectories of compiled class code which I know runs at AIX 5.3:

jtds-1.2.jar

jtds-1.2.tar

The following example script will make a simple database connection and is pretty self-explanatory, it requires that the ‘net’ directory structure exist because I am not clever enough to figure out how to use jar files yet and I just un-jarred it with the -x command:

/* 
Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
Use of this software is authorized pursuant to the terms of the license found at
http://developer.java.sun.com/berkeley_license.html.

Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

- Redistribution of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

- Redistribution in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

Neither the name of Sun Microsystems, Inc. or the names of contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.

This software is provided "AS IS," without a warranty of any kind.
ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICORSYSTEMS, INC. ("SUN")
AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN
IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

You acknowledge that this software is not designed, licensed or intended for
use in the design, construction, operation or maintenance of any nuclear
facility.


*/


/*
 * Copyright 2003 Sun Microsystems, Inc.  ALL RIGHTS RESERVED.
 * Use of this software is authorized pursuant to the terms of the license found at
 * http://developer.java.sun.com/berkeley_license.html.
 */

import java.sql.*;
import net.sourceforge.jtds.jdbc.*;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


class MS_SQL_Execute {

  public static void main(String args[]) {

    String url = "jdbc:jtds:sqlserver://nad0019st01:1433/DBAReporting";
    Connection con;
    Statement stmt;
    String query = "";
    String lastcol = "";

    File file = new File("execute.sql");

FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);

      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      // dis.available() returns 0 if the file does not have more lines.
      while (dis.available() != 0) {

      // this statement reads the line from the file and print it to
        // the console.

        query = dis.readLine();
        System.out.println(query);


    try {
      Class.forName("net.sourceforge.jtds.jdbc.Driver");

    } catch(java.lang.ClassNotFoundException e) {
      System.err.print("ClassNotFoundException: ");
      System.err.println(e.getMessage());
    }

    try {
      con = DriverManager.getConnection(url,
                   "AIXUser", "P@ssw0rd");

      stmt = con.createStatement();

  /*    stmt.executeUpdate("insert into SUPPLIERS " +
                   "values(49, 'Superior Coffee', '1 Party Place', " +
         "'Mendocino', 'CA', '95460')");
 */
      ResultSet rs = stmt.executeQuery(query);

/*
      ResultSetMetaData rsmd = rs.getMetaData();
        int numColumns = rsmd.getColumnCount();

// Get the column names; column indices start from 1
        String FirstcolumnName = rsmd.getColumnName(1);
        for (int i=2; i
			

4 thoughts on “Using jTDS to connect to a Windows SQL DB from AIX

  1. i am getting following error
    Exception in thread “main” java.sql.SQLException: Unable to get information from SQL Server: DATABASE.
    at net.sourceforge.jtds.jdbc.MSSqlServerInfo.(MSSqlServerInfo.java:97)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC2.(ConnectionJDBC2.java:287)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC3.(ConnectionJDBC3.java:50)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at controller.Test.main(Test.java:23)

  2. hi i have read your blog after spending days on an ibm aix to sqlserver connection error. Thanks to you, your jtds jar works .. However what i dont get is “how your jar works?”. i have downloaded from jTDS sourceforge but downloaded jars got an silly unsupported vc socket option. have you change vc_socket option in source?

  3. Java is still a bit of a mystery to me. I confess that I simply got something to work and stuck it onto my blog for safe keeping. I have managed to befriend the Java folks at work, so maybe one day I will print this all off and shove it in their face in hopes of getting an explanation. 😉

Leave a Reply

Your email address will not be published. Required fields are marked *