errors while connecting java with sql server 2017 - java

public class RDF2Connection {
static Connection connection=null;
final static String connectionUrl = "jdbc:sqlserver://DESKTOP-Q5K9FE6:1433;" +
"databaseName=RDFDB;";
public static Connection getRdf2Connected(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(connectionUrl, "sa", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection
to the host DESKTOP-Q5K9FE6, port 1433 has failed. Error: "Connection
refused: no further information. 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:206)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:257)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2385)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:567)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1955)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1616)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1447)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:788)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1187)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.rdf2.databaseconnection.RDF2Connection.getRdf2Connected(RDF2Connection.java:22)
at MainClass.main(MainClass.java:53)
java.lang.NullPointerException
at MainClass.main(MainClass.java:54)
Process finished with exit code 0

Can you see if the SQL Server is only listening on an IPv6 port? If so you can use the following
System.setProperty("java.net.preferIPv6Addresses", "true");
That would be my only suggestion as far as code changes. Possibly try and use the IP as opposed to the name. If that doesn't work you need to go to your SQL Server and verify that it accepts TCP/IP connections, or check your local firewall settings. At that point the question would be more appropriate for server exchange.

just make sure this the the correct port
this is the true answer
Thanks.

Related

How to fix "The TCP/IP connection to the host localhost, port 1433 has failed."

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.".
This is for java netbeans 8.2 version and running sql server 17.I’ve tried on existing servers and always seem to end up having to TCP/IP connection to the host localhost, port 1433 has failed.
following codes::::
private void signin1ActionPerformed(java.awt.event.ActionEvent evt) {
user= username.getText().trim();
pass= password.getText().trim();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=xyz;integratedSecurity=true");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(" select id, password from signin where username='"+user+"' and password='"+pass+"' ");
if(resultSet.next()){
new NewJFrame().setVisible(true);
this.setVisible(false);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}

How can I connect to db2 database remotely

I am using following java application to connect db2 database for localhost and code is:
String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
String url="jdbc:db2://localhost:50000/sample";
String user="admin";
String password="admin";
try {
//Load class into memory
Class.forName(jdbcClassName);
//Establish connection
connection = DriverManager.getConnection(url, user, password);
stmt = connection.createStatement();
}
catch (SQLException e) {
e.printStackTrace();
}
The problem is that when application and db2 is running on the same machine, then it is working, but if db2 database is on another machine then it is not working. I am using ip in my url which is:
String url="jdbc:db2://192.168.1.68:50000/sample";
And it's giving the following error:
com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.59.81] Exception java.net.ConnectException: Error opening socket to server /192.168.1.64 on port 50,000 with message: Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001
How can I connect to the database remotely?
Check if your private or public network firewall is on ?? If it is on then instead of doing it off make an inbound rule and then try to connect it with DB2 database from another system.

Connection refused: connect, can't connect database mysql

