io error:network adapter could not establish the connection - java

I am developing a mini project using Java and Oracle XE database.
I was able to connect to database, insert, update and run all other queries from my personal computer, but when I tried to run the same program on my college computer (which is networked) it did not run and gave me the following exception message:
io error:network adapter could not establish the connection
I tried researching the error message on the internet and found that it must be a firewall or port number problem, but I am unable to sort it. How can I fix this error?
Here is the connection code that I used:
public void connect()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","system","system");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
SimpleDateFormat sd=new SimpleDateFormat("dd-MM-yy");
DefaultTableModel table_allrec;
PreparedStatement pstmt,pstmt1;
Connection conn;
ResultSet rs,rs1;

This error is most likely caused by one of these factors:
You are using the wrong URL
The wrong port number or IP address (or DNS host name) was used
The listener is not configured properly
The listener process (service) is not running. You can re-start it with the "lsnrctl start" command or on Windows by starting the listener service.

Please make sure, you are entering correct username, password, hostname, PORT, SID while creating connection in SQL Developer
On the machine where DB is installed,
go to this folder C:\app{{Username_machine}}\product\11.2.0\dbhome_1\NETWORK\ADMIN
P.S. : {{Username_machine}} : This is will differ from machine to machine
Open 2 files inside this listener.ora AND tnsnames.ora
Check hostname, PORT, SID mentioned in the files MUST match with the one you are trying to use in STEP 1.
If any of the value doesn't match, Use the values present in the 2 Files for creating connection in SQL Developer
Try to connect with updated values in case mismatch of values were found
If you are still not able to connect
press windows + R, and type services.msc -- this is will open Services running on your machine
restart all the services that are starting with oracle
Try to reconnect again on SQL Developer
If it fails again,
Open CMD in administrative mode,
Check if Your TNS Listener is up by typing the following command
lsnrctl status
The above command will give error if the Listener is not up
Start the Listener, by typing the following command,
lsnrctl start
In case you are having problems in starting your listener then try starting “OracleOraDb11g_home1TNSListener” windows service. For that open your Run Command and write services.msc this will open your service panel. Here search for the same and right click to start it.
Doing the above steps, will resolve your Problem. Thanks !

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.
:)

Check if ftp service is running

I want to check if a server or ip has ftp service running before connect to it. I have the next code, however if the ftp service is not active I get: "Error: failed to connect to localhost...NNREFUSED (connection refused)"
I want to capture this error to show message if service is offline or continue if is online.
ftpClient.connect(localhost, 21);
Boolean temp = FTPReply.isPositiveCompletion(ftpClient.getReplyCode());
Log.d(TAG,"respuesta server: " + Boolean.toString(temp));
The main problem is that in line 1, logcat shows the error and the lines after it are not executed.
I don't know if with FTPClient is possible, if not, something similar to make a "telnet localhost 21" manually for example.
Note: the error is not a exception.
I want to check if a server or IP has FTP service running before [trying to] connect to it
No you don't. The best way to tell whether it's running is to try to connect to it. If you get a connect exception, it isn't running. No other scheme you can devise will work as well.

How do I connect to Derby which is included with WebLogic?

I'm trying to add the Derby included with WebLogic as a data source. I'm running WebLogic 10.3.5. I didn't see a Derby flag in the setDomainEnv.cmd, so I added the client driver to the CLASSPATH:
set CLASSPATH=%PRE_CLASSPATH%;%WEBLOGIC_CLASSPATH%;%POST_CLASSPATH%;%WLP_POST_CLASSPATH%;%WL_HOME%\common\derby\lib\derbyclient.jar
in setDomainEnv.cmd. Now when I try to activate my data source in WebLogic I get:
An error occurred during activation of changes, please see the log for
details. Message icon - Error weblogic.application.ModuleException:
Message icon - Error java.net.ConnectException : Error connecting to
server localhost on port 1527 with message Connection refused:
connect.
What else do I need to do to get this to work?
I believe your local derby is not up yet. To check this, issue:
netstat -an | findstr 1527.
See if it returns anything. If nothing is returned, try to start the Derby Network Service manually. Go to <WLS_HOME>\common\derby\bin and click the startNetworkServer.cmd.
Double check with netstat again. If it's there, you can try to add to the datasource.
Note: this only covers manually start the Derby network service (not automatially start it upon weblogic start, which a bit tricky).
Either whatever is supposed to be running on that port isn't running or there is a firewall issue.

connect database to another computer

I have two computers on one network. I have entered the ip of another computer, it works fine in the browser, but when using this ip on java I found the database connected to my localhost database not from the other computer!
my code jdbc
public Connection MakeConnect() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:http://192.168.1.109:5560/isqlplus", "school",
"sch"); // first : user(hr) second pass(hr) .!
return connection ; // return connetion of database
I think your JDBC connection string may be in a wrong format. You should provide this in the following way:
jdbc:oracle:thin:#[HOST][:PORT]:SID
jdbc:oracle:thin:#//[HOST][:PORT]/SERVICE
Instead it looks you specified ISQLPLus link. I haven't tested it but my guts say me it is the reason. I think in your case it should be something like that:
jdbc:oracle:thin:#192.168.1.109/SERVICEorSID
And you have to find the service name by yourself. In a generic case it is a name of the database. By default it is usually set to ORCL but possibly you set it to another name.
Make sure you read a following document: http://www.orafaq.com/wiki/JDBC
If the connection succeeds without error and if this 192.168.1.109 ip address is not of your local machine then it must connect to some other machine.
What made you think it's connecting to the local DB instance?
Check firewall settings of the other computer. Sometimes it may not allows for the external connections
If you want to allow a specific client ip-address (for example: 192.168.1.4) to access the mysql database running on a server, you should execute the following command on the server that is running the mysql database.
$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON *.* to root#'192.168.1.4' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;
Also, update firewall rules to make sure port# 3306 is open on the server that is running the mysql database.

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