Can't connect to Sql Server database using JDBC - java

I'm trying to make a connection to an already existing database in SQL Server 2012n with Netbeans 8.
Using the connection wizard I select Sql Server 2012 and enter the credentials and fields.
However I get the message that a connection cannot be established.
You can see screenshots of the entered fields:
I also tried using "localhost" instead of "STIJN" this didn't work. I also tried with the default port "1433". Also didn't worked, however what port should this be? Where can I see what port I'm using? Maybe the error lies in the port, otherwise I wouldn't know where I made a mistake.

Go to your SQL Server configuration manager -> SQL server network configuration -> protocols for SQLEXPRESS -> TCP/IP (double click on it for properties).
On protocol page set enabled to Yes.
On IP Addresses page, scroll to bottom.
TCP Dynamic Ports: 54629
TCP Port: 1433
Apply and ok.
Make a connection with:
Host: localhost
Port: 1433
Your credentials
Instance field = blank.
This should work.

Related

How to connect to MSSQL from Pentaho?

I am trying to connect Pentaho to my local MSSQL server.
I start Microsoft SQL Server Management Studio and I am promted with this window and then just click Connect:
I am using Java 8 Update 261 and Pentaho 7.1. I have downloaded MSSQL JDBC driver from following link:
https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15
and put mssql-jdbc-8.4.1.jre8.jar into pdi-ce-7.1.0.0-12\data-integration\lib folder. Also I have coppied file mssql-jdbc_auth-8.4.1.x64.dll into C:\Program Files\Java\jre1.8.0_261\bin...
Now when I start Pentaho via Spoon.bat file, and try to add MSSQL connection, I get errors, furthermore I have no idea what I am supposed to enter into fields:
HOSTNAME: ?
DATABASE NAME: (name of database is PROFIT)
INSTANCE NAME: ?
PORT: 1433
USERNAME: ?
PASSWORD: ?
From the picture you can see what I have entered. I also checked Use Integrated Security box.
I get following error:
Error connecting to database: (using class com.microsoft.sqlserver.jdbc.SQLServerDriver)
The TCP/IP connection to the host DESKTOP-CS70NHL, port 1433 has failed. Error: "Socket operation on nonsocket: configureBlocking. 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.".

Failing to connect to MySQL Server from Java application

I am attempting to establish a connection to my local sqlserver instance on android with this line
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:3306;DatabaseName=test", "admin", "password");
But I am getting this error
The TCP/IP connection to the host localhost, port 3306 has failed.
Error: "Connection refused. 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
I know that using jdbc to create direct database connections on android is not recommended for very good reasons, but that is not my concern right now.
I have verified in sql server configuration manager that tcp/ip is enabled for LOCAL and that IPAll TCP Port is set to 3306. I restarted the server after making these changes.
I have created a firewall rule allowing tcp/ip connections on port 3306 for local/domain connections inbound and outbound.
I checked the port with telnet telnet localhost 3306 and it connected.
Any help is appreciated, it is beginning to get frustrating.

The TCP/IP connection to the host localhost, port 1433 has failed

I'm getting this 2 errors repeatedly when I'm trying to connect eclipse with sql server through jdbc. Could anyone help me with this or explain why am I getting this?
1. The TCP/IP connection to the host localhost, port 1433 has failed.
2. The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption.
Thank you.
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(driver).newInstance();
String connString ="jdbc:sqlserver://localhost:1433/databaseName=Engg_Street;instance=SQLSERVER;encrypt=true; trustServerCertificate=true";
String username = "Vijayalakshmi";
String password = "";
conn = DriverManager.getConnection(connString,username,password);
You need to Go to Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager
When it opens Go to
SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for SQLExpress
Where you'll find the Protocol TCP/IP, if disabled then Enable it Click on TCP/IP, You'll 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...
If you are running SQLSERVER EXPRESS:
Use "SQL Server Configuration Manager" or write "mmc.exe" on Win+R and search "SQL Server Network Configuration", in the "Protocols for SQLEXPRESS" enable the option TCP/IP
I hope than this works for somebody!!
For this kind of issue - you can resolve by checking whether all sql server related services are running or not.
TO check Start -> Run -> services.msc
check services related to sql as
SQL Server (MSSQLSERVER)
SQL Server Agent (MSSQLSERVER)
SQL Server Analysis Services (MSSQLSERVER)
SQL Server Browser
SQL Server Integration Services 10.0
SQL Server Reporting Services (MSSQLSERVER)
SQL Server VSS Writer
Start all the above services. and set to start up type to Automatic.
SQL Active Directory Helper Service is not required to keep running.
It can used to deal with error SQL server connection error 2
and
with
he TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused:
It can used to deal with error SQL server connection error 2 and with he TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: https://www.youtube.com/watch?v=R8_jjkeulEw
or please check if the table name is duplicated, i changed the table name --> problem solved

