10 January 2013

Connecting to Sybase from Java with the JTDS drivers

A short piece of Java code that uses the jtds drives to connect to Sybase and issue a query:

import java.sql.*;
import net.sourceforge.jtds.jdbc.*;

public class SimpleProcSybase {

    private static Connection conn;

    public static void main(String[] args)
    throws ClassNotFoundException, SQLException, InterruptedException
    {
        connect();
        System.out.println ("Got connected OK");
        PreparedStatement stmt = conn.prepareStatement("select 'abc'");
        ResultSet res = stmt.executeQuery();
        while(res.next()) {
            System.out.println(res.getString(1));
        }
    }

    public static void connect()
    throws ClassNotFoundException, SQLException
    {
        DriverManager.registerDriver
            (new net.sourceforge.jtds.jdbc.Driver());

        String url = "jdbc:jtds:sybase://localhost:4100";

        conn = DriverManager.getConnection(url,"user","password");
    }

}
blog comments powered by Disqus