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>";
Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
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

kdrag136
21115
accept rate: 0%

edited 15 Mar '13, 18:07

Mark%20Culp's gravatar image

Mark Culp
22.9k9129269

1

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
30.3k300452659
accept rate: 32%

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,
                    dbUser,
                    dbPW);

            PreparedStatement stat1= connManageDB.prepareStatement("SELECT next_database( ? ) as nextID, db_name( next_database( ? )) as nextName");
            Integer nextID= null;
            do
            {
                stat1.clearParameters();
                if (nextID == null)
                {
                    stat1.setNull(1, Types.INTEGER);
                    stat1.setNull(2, Types.INTEGER);
                }
                else
                {
                    stat1.setInt(1, nextID);
                    stat1.setInt(2, nextID);
                }
                ResultSet rs= stat1.executeQuery();
                if (rs.next())
                {
                    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;
                    }
                }
                else
                {
                    nextID= null;
                }
                rs.close();
            }
            while  (!dbFound && nextID != null);
            stat1.close();
permanent link

answered 20 Feb '12, 01:34

ASchild's gravatar image

ASchild
777222740
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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×67
×64
×32

question asked: 18 Feb '12, 00:08

question was seen: 3,309 times

last updated: 15 Mar '13, 18:07