Connection JDBC SQL Server Error - java

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

Related

JT400 - JDBC connection refused from IBMi machine but working from Windows machine

JT400 - The JDBC connection is working fine when connecting to a IBMi machine from windows machine.
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
String sysname = "xxx.xxx.xx.xxx";
String xref = "IBMISQLLIB";
String url = "jdbc:as400://" + sysname + ";translate binary=true;prompt=false;naming=sql;libraries=" + xref;
Connection connection = DriverManager.getConnection(url, "USERNAME", "PASSWORD");
print("Connection created successfully");
But fails to create the connection by using the same code - when running the code on the same IBMi machine (by a runnable jar on 'QSH Command Entry'), ERROR:
java.sql.SQLException: The application requester cannot establish the
connection. (A remote host refused an attempted connect operation.
(Connection refused)) at
com.ibm.as400.access.JDError.createSQLExceptionSubClass(JDError.java:887)
at com.ibm.as400.access.JDError.throwSQLException(JDError.java:610)
at com.ibm.as400.access.JDError.throwSQLException(JDError.java:568)
at
com.ibm.as400.access.AS400JDBCConnectionImpl.setProperties(AS400JDBCConnectionImpl.java:3522)
at
com.ibm.as400.access.AS400JDBCDriver.prepareConnection(AS400JDBCDriver.java:1430) at
com.ibm.as400.access.AS400JDBCDriver.initializeConnection(AS400JDBCDriver.java:1280)
at
com.ibm.as400.access.AS400JDBCDriver.connect(AS400JDBCDriver.java:403)
at java.sql.DriverManager.getConnection(DriverManager.java:675) at
java.sql.DriverManager.getConnection(DriverManager.java:258)
Any idea what is going wrong now ??
because this was working some days back
Same code is working on other IBMi servers
Some more details about the IBMi machine are:
No access right issue for the username used in connection profile
No firewall is setup on IBMi machine
TCP/IP configuration is verified as explained - Configuring TCP/IP on IBM i
Is the database host server active? Use WRKACTJOB to see if jobs name QZDASOINIT are running.
If not, try starting the host server with the command STRHOSTSVR *DATABASE.
If you're running your jar file from QSH, you need to make sure that multi-threading is allowed. That could possibly gum things up.
The same code should work just fine if it works on the PC unless you are using a different version of the jt400.jar file possibly as well.
Try using localhost also instead of a system name.
Or even writing a small Java class to open and close a connection.
The problem was related to default TCP/IP port, which was not listening the JDBC connection request.
And finally the problem got resolved by restarting the IBMi machine.
:)

cant connect my driver to from eclipse to sql server [duplicate]

This question already has answers here:
JDBC connection failed, error: TCP/IP connection to host failed
(9 answers)
Closed 5 years ago.
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. 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)
I'm facing this exception after I tried to connect with my driver through the new database profile.
The information I have was correct and the TCP port was enabled and the firewall was turned off.
I'm using Microsoft SQL server 2008 JDBC driver and I've added the jars.
A "connection refused" is rarely caused by the connecting client, i.e. the JDBC client within your Eclipse setup. A good check is to open a shell (e.g. cmd.exe on Windows) and do a
telnet [server name used in JDBC configuration] 1433
On newer Windows installations you need to install the telnet client because it's not part of the standard installation anymore (Settings -> Software -> Add/Remove Features).
If that leads to a similar error message you can focus your trouble shooting to the network part of your system, since the JDBC side is completely uninvolved. If you can connect, make sure that the server name resolves to the same IP when using the command prompt as it's resolved within Java (I once had this effect which took quite a time to find that out). Especially with localhost as server name one might resolve to 127.0.0.1 while the other resolvs to ::1.

access derby database from an executable jar file

I did a java project with netbeans using derby. The project runs fine in the IDE, but when i build it and run from the .jar file without starting server from the netbeans, it displays:
"java.net.ConnectException : Error connecting to server localhost on port 1527
with message Connection refused: connect."
How can i connect to the database on my client`s machine without netbeans on it?
You need to know the IP of your DB server, as localhost or 127.0.0.1 refers to the DB installed on local machine, and if you run the app on client's machine it is pretty sure, that this DB is not there locally. You need to specify the exact IP address, if multiple clients should connect to the same DB.
If you just need a DB per app, then you can use Embedded Derby, that starts and stops with your application, so you don't really need external server (very good solution in case when this is the only app that use this DB):
public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
...
Class.forName(driver).newInstance();
Get an Embedded Connection
public String protocol = "jdbc:derby:";
...
conn = DriverManager.getConnection(protocol + "derbyDB;create=true", props);
That embedded connection URL, fully constructed, looks like this:
jdbc:derby:derbyDB;create=true

Connect to sql server from java with jdbc(windows authentication mode)

I need to connect to Sql Server 2008 from java using jdbc 4.0.
I have a very simple code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost;" +
"integratedSecurity=true;";
Connection con = DriverManager.getConnection(connectionUrl);
But i have this error:
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. 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)
...
I follow this answer:https://stackoverflow.com/a/12524566/1554397
I added jdbc4.jar in Libraries/Compile
SQL Server Browser windows service is running.
In SQL Server Network Configuration i selected Enebled on TCP/IP properties.
I Set TCP Address to 1433.
On Run,VM Options i put -Djava.library.path=my path to sqljdbc_auth.dll
And copy in JDk ,in bin sqljdbc_auth.dll.
What should I do?
EDIT:
When write in cmd telnet localhost 1433 i get 'Could not open connection to the host,on port 1433'
If using Window authentication you can do something like:
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
and then add the path to sqljdbc_auth.dll as a VM argument (you need sqljdbc4.jar in the build path).
Take a look here for a short step-by-step guide showing how to connect to SQL Server from Java should you need more details. Hope it helps!

connecting to postgreSQL server failed

I am trying to install "Kwok information server" and I am new to postgreSQL. I just followed the installation guide of Kwok information server.
when I am trying to install kwok-schema-setup.jar file using java through postgreSQL server in Command Prompt, I am getting the following error..."Connecting to PostgreSQL Server ...failed. Connection refused. Check that the ho
stname and port are correct and that the postmaster is accepting TCP/IP connecti
ons. Cause: java.net.ConnectException: Connection refused: connect
Schema setup encountered errors" .
And when I am trying to connect to the server using pgAdmin III, I am getting the following error "Server doesn't listen
The server doesn't accept connections: the connection library reports
could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432? ".
Please anyone help me regarding, Thanks in advance!!
Looks like your Postgresql daemon is not running. Check if the server has started and is running. I dont know which OS you are using but try ps -ax on linux/mac or the task manager in windows.
Maybe a firewall that is blocking the connection, you have to provide more information about your environment to be sure.

Categories