1

I want to get the list of all database names from a Sybase DB server. I can connect the Sybase db through Java, but don't know how to get the list of database names. I am using jconn4 jar file.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.sybase.jdbc4.jdbc.SybDriver;

public class ConnectToSybase {
    public static Connection conn = null;
    public static Statement stmt = null;
    public static SybDriver sybDriver = null;
    public static ResultSet rs = null;
    public static String dbServerIP = "10.10.10.11";
    public static String portNo = "5000";
    public static String dbName = "NewDB";

    public static void main(String[] args) {
        try {
            Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance();
            System.out.println("Driver loaded");

            conn = DriverManager.getConnection("jdbc:sybase:Tds:" + dbServerIP + ":" + portNo, "usrname", "password");
            stmt = conn.createStatement();

        } catch (Exception e) {
            System.out.println("In exception");
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                stmt.close();
                conn.close();
            } catch (Exception e) {

            }
        }
    }
}
AbiSaran
  • 2,538
  • 3
  • 9
  • 15
  • Which Sybase product (ASE? IQ? SQLAnywhere? Advantage?) and version? – markp-fuso Jul 08 '19 at 12:14
  • Hi, for ASE v15. – AbiSaran Jul 08 '19 at 13:42
  • 3
    I don't see anything in your code that looks like a (SQL) query or (Sybase/ASE) metadata lookkup so I can't tell if you have a java issue or a Sybase/ASE issue; while I can't answer the java portion of the question, I can answer the ASE portion of the question: `select name from master..sysdatabases` – markp-fuso Jul 08 '19 at 13:49

1 Answers1

2

I found myself a working code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.sybase.jdbc4.jdbc.SybDriver;

public class ConnectToSybase {
    public static Connection conn = null;
    public static Statement stmt = null;
    public static SybDriver sybDriver = null;
    public static ResultSet rs = null;
    public static String dbServerIP = "10.10.10.11";
    public static String portNo = "5000";
    public static String dbName = "NewDB";

    public static void main(String[] args) {
        try {
            Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance();
            System.out.println("Driver loaded");

            conn = DriverManager.getConnection("jdbc:sybase:Tds:" + dbServerIP + ":" + portNo, "usrname", "password");
            stmt = conn.createStatement();

            List<String> dbList = new ArrayList<String>();
            // getting list of DB names from the DB server
            ResultSet rs = conn.getMetaData().getCatalogs();
            while (rs.next()) {
                dbList.add(rs.getString(1));
            }

            for (String list : dbList) {
                System.out.println(list);
            }

        } catch (Exception e) {
            System.out.println("In exception");
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                stmt.close();
                conn.close();
            } catch (Exception e) {

            }
        }
    }
}
AbiSaran
  • 2,538
  • 3
  • 9
  • 15