mssql port auto detect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patricktn
    New Member
    • Jun 2009
    • 3

    mssql port auto detect

    Hi,

    In Windows programming, there is a built-in function to get MSSql database server names and connect to it without needing to know the port numbers.

    Is it possible to do this in Java (connect to MSSql database without knowing the port number used)?

    As far as I know, using JDBC, I would need to know the port numbers to connect to MSSql server. If that's the case, does anybody know if it is possible to detect the port numbers of MSSql?

    Regards,
    Patrick.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    It depends on your MSSQL driver; see the API documentation for the Driver interface; you can consult it whether or not it accepts a URL without a port number.

    kind regards,

    Jos

    Comment

    • patricktn
      New Member
      • Jun 2009
      • 3

      #3
      Thanks. I'm sure it needs a port number because I have tried it without a port number, and it won't connect.

      My java program uses an ini/text file that stores the port number, to connect to the MSSql database server. User has to input the port number of MSSql into the ini/text file.

      However I would like my java program to automatically connect without user needing to know the port number.

      I have tried to do this by trial and error, i.e. using a range of port numbers to connect. But, this methods takes too long, 1 second for every port to test.

      It would be great if there is a way to connect without needing to know the port number just like in some windows program.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Use JNI to consult that built-in function you mentioned; it delivers the valid port numbers to your Java code and you're in business. Or maybe you can write a bit of wrapper code in C/C++ that builds your .ini file for you before your Java process starts.

        kind regards,

        Jos

        Comment

        • patricktn
          New Member
          • Jun 2009
          • 3

          #5
          Thanks Jos. I'll try to use JNI.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by patricktn
            Thanks Jos. I'll try to use JNI.
            Good; basically what you have to do is write a little class with 'native' methods. Feed that class to your 'javah' tool (it comes with your JDK distribution). It generates a .h header file for you than contains the prototypes for all your native methods. User that file (i.e. build another file) that should contain the definitions of all those prototypes. Use your C/C++ compiler to compile it all and build a .dll or .so file out of it.

            In your Java code you should use the System.loadLibr ary() method to load your .dll (or .so) file and you're up and running ...

            kind regards,

            Jos

            Comment

            • kennedyusa058
              New Member
              • Jun 2009
              • 1

              #7
              Thanks for the quick reply!

              <link removed>
              Last edited by Frinavale; Jun 18 '09, 07:40 PM. Reason: Link removed: irrelevant to question (spam).

              Comment

              Working...