I can't connect remote database. When I connect my own database on localhost, it connects. What's wrong?
Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: Connection refused: connect
Java Code:
String url = "jdbc:mysql://ipadress:3306/database?autoReconnect=true&useSSL=false";
String username = "username";
String password = "password";
String query = "SELECT * FROM database";
System.out.println("Connecting database...");
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
try (Connection connection = DriverManager.getConnection(url, username, password)) {
System.out.println("Database connected!");
//Uruchamiamy zapytanie do bazy danych
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
}
connection.close();
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
}
I can login to database on PHPMyAdmin, I have no root account, it's my friend's database. I checked if port 3306 is open here: http://www.yougetsignal.com/tools/open-ports/ and it's closed. Where can I open it? In router settings in "port forwarding"? What private IP and type(TCP or UDP) should I set to open this port?
(Apologies if this answer is incomplete, but there is too much that does not fit in comments)
1) Don't ignore exceptions. This is bad : with // handle the error and nothing else in your catch block, in case of error there, your code will not report the error, and will move on with the execution (it should exit/break/return, depending on where that piece of code is).
2) I think checking "SHOW GLOBAL VARIABLES LIKE 'PORT';" is not enough. Ask your friend if the database daemon actually listens to port 3306 on a network interface that you can reach. MySQL can be configured with networking disabled (skip-networking), or enabled but only for local machine (bind-address=127.0.0.1 or localhost -- it should be bind-address=0.0.0.0 or bind-address=hostname or public IP address...).
To check for that yourself, if you are on linux, try with nc or telnet (install nc if you don't have it): nc remotehost 3306. If you get "connection refused", the error is definitely not in your java code, but in the server setup.

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

Full error I'm getting:
The TCP/IP connection to the host localhost, 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.".
I have already checked that TCP/IP is enabled, using port 1433, and TCP dynamic ports is empty. I have disabled windows firewall.
Here is my code:
import java.sql.*;
public class DBConnect {
public static void main(String[] args) {
// TODO Auto-generated method stub
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB1;instance=SQLSERVER;encrypt=true;TrustServerCertificate=true;";
String user = "sa";
String pass = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myConn = DriverManager.getConnection(dbURL, user, pass);
try {
Statement myStmt = myConn.createStatement();
try {
ResultSet myRs = myStmt.executeQuery("Select * from Login");
while (myRs.next())
{
System.out.println(myRs.getString("Username"));
System.out.println(myRs.getString("Password"));
}
}
catch (Exception e)
{
System.out.println("Error with query");
}
}
catch (Exception e)
{
System.out.println("Error connecting to database");
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Have you enabled 'Named Pipes' and 'TCP/IP'?
Open the 'Sql Server Configuration Manager' application.
In the left pane, go to 'SQL Server Network Configuration' -> 'Protocols for [instance-name]'
Right-click on both 'Named Pipes' and 'TCP/IP' and select 'enable'.
Have you used the correct port?
Double-click on 'TCP/IP'
Select 'IP Addresses' tab
Scroll to IPAII. Your port number is here.
Restart the 'SQL Server ([instance-name])' windows service.
This error usually come when SQL server not accepting TCP/IP Connection, pls try below steps it will work for sure.
1)open run and add command SQLServerManager15.msc
2)click on network configuration then "protocols for MSSQLSERVER"
3)Select protocol name - "TCP\IP" and make sure that it is enable if not then pls make it enable.
4)Check the property and find port in IP address tab.
Restart the server, it should work
And also make sure that on the same page TCP/IP is enabled
My solution:
Client: DBeaver
Auth: Windows Authentication
After taking the steps:
Enable tcp/ip
Enabling named pipes
Connection string: localhost\SQLEXPRESS (that backslash made all the difference).

JDBC Example for java

I have downloaded JDK 6 and also I have sqljdb4.jar and I have database.properties file that content the following data
database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc:sqlserver://.;databaseName=UserInfo;integratedSecurity=true;
database.username=sa
database.password=admin
B.N : I'm installing the server on my machine and the server name = . , also I'm using Windows Authontication
My problem now is when I try to create connection I have the following error
com.microsoft.sqlserver.jdbc.SQLServerException:
The TCP/IP connection to the host
localhost, port 1433 has failed.
Error: Connection refused: connect.
Please verify the connection
properties and check that a SQL Server
instance 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:130)
I don't know what is the exact problem here
If any one can help I will be appreciated
Thanks in Advance
That's caused by many probabilities like
1- IP is worong
2- Port is wrong
3- There is firewall prevent machine to go out and connect to another IP
4- SQL server down .
try to use
public class JdbcSQLServerDriverUrlExample
{
public static void main(String[] args)
{
Connection connection = null;
try
{
// the sql server driver string
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// the sql server url
String url = "jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE";
// get the sql server database connection
connection = DriverManager.getConnection(url,"THE_USER", "THE_PASSWORD");
// now do whatever you want to do with the connection
// ...
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(2);
}
}
}
What i need to explain is there is very good technology called " Persistence " is better than JDBC and is more than brilliant and easy to use .
The problem is that your SQL server is either
not installed,
not running or
not accepting TCP/IP connections.
Particularly the last one is nasty, as I remember that some versions of SQL Server have not configured the TCP/IP connector to run by default.
Well first and foremost we need to see your code. Second looking at the error message the database is A)not running
B) on a different port
or C) the code is incorrect.

Categories