Please be aware that the content in SAP SQL Anywhere Forum will be migrated to the SAP Community in June and this forum will be retired.

I have a situation where there are 0-n databases available on the same host and port, with only the database name distinguishing between them. Given a database name, I need to be able to determine if that database can be connected to.

When I have any number of databases other than one, everything works fine. i.e. If I attempt to connect to an unavailable database, I will get an SQLException, as expected. If I connect to an available database, everything works as expected.

The problem is when there is exactly one database available. In this case, it seems like ServiceName is simply ignored. No matter what I put in as database_name, I end up with a successful connection to the (only) available database.

String url = "jdbc:sybase:Tds:<host>:<port>?ServiceName=<database_name>";
DriverManager.getConnection(url, "username", "password");

Is there any way around this? I need a way to know that database_name cannot be connected to.

asked 18 Feb '12, 00:08

kdrag136's gravatar image

accept rate: 0%

edited 15 Mar '13, 18:07

Mark%20Culp's gravatar image

Mark Culp


For what it's worth SELECT DB_NAME() will return the name of the current database, and you could compare it with what you actually wanted.

(20 Feb '12, 09:25) Breck Carter

Do you need to use the jConnect driver?

AFAIK, the somewhat cumbersome way to specify the database one wants to connect to does only apply to jConnect (and might be due to its ASE-based origin).

When using the SQL Anywhere JDBC driver, you can simply use the common SQL Anyhwere connection parameters, including the DBN parameter to name the desired database.

permanent link

answered 20 Feb '12, 10:55

Volker%20Barth's gravatar image

Volker Barth
accept rate: 34%

I don't have a solution for the service name beeing empty, but with this code you can check what databases are running on a give engine:

            Driver DriverRecordset1 = (Driver)Class.forName(_settings.getDatabasedriver()).newInstance();
            String dbConn= _settings.getDefaultDBConnection();
            String dbUser= _settings.getDefaultDBUser();
            String dbPW= _settings.getDefaultDBPassword();
            connManageDB= DriverManager.getConnection(dbConn,

            PreparedStatement stat1= connManageDB.prepareStatement("SELECT next_database( ? ) as nextID, db_name( next_database( ? )) as nextName");
            Integer nextID= null;
                if (nextID == null)
                    stat1.setNull(1, Types.INTEGER);
                    stat1.setNull(2, Types.INTEGER);
                    stat1.setInt(1, nextID);
                    stat1.setInt(2, nextID);
                ResultSet rs= stat1.executeQuery();
                if (
                    nextID= rs.getInt("NextID");
                    if (rs.wasNull())
                        nextID= null;
                    String nextName= rs.getString("NextName");
                    _log.debug("NextID: "+nextID+" nextName: "+nextName);
                    if (nextName != null && nextName.equalsIgnoreCase(searchName))
                        dbFound= true;
                    nextID= null;
            while  (!dbFound && nextID != null);
permanent link

answered 20 Feb '12, 01:34

ASchild's gravatar image

accept rate: 14%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 18 Feb '12, 00:08

question was seen: 6,142 times

last updated: 15 Mar '13, 18:07