How to connect my web application to SQL Server / SQL Server Express

I am working with a web application that connects to a SQL Server database with this:
jdbc.url=jdbc:jtds:sqlserver://127.0.0.1/MyDatabase
jdbc.username=sa
jdbc.password=password
I am relatively new to SQL Server, however I have successfully accessed a SQLEXPRESS instance through the SQL Server Management Studio.
I can see from the Security->Login folder there is a "sa" entry
However when I run the web application I am getting this error:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property
'dataSource' threw exception; nested exception is
org.apache.commons.dbcp.SQLNestedException: Cannot create
PoolableConnectionFactory (Network error IOException: Connection
refused: connect)
I seems that the user: "sa" is not authenticated to access the database?
If so, how can I use the SQL Server management studio to fix this?
Make sure authentication mode is set to Mixed. It's set to Windows authentication mode as default which disables sa account. See here: http://msdn.microsoft.com/en-us/library/ms143705%28v=sql.90%29.aspx
Edit: as Jacob suggested, make sure you can connect via TCP/IP (I assume it's used by the driver). To do so launch SQL Management Studio and in connection preferences set: Authentication - SQL Server authentication, login - sa, password - your password. Then click Options, connection properties and select tcp/ip as protocol. Check if you can login.
Also check these things:
http://msdn.microsoft.com/en-us/library/bb909712(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/bb909725(v=vs.90).aspx
To check whether TCP/IP is enabled and the port is not blocked you can use "telnet 1433". Until telnet doesn't connect, jTDS won't either.
e.g, c:>telnet servername 1433
to enable telnet client on windows
http://social.technet.microsoft.com/wiki/contents/articles/910.how-to-enable-telnet-client-in-windows-7.aspx

Connection JDBC SQL Server Error

I've tryed to create a connection with a Microsoft SQl Server 2008 database through JDBC on Eclipse SDK. I've dowloaded JDBC driver from microsoft and I've installed it, then I've added at my System environment variables CLASSPATH the path of sqljdbc4.jar file. After icluding in the Eclipse project the jar file I've tryed to create the connection using:
String connectionUrl = "jdbc:sqlserver://localhost;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
but it doesn't works, and launch me this exception:
com.microsoft.sqlserver.jdbc.SQLServerException: TCP/IP connection
at host localhost, port 1433 failed. Error: "Connection refused:
connect. Verify connection properties and make sure an instance of SQL
Server is running on the host and is accepting TCP/IP connections at
the port. Be sure no firewall blocks connections at the port.
I'm working on a JRE 1.6 so a sqljdbc4 should work, and I've created a working ODBC, so the server is responding, and the error should be in java command or JDBC installation.
Can anyone help me?
At the risk of stating the obvious, this looks to me as if TCP connections haven't been enabled on SQL Server. You have to manually enable them, they don't come enabled by default.
There's an article on MSDN here which explains how to enable TCP protocols for SQL 2005/2008.
Following are the quick trouble shooters:
Try to connect to your server using external front end.
Check if your firewall blocks the connection to the port
Check to see if server is really up.
A Suggestion :
If you are using eclipse , you don't need to add the jar into CLASSPATH variable , you can just add it in library of your project to make it available at runtime

Categories