Please help me.I have installed Oracle 11.2.0 g on Windows 7 (32 bit) and I'm trying to connect database with jdk 1.7
I get an error saying
---exception
java.sql.SQLRecoverableException:IO Error: The Network Adapter could not establish the connection
ORACLE_HOME=E:\app\OraDhanya\product\11.2.0\dbhome_1
CLASSPATH=E:\app\OraDhanya\product\11.2.0\dbhome_1\jdbc\lib\*;C:\Program Files\Java\jdk1.7.0_03\bin
Path=E:\app\OraDhanya\product\11.2.0\dbhome_1\BIN;C:\Program Files\Java\jdk1.7.0_03\bin;
Global Database Name =orcldhamanoj.168.1.100
SID=orcldhaman
CODE:
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connecting to Database");
Connection cn=DriverManager.getConnection("jdbc:oracle:thin:#orcldhamanoj:1521:orcldhaman","SCOTT","Tiger1");
System.out.println("Connected to Database");
Statement st=cn.createStatement();
st.executeUpdate("create table User(UserID number(3), UserName varchar2(20));");
System.out.println("Table Created");
st.close();
cn.close();
}
catch (SQLException e)
{
System.out.println("exception"+e);
}
The error has nothing to do with JDBC, it's a low level error that tells you the networking layer cannot make a connection to the machine your Oracle server is running on. Causes can be multiple:
Wrong connection parameters (ip / host name/ port). This is probably the most frequent cause. Check the ip / hostname using ping, and the port using "telnet [ip/hostname] port" command;
Something wrong with your network like a cable problem or wrong network settings.
In the DriverManager.getConnection method, make sure you specify the IP address of the machine on which Oracle is running -- or localhost if its your current machine.
DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcldhaman","SCOTT","Tiger1");
// ^^^^^^^^^ <-- Or this could be an IP address
I would suggest the following may be causing this kind of error:
(obvious) IP address is incorrect - try PING
The port is not open, or is blocked by a firewall - try TELNET
The DB listener is not running or is binding to a different network interface - again, TELNET should confirm this (also use Oracle client tools to connect)
No local ports are available for the out-going connection (unlikely) - only if you're making thousands of connections, or creating hundreds of new connections every minute.
Related
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.
:)
My java program is working fine when I use localhot instead of IP address, but getting error when I connect using IP address. (I have to use IP address to connect, because it is a client server application).
I have also changed this /etc/mysql/mysql.conf.d/mysqld.cnf file bind address from 127.0.0.1 to 0.0.0.0, But again I get the error message like -
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.
My code is:-
try{
Connection con;
Statement st;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(
"jdbc:mysql://172.16.16.185:3306/itcentre?
verifyServerCertificate=false&useSSL=true","root","");
st=con.createStatement();
ResultSet rs=st.executeQuery("select * from teacher");
while(rs.next())
{
System.out.println(rs.getString(2));
}
rs.close();
st.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
Java can definitely establish an SSL connection without a client validating the certificate chain of the server.
The classes that are establishing the connection (javax.net.ssl classes) would normally treat the unverified server certificate with suspicion and would fail the handshake.
But they provide a way for the user's of those classes to in effect say "It's ok if the server's certificate doesn't validate, go ahead and establish the connection".
That is what's happening when you say verifyServerCertificate=false.
Thanks to everyone to help me. After struggling to many other issues, finally I have the luck with this solution. I logged into my mysql database using phpmyadmin and then
User Accounts ---> login information ---> and changed Host name from localhost to any host.
See the screen shot -
Below is my connection to the server
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:81/user", "root", "");
JOptionPane.showMessageDialog(null, "Connection Established");
return conn;
}catch (Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
The actual connection can be found on this line:
DriverManager.getConnection("jdbc:mysql://localhost:81/user", "root", "");
am using port 81 for mysql database because port 80 is being used by another program.
Please I need help solving this problem. And thanks in advance.
am using port 81 for mysql database because port 80 is being used by
another program.
The default TCP/IP port on which the MySQL server is listening is 3306 not 80.
MySQL defaults to port 3306 unless you specify another one in the "my.cnf" config file . Then it is very likely you are using the default 3306 port.
Try using the default port unless you changed it.
Try Below steps to find possible reason
1.Manually Ping and connect to Mysql Database from command line and check if you are able to connect or not.
2.Handle Java Exception in your code and print Exception Object.
3.Check if your server /Database is up and running.
4.Also if you have a MYSQl Client first try to connect from front end ,if you are able to connect then we you need to look into the JDBC Api Code and print the Exception and put System.out after each line of code.
I am trying to connect to MySQL from JDBC via localhost. But the connection fails. In the exception, I see that JDBC is trying to connect to 127.0.0.1
String connectionString = "";
try {
loadProperties();
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connectionString = "jdbc:mysql://" + properties.getProperty("host") + "/" + properties.getProperty
("database") + "?user=" + properties.getProperty("user") + "&password=" + properties
.getProperty
("password");
connect = DriverManager
.getConnection(connectionString);
logger.debug("Connected to " + properties.getProperty("host"));
} catch (Exception e) {
logger.error("Database Connection failed with connection string - " + connectionString,e);
}
From the log:
Database Connection failed with connection string - jdbc:mysql://localhost/testdb?user=testuser&password=testpass
java.sql.SQLException: Access denied for user 'testuser'#'127.0.0.1' (using password: YES)
Why is it replacing localhost with 127.0.0.1? I have configured login only for localhost.
I stumbled across this question when encountering the same issue.
To answer the question "Why is it replacing localhost with 127.0.0.1?":
From the MySQL docs, using localhost in your connection URL implies that you want to connect to a socket. Using 127.0.0.1 implies that you want to connect through TCP/IP.
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. ... To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1
According to this answer, it appears that by default JDBC only supports TCP/IP connections, at least for some Java versions. Original source: http://lists.mysql.com/java/8749:
Java itself doesn't support unix domain sockets
So, I'd guess that since JDBC only connects through TCP/IP, it converts localhost to 127.0.0.1 internally.
To solve the problem in my case:
I granted permission in MySQL for user#127.0.0.1
I changed localhost to 127.0.0.1 in my connection URL.
The IP address 127.0.0.1 is a special purpose address reserved for use on each computer. 127.0.0.1 is conventionally a computer's loopback address.
Network software and utilities can use 127.0.0.1 to access a local computer's TCP/IP network resources. Messages sent to loopback IP addresses like 127.0.0.1 do not reach outside to the local area network (LAN) but instead are automatically re-routed by the computer's own network adapter back to the receiving end of the TCP/IP stack. In simple words, localhost can also be referred as 127.0.0.1. There is a problem with MySql access privileges. This link would help you resolve it
I keep getting this error when i'm trying to connect to my MySQL database:
exception
javax.servlet.ServletException: JDBC Problem:
root cause
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.
root cause
java.net.ConnectException: Connection timed out: connect
here's some code:
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://mysql11.000webhost.com/database_name","user_name","password");
stmt = con.createStatement();
rs = stmt.executeQuery("INSERT into emailadresses('email') values ('"+email+"')");
} catch (ClassNotFoundException ex) {
Logger.getLogger(EmailServlet.class.getName()).log(Level.SEVERE, null, ex);
}catch(SQLException e){
throw new ServletException("JDBC Problem: ", e);
}
edit: I have the mysql-connector-java jar file in my libraries
EDIT: FOUND THE PROBLEM
I was looking for the answer on the site where I host my database and found this:
JDBC/ODBC is not supported here
that s*cks...
"Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port)". - From Oracle Docs
Take a look into your connection string, be sure that the database, username and the password are correctly typed.
Another cause may be that your database server is not up.
Yes its not a classpath problem, its a networking problem. For some reason no network connection can be established to the given host from your client machine. So yeah, could be lots of things.
server is actually not running!
host/IP wrong
firewall (your side)
firewall (other side)
whatever else networking problem there might be
Definitely a networking problem. Note that your tcp connection is NOT refused, instead it incurs in a timeout error. In my experience that behaviour could be caused by a TCP handshaking not correctly finalized (eg. a firewall dropping SYN packets).
Try connecting that database from any database client(like sqlyog) from machine you are running your application. If connection fails from client also, then surely it's a network issue.