JDBC failed to connect to the database: COM. Microsoft. sqlserver. JDBC. sqlserverexception: The TCP/IP connection to the host fails.

Source: Internet
Author: User
Tags mssqlserver
Objective: To practice JDBC

Problem description: The simplest database connection error is that the TCP/IP connection to the host fails. (Windows 7 Operating System)

Eclipse 8.5 error:

Com. Microsoft. sqlserver. JDBC. sqlserverexception: The TCP/IP connection to the host fails. Java.net. connectexception: Connection refused: connect
At com. Microsoft. sqlserver. JDBC. sqlserverexception. makefromdrivererror (unknown source)
At com. Microsoft. sqlserver. JDBC. sqlserverconnection. connecthelper (unknown source)
At com. Microsoft. sqlserver. JDBC. sqlserverconnection. loginwithoutfailover (unknown source)
At com. Microsoft. sqlserver. JDBC. sqlserverconnection. Connect (unknown source)
At com. Microsoft. sqlserver. JDBC. sqlserverdriver. Connect (unknown source)
At java. SQL. drivermanager. getconnection (drivermanager. Java: 582)
At java. SQL. drivermanager. getconnection (drivermanager. Java: 185)
At userlogin. Main (userlogin. Java: 18)

Source code:

Import Java. SQL. statement; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. sqlexception; public class userlogin {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubconnection con = NULL; try {// load the driver class. forname ("com. microsoft. sqlserver. JDBC. sqlserverdriver "); con = drivermanager. getconnection ("JDBC: sqlserver: // localhost: 1433; databasen Ame = login "," admin "," admin "); Statement statement = con. createstatement (); string insertsql = "insert into myuser values ('banana ', '000000');"; statement.exe cute (insertsql); statement. close (); con. close (); system. out. println ("success! ");} Catch (classnotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace ();}}

Solution:

After SQL Server 2005 is installed. Find the SQL Server Configuration Manager tool in the directory, and find MSSQLServer under the network configuration of SQL Server 2005 in the directory on the left. Click "all protocols" on the right and find the TCP/IP, it is disabled. You can restart the MSSQLServer service after it is enabled (this step is required !), This item can be found in the SQL Server 2005 service under this tool, or in the Windows service.

Other possible solutions:

There are many answers on the Internet that the TCP port is not 1433. Check the real port and modify it in the code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.