cancel
Showing results for 
Search instead for 
Did you mean: 

Why is (com.sybase.jdbc3.jdbc.SybDriver) ServiceName ignored in connection url with single database?

Former Member
6,504

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.

Accepted Solutions (1)

Accepted Solutions (1)

VolkerBarth
Contributor

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.

Answers (1)

Answers (1)

Former Member
0 Kudos

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();