Connecting to external database through telnet - java

I have a java program that connects to a MS SQL database. The program works perfectly when running through eclipse however I get an error when I run it through AIX:
java.sql.SQLException: Network error IOException: A remote host refused an attempted connect operation.
I can successfully ping the server but am not able to telnet into the server. I am also not able to telnet from my windows desktop.
I am using jtds to connect:
String connectionString = "jdbc:jtds:sqlserver://"+dropez_ip_address+"/"+dropez_db_name;
ResultSet rs = null;
Statement stmt = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionString, dropez_db_username, dropez_db_password);
stmt = conn.createStatement();
}catch(Exception e){}
Here is some documentation from jTDS regarding the issue, but I am still not able to resolve the issue.
Why do I get java.sql.SQLException: "Network error IOException: Connection refused: connect" when trying to get a connection?
The "Connection refused" exception is thrown by jTDS when it is unable to connect to the server. There may be a number of reasons why this could happen:
- The server name is misspelled or the port number is incorrect.
- SQL Server is not configured to use TCP/IP. Either enable TCP/IP from SQL Server's Network Utility app or have jTDS connect via named pipes (see the URL format for information on how to do this).
- There is a firewall blocking port 1433 on the server.
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. If you can't figure out why, ask your network administrator for help.

If you can't telnet on port 1433, you are blocked by a firewall somewhere in the middle between your machine and the server. That's not a java related problem.
May it be that when you say "it runs perfectly under eclipse but not AIX" you are taking about 2 different computers ? If so, the one with eclipse is not firewalled, the one where you deploy your app is blocked.
But again, nothing to do with java. It's a level 3 error (TCP layer) of TCP-IP model.
Regards,
Stéphane

Your SQL Server database probably doesn't have the TCP/IP protocol enabled, to enable it:
From the Microsoft SQL Server 2005 -> Configuration Tools, open the 'Microsoft SQL Server Configuration Manager'.
Expand ‘SQL Server 2005 Network Configuration’, and then click ‘Protocols for ’.
Right-click ‘TCP/IP’ and then click ‘Enable’. The icon for the protocol will change to show that the protocol is enabled.
For SQL Server 2008:

Related

Unable to connect to a Oracle db from Linux machine in java

I have registered a Linux machine as runner in my gitlab and tried to execute my scripts there.
While executing I got an error for database connection
Issue : java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
When I connect from my local machine (Windows 7 & 10) it is working fine.
Attempts:
Tried to connect using jtds but I'm getting some other unknown issue.
Checked few other answers and they have provided that it could be because of firewall enabled but since we did not have full access to that db server , I could not disable and check.(Could this be the issue?)
My Connection code :
ConURL = "jdbc:oracle:thin:#" + ConstructSQLConnectionURL(host,sid,port);
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(ConURL, dbUserName, dbPassword);
I am stuck with this issue and blocked from executing pipeline.
Can anyone let me know what could be the possible reason for this and help me with this?
Thanks in advance
It appears to be a network connectivity issue. I'd start with local firewalls on each system. Can you ping the database from the client? If not, look for network routing or firewall blockers. Also look at host-based firewalls, or confirm with the DBA whether there is something like valid node checking in place to limit database clients.

Unable to connect Java application to SQL Server database

I am trying to write a Java desktop app that can connect to my database made with Microsoft SQL Server Manager to allow me to view and update it. But, I am having trouble getting the connection to work. I've read through a bunch of tutorials and threads here on Stack Exchange of similar problems, and I'm not sure what I'm doing wrong.
The server is called "SQLEXPRESS" using Windows authentication. I downloaded the JDBC driver found here: https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 installed it in NetBeans by going to "Services-Databases(right click)-New Connection-Add", but I also added it as a library in my project.
When I try this code, I get the exception that the TCP/IP connection failed either because the server isn't running or port 1433 is locked:
try{
String
URL="jdbc:sqlserver://sqlexpress:1433;DatabaseName=GreenhouseManagement";
Connection conn = DriverManager.getConnection(URL,"","");
System.out.println("connected");
}catch(Exception e){
System.out.println("Oops\n"+e);
}
What do I need to change to fix this?
You might need to reconfigure your connection string into this format.
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE
HOST in this case is most likely to be "localhost" since you are connecting on a local machine.
DATABASE will be the name of your database
Reference: http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver

java and access error when connecting to the database

