JDBC: Simple MSSql connection example not working

后端 未结 8 1523
无人共我
无人共我 2020-12-08 10:29

I am learning Java and need to just run something simple to retrieve some data from MSSQL via JDBC. The example in my book doesn\'t work (but it is several years old) and th

相关标签:
8条回答
  • 2020-12-08 10:57

    Thanks Finally it's working. If it shows below message as error,

     Verify the server and instance names and check that no firewall is blocking
     UDP traffic to port 1434. For SQL Server 2005 or later,
     verify that the SQL Server Browser Service is running on the host
    

    Please enable Sql Server Browser by,

    Start > Control Panel > Systems & Security > Administrative tools > Services

    Select SQL Server Browser Right click and select properties.

    Set start type as Automatic. Click on Apply > click on start > click on Ok

    Make Sure your IPALL TCP Address is - 1433 !

    0 讨论(0)
  • 2020-12-08 10:58

    It may be silly, but check that your Products table is not empty. Is the only reason I see for no getting any exceptions and no printing anything in the console.

    0 讨论(0)
  • 2020-12-08 11:05

    You may want to check the steps I listed here: Error:The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect and see if the SQLExpress instance is configured correctly, then modify your JDBC URL to specify the instance name separately. The example there is for password-based authentication, but works with your integratedSecurity=true; property.

    0 讨论(0)
  • 2020-12-08 11:06

    OK, so here's what solved my problems:

    1. Download latest MSSQL JDBC driver from here: http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx

    2. Referenced the 2 JAR files in my project: sqljdbc.jar and sqljdbc4.jar (I'm not yet sure if both of the above are required or just one..)

    3. Make sure the SQL Server Browser windows service is running

    4. Open SQL Server Configuration Manager and go to Protocols for SQLEXPRESS under SQL Server Network Configuration. Right-click on TCP/IP and choose Properties. Set Enabled = YES.

    5. While you're there, click on IP Addresses tab and find the section IP All. Set TCP Port to 1433.

    6. Add sqljdbc_auth.dll to your PATH Environment Variable. In my case: D:\Java\sqljdbc_4.0\enu\auth\x64

    7. Copy the sqljdbc_auth.dll to your JDK directory. In my case: C:\Program Files\Java\jdk1.7.0_04\bin

    I hope this helps someone.

    0 讨论(0)
  • 2020-12-08 11:06

    Named and Multiple SQL Server Instances

    If you need to connect from uni* to a named instance, please pay particular attention when you're building the JDBC connection URL

    If both a portNumber and instanceName are used, the portNumber will take precedence and the instanceName will be ignored.

    This also means that if want use Instance Names you cannot change the port number.

    In any of these lines the parameter myinstancename will be ignored:

    • jdbc:sqlserver://db-devmyinstancename;databaseName=mydbname;portNumber=1433
    • jdbc:sqlserver://db-dev;databaseName=dbname;instanceName=myinstancename;portNumber=1433
    • jdbc:sqlserver://db-dev\myinstancename:1433;databaseName=mydbname
    • jdbc:sqlserver://db-dev:1433;databaseName=dbname;instanceName=myinstancename

    So if you want use instance names the port number must be removed (tried with jdbc sqlserver drivers ver. 3, 4, 4.1)

    0 讨论(0)
  • 2020-12-08 11:08

    Please enable Sql Server Browser by, Start > Control Panel > Systems & Security > Administrative tools > Services Select SQL Server Browser Right click and select properties.

    Set start type as Automatic. Click on Apply > click on start > click on Ok

    Make Sure your IPALL TCP Address is - 1433 !

    0 讨论(0)
提交回复
热议问题