Not able to connect sql server 2008 r2 with java - java

I am trying to connect sql server 2008 r2 with my java code , But it is showing me this error .
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host DINESH-PC, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:13)
my code is:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String dbURL = "jdbc:sqlserver://localhost:1433;user=sa;password=123456";
Connection conn = DriverManager.getConnection(dbURL);
if (conn != null) {
System.out.println("Connected");
}
i am using jre7 and sqljdbc4-3.0.jar is included in library.
Help me with ur suggesstions.

You have quite a lot of information from exception message. Just follow the hints. Check if all connection string are right. Check if DB server run. Check if there is no firewall blocking that port on server where DB is situated.
also must check wheather tcp/ip is enabled ..
to check
open sql server configration manager
select sql server network configration
then, select protocol for MSSQLSERVER
them check wheather tcp/ip is enabled or not. If not enable it.

Related

i want to connect to SSMS database from eclipse but it throws SQLserverException

I want to connect to SSMS database from eclipse. when compiler reaches to DriverManager.getConnection(url) line it throws error.I enable TCP/IP also but it giving error.
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("driver loaded successfully");
connection= DriverManager.getConnection( "jdbc:sqlserver://localhost\\MSSQLSERVER;user=sa;password=coder182");
System.out.println("Connection created successfully");
connection.setAutoCommit( autoCommit);
statement=connection.createStatement();
String query ="insert into User_information(name, card_no,amount)" + "values ('"+name+"','"+card_no+"', '"+amount+"');";
statement.executeUpdate(query);
System.out.println(query);
}catch(Throwable th){
th.printStackTrace();
}
}
Error:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the
host localhost, named instance mssqlserver failed. Error:
"java.net.SocketTimeoutException: Receive timed out". 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.
I want to connect it to database.
open SQL Server Configuration Manager
SQl Server Network Configuration
protocols for MSSQLSERVER
TCP/IP(Must be enabled already or enable it)
(Double Click)
IP Addresss
scroll down to IPALL
Enter port number 1433 or 1434 and leave dynamic ports empty and apply setting and restart your service
And you must specify databaseName you want to connect
"jdbc:sqlserver://localhost;instance=MSSQLSERVER;databaseName=name_of_database;user=sa;password=your_password;"
Mark answer as accepted if issue resolved
If "MSSQLSERVER" is the name of the database then try
"jdbc:sqlserver://localhost;databaseName=MSSQLSERVER;user=sa;password=coder182"
if it is the name of the instance try
"jdbc:sqlserver://localhost;instanceName=MSSQLSERVER;user=sa;password=coder182"
and of course verify that the server is running and using port 1434

How do I fix port 1433 failing error in my SQL server with JDBC, I tried everything [duplicate]

This question already has answers here:
The TCP/IP connection to the host localhost, port 1433 has failed error, need assistance
(4 answers)
Closed 5 years ago.
So I'm trying to do UDT using in Java with my eclipse and SQL server 2014:
This is the part of the code where I connect my SQL server to my eclipse, I already installed the JDBC jar to the path.
Connection con = null; PreparedStatement statement = null; //to take care of the sql statements to be run
//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost‌​;databaseName=Company;integratedSecurity=true;");
//con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433‌​;databaseName=MASTER‌​;user=sa;password=Se‌​cret");
This is my error output, although I haven't created my tables in SQL server yet, I want try to see if eclipse is connected to SQL server 2014, so far this is error I have:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost‌​, port 1433 has failed. Error: "localhost‌​. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2369)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:551)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1963)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at insertion.insert_values(insertion.java:12)
at PointType.main(PointType.java:65)
Don't worry about my insertion part, I just want to know why my port connection is failing?
I tried everything but so far still connection is not working, what could be causing it?
You've misspelled "localhost" in your URL

JDBC connection doesn't work with Sql Server 2012 or higher

The code below works with Sql Server 2008 but not with Sql Server 2012 and 2014 (v 11 or 12)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://myhost\\myinstance:1433;database=mydb";
connection = DriverManager.getConnection(url,"username", "pwd");
I'm using Java Runtime 1.7
I'm using the Microsoft Drivers as you can see in the Shell line below.
I've tried putting
sqljdbc_auth.dll (I've tried both 32 and 64 bit) in the jar drivers folder,
in JRE bin folder or even passed as java.library.path
I've checked in the configuration manager (on the Sql Server) that TCP/IP is enabled and responds at port 1433.
The firewall is off.
Here is the error I get:
c:\test\TestConn\Test2\bin>"C:\Program Files\Java\jre7\bin\java"
-classpath C:\libsqlsvr\sqljdbc_6.0\ita\sqljdbc4.1.jar;.
-Djava.library.path=C:\libsqlsvr\sqljdbc_6.0\ita\auth\x64 Test
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the hot myhost , port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(S
QLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExcepti
onToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:22
43)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer
verConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne
ction.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon
nection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.
java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Test.testsqlsvr(Test.java:135)
at Test.main(Test.java:24)
Do you know what could cause the problem?
Since you're using HOST:PORT identification of the SQL Server in JDBC, you should provide a valid IP or hostname in the HOST part, not the instance name (i.e. no "myhost\myinstance", but the actual network hostname / IP).
The JDBC connection goes through normal TCP, so the HOST:PORT is an ordinary network address, no Microsoft magic. HOSTNAME\INSTANCE uses a dispatcher service (SQL Server Browser) that resides on a known port (UDP 1434) and forwards the connection according to the \INSTANCE part, and is not combined with port.

building an sql connection url in java

so I am attempting to connect to a database on a network through java and I am having difficulty getting a connection to the server. I believe my problem is with the connection url. I have tried a few different things to test it out, but nothing has been working yet, so hopefully someone can help me. The error I get is:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.1.16, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at dbTest.db.dbConnect(db.java:23)
at dbTest.testConnection.main(testConnection.java:11)
Though depending on which connection url I use sometimes i get a java.net.sockettimeoutexception error. This is the code I am currently using to try to connect:
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Properties props = new Properties();
props.setProperty("user","sa");
props.setProperty("password","");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://192.168.1.16;databaseName=ECCS",props);
//MSSQL01\\ECLAIMS_DATA
System.out.println("connected");
}
catch (Exception e)
{
e.printStackTrace();
}
The commented out MSSQL01\ECLAIMS_DATA is the name of the server and database I am attempting to connect to, I have tried using that as well. I'm just a little lost as to where everything needs to be. I have tried including the username and password in the connection string, I have tried using getConnection(url,username,password). I'm also not sure if the databaseName is where that needs to go. Can anyone help me out?

The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect [duplicate]

This question already has answers here:
JDBC connection failed, error: TCP/IP connection to host failed
(9 answers)
Closed 5 years ago.
I want to connect Java class file with SQL server 2008. I have logged in with SQL server authentication use: sa, pass:123456. But i am receiving error in connectivity.
static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String dburl = "jdbc:sqlserver://localhost\\SQL2008:1433;Database=Java";
static String user = "sa";
static String password = "123456";
public static void update() throws Exception{
String sql = "UPDATE Categories SET Id='COM' WHERE Id='LAP'";
Class.forName(driver);
Connection conn = DriverManager.getConnection(dburl, user, password);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
}
public static void main(String[] args) throws Exception {
Basic.update();
}
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1048)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:829)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:712)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Tutorials.jdbc.Basic.update(Basic.java:48)
at Tutorials.jdbc.Basic.main(Basic.java:72)
Probably a little late to answer this question, but doing it for my own benefit.
The actual TCP port to connect to through JDBC can be found in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\MSSQLServer\SuperSocketNetLib\Tcp
Use this value to connect to SQL Server.
For example
"jdbc:sqlserver://localhost\SQLEXPRESS:49922;databaseName=db;user=user;password=secret"
Also ensure that TCP connections are enabled in the server configuration tools.
I think that your dburl may be malformed.
Try this url instead.
String dburl= "jdbc:sqlserver://localhost\\SQL2008:1433;" +
"databaseName=Java;user=sa;password=123456";
Make sure you have Driver jar file and change your dburl to this as mentioned below
try this
String dburl ="jdbc:sqlserver://SQL2008:1433;DatabaseName=Java;user=sa;Password=123456";
Thanks you for your help, i'm fix my problem, it's not error on String dburl= "jdbc:sqlserver://localhost\SQL2008:1433;Database:Java"
it's error port 1433 no connect in SQL server 2008
Where i find the Protocol TCP/IP, if disabled then Enable it Click on TCP/IP, i'm find its properties.
In this properties Remove All the TCP Dynamic Ports and Add value of 1433 to all TCP Port and restart your SQL Server Services > SQL Server
And Its Done...
Thanks all for your help!
TCP/IP Connection Refused.
Run NETSTAT -A to see a list of ports that are open (they'll say LISTENING) If 1433 isn't among them, then SQL server isn't running or isn't listening on that port. Follow Michael Todd's advice from there.
If 1433 is on the list, check your local firewall to see if it allows connections to/from localhost and/or allows connections to 1433.
You can try TELNET 1433 and see if it connects (you should get a blank screen with a cursor blinking at the top left.) (You may have to enable the telnet client in Programs and Features.) If so then you have a problem with your code.

Categories

Resources