Today I encountered the following problem. When connecting to a database, the following error.Appeal to the remote database by connecting through vpn.
Added necessary firewall ports and even tried to disconnect its(did not help)
Added record to $JAVA_HOME/jre/lib/security/java.policy
grant codeBase "file:/-" {
permission java.security.AllPermission;
};(did not help)
If trying to connect with same data through sql management studio, it works and there is access. from the development environment no connection
Example(string connection = jdbc:sqlserver://DEVMSSQL14.test.com:1433;SCHEMA=test;DATABASENAME=test, sql managment studia = DEVMSSQL14.test.com,1433)
on another computer it works without problems, the same version of the project
It remains only to "Permission denied: connect".Maybe somebody faced with similar
Error:
The TCP/IP connection to the host DEVMSSQL14.test.com, port 1433 has failed. Error: "Permission denied: 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.".
In the error message, you have shown like DEVMSSQL14.test.com, 1433 has failed.
I believe, it is correct. I dont think. this host name is correct. which says "TEST" as domain
Open the cmd, try to ping DEVMSSQL14.test.com
Success case :
If you are able to ping, then make sure the services are running for sql in services
Failure case:
Get the right host name to establish the connection
When i tried, i got this message, because i may be out of network.
Pinging DEVMSSQL14.test.com [208.64.121.161] with 32 bytes of data:
Request timed out.
Request timed out.
Try setting a System Property while running the code.
The catch is to introduce this line in code: System.setProperty("java.net.preferIPv4Stack", "true");
Or to pass it in VM option: -Djava.net.preferIPv4Stack=true

The Network Adapter could not establish the connection when connecting with Oracle DB

When trying to connect with a remote Oracle database via JDBC I receive the following exception:
java.sql.SQLRecoverableException: IO-fout: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:419)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:536)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:228)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(DriverManager.java:322)
at java.sql.DriverManager.getConnection(DriverManager.java:358)
The following is my set-up:
Database: Oracle 10g Release 2 Standard Edition
JDBC library: ojdbc6.jar
JDBC driver: oracle.jdbc.driver.OracleDriver
JDBC URL: jdbc:oracle:thin:#9.2.2.2:1521:ORCL where ORCL is database's SID
JDBC User/pwd: Correct username / password
Strange about this problem is that the connection works just fine when I work from work. When I try to connect however from home via an AT&T VPN connection, it doesn't work.
I have confirmed that I can reach the IP address and have also telnetted the ip on port 1521, which works just fine. Connecting to the datasource from a local WebLogic Application Server also works alright. Furthermore, when trying to connect to the database via sqldeveloper I can also reach the database.
I need to reach the database however from a standalone application (for testing purposes). Does anyone have an idea why this problem occurs? And whether there are alternatives for connecting to a remote Oracle Database, alternatives which sqldeveloper and weblogic perhaps use?
Here's an excerpt of the code attempting to connect with the database:
public static void main(String args[]) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#9.2.2.2:1521:ORCL", "user", "pwd");
}
When a client connects to an Oracle server, it first connnects to the Oracle listener service. It often redirects the client to another port. So the client has to open another connection on a different port, which is blocked by the firewall.
So you might in fact have encountered a firewall problem due to Oracle port redirection. It should be possible to diagnose it with a network monitor on the client machine or with the firewall management software on the firewall.
If it is on a Linux box, I would suggest you add the database IP name and IP resolution to the /etc/hosts.
I have the same error and when we do the above, it works fine.
Take a look at this post on Java Ranch:
http://www.coderanch.com/t/300287/JDBC/java/Io-Exception-Network-Adapter-could
"The solution for my "Io exception: The Network Adapter could not establish the connection" exception was to replace the IP of the database server to the DNS name."
I had similar problem before. But this was resolved when I started using hostname instead of IP address in my connection string.

Not able to connect local instance of SQL Server 2008 in a Java program

I am using sqljdbc4.jar in the Build path of eclipse project.
I am trying to connect to SQL Server 2008 in a java program
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=sis;user=sample;password=sample;";
connection = DriverManager.getConnection(connectionUrl);
But I am getting the following error.
The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect
I tried the following
Firewall off
Enabled TCP/IP in SQL Server configuration manager --> CLient protocols
SQL Server browser service is also running
Please let me know what other things should I try.
Thanks
Firewall off which way, this way??? open/enable port localhost:1433
run cmd and paste
netsh firewall set portopening protocol = TCP port = 1433
name = SQLPort mode = ENABLE scope = SUBNET profile = CURRENT
2/ change Sql Server authentication from
Windows authe... to Sql Server and Windows authentification (Mixed mode)
3) your version is SQL Server 2008 or SQL Server 2008 Express, if Express, then search on downloaded package (contains excelent help inc examples) how to connect Express on Localhost, there is difference localhost\sqlexpress
here and here and here
4) no idea how to connected, there are TCP/IP and Names Pipes too
EDIT:
5) in Management Studio you have to add new User (SQL autenthification) and assign to the decision Database (whatever or Model too) is there ??? aren't you

Categories

Resources