views:

62

answers:

2

Is there a jdbc driver for SQL Server that can search for database instances on network?

Just wanting to emulate "OSQL -L" from the JDBC driver. Dont want to have to call OSQL from JNI or something. I also don't want to have to write my own code for scanning a UDP port.

I want my Java application to be able to search for live SQL Servers and prompt me with a list of available servers. Isn't this a reasonable expectation for a JDBC driver? OSQL.exe can do it and so why not the JDBC driver?

+1  A: 

The JDBC API doesn't offer any facilities for this. Your best bet is to fall back to low level Java socket programming and test if TCP port 1433 is open or not and then try to connect it.

Socket socket = null;
try {
    socket = new Socket("localhost", 1433);
    System.out.println("Service is available!");
} catch (Exception e) {
    System.out.println("Service is not available.");
} finally {
    if (socket != null) try { socket.close(); } catch (IOException logOrIgnore) {}
}

Once confirmed that the service is available, then you can connect it the usual way without database name in URL and get a list of available catalogs (databases) by DatabaseMetaData#getCatalogs().

BalusC
A: 

osql is actually quite stupid when it comes to finding SQL Server instances.

The same technique is used by SSMS, Enterprise Manager etc and it's unreliable if your SQL boxes aren't all in the same room (Ok, slight exaggeration) with default instances.

What you could do is roll your own based on the source code here from SQLPing1.1 and SQLPing.Net... which I've used to good effect before.

gbn