ConnectionException using Java MariaDB Connector - java

i want to establish a connection between my Java Program and my Database created with MariaDB on a different server (not localhost). Everything is fine with the ServerIP, Databasename, User and Password.
But i get this error:
Exception in thread "main" java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=MY_SERVER_IP)(port=3306)(type=master) : Connection timed out: connect
at org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:136)
at org.mariadb.jdbc.internal.SQLExceptionMapper.throwException(SQLExceptionMapper.java:106)
at org.mariadb.jdbc.Driver.connect(Driver.java:106)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MySQL_Tester.LoadDriver.main(LoadDriver.java:11)
Caused by: org.mariadb.jdbc.internal.common.QueryException: Could not connect to address= (host=MY_SERVER_IP)(port=3306)(type=master) : Connection timed out: connect
at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connectWithoutProxy(MySQLProtocol.java:629)
at org.mariadb.jdbc.internal.common.Utils.retrieveProxy(Utils.java:541)
at org.mariadb.jdbc.Driver.connect(Driver.java:101)
... 3 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:288)
at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connectWithoutProxy(MySQLProtocol.java:624)
... 5 more
My Code:
package MySQL_Tester;
import java.sql.*;
public class LoadDriver {
static Connection sqlHandler = null;
public static void main(String[]args) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Class.forName("org.mariadb.jdbc.Driver").newInstance();
sqlHandler=DriverManager.getConnection("jdbc:mysql://MY_SERVER_IP/DATABASENAME?user=USERNAME&password=PASSWORD");
}}
All the versions i use:
MariaDB: 10.1.44
MariaDB-Java-Connector: mariadb-java-client.1.2.0.jar
Java Version: Version 8 Update 161
Eclipse Version: 2019-03 (4.11.0)

You must update your connection string with the real values.

Thank you for your answer davidbuzatto but the problem was easier to solve than i expected.
Here is a Guide how i solved this problem for me:
Step 1: Check your MySQL-Config File with (nano /etc/mysql/my.cnf in Ubuntu)
replacing bind-adress: 172.0.0.1 with bind-adress: 0.0.0.0
When this doesnt help... For me it didnt help....
Step 2: Check the Firewall Settings on your Server and allow the access to the MySQL Server
When this also doesnt help... For me it didnt....
Step 3: Grant Access to your MySQL-Database
Use this MySQL-Query: GRANT ALL ON yourDatabasename.* to yourUser#yourIP identified by 'passwordOfTheUser';

Related

Detach and attach RServe connection: Cannot connect

I am trying to detach and then attach an RServe session from Java. Connecting to RServe works flawlessly, but attaching causes an exception.
package com.company.korana.r_interface;
import org.rosuda.REngine.Rserve.RConnection;
import com.company.korana.config.Config$;;
public class RToast {
public static void main(String[] args) throws Exception {
RConnection rConnection = new RConnection(Config$.MODULE$.rserveHost(), Config$.MODULE$.rservePort());
rConnection.assign("testVariable", "hello from java");
rConnection.eval("print('Hi R console from java')"); // <--- this is visible in the R console
org.rosuda.REngine.Rserve.RSession sessionHandle = rConnection.detach();
rConnection = sessionHandle.attach(); // <---- this throws
System.out.println(rConnection.eval("testVariable").asString());
}
}
Exception in thread "main" org.rosuda.REngine.Rserve.RserveException:
Cannot connect: Connection refused: connect at
org.rosuda.REngine.Rserve.RConnection.(RConnection.java:90) at
org.rosuda.REngine.Rserve.RConnection.(RConnection.java:66) at
org.rosuda.REngine.Rserve.RSession.attach(RSession.java:36) at
com.company.korana.r_interface.RToast.main(RToast.java:16) Caused by:
java.net.ConnectException: Connection refused: connect at
java.net.DualStackPlainSocketImpl.connect0(Native Method) at
java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at
java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at
java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at
java.net.AbstractPlainSocketImpl.connect(Unknown Source) at
java.net.PlainSocketImpl.connect(Unknown Source) at
java.net.SocksSocketImpl.connect(Unknown Source) at
java.net.Socket.connect(Unknown Source) at
java.net.Socket.connect(Unknown Source) at
java.net.Socket.(Unknown Source) at
java.net.Socket.(Unknown Source) at
org.rosuda.REngine.Rserve.RConnection.(RConnection.java:85) ...
3 more
The Rserve is running on localhost with the default port.
EDIT:
I'm using the following maven artifact:
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>Rserve</artifactId>
<version>1.8.1</version>
</dependency>
After inspection of the sources, it turns out that Rserve sessions, when detached, listen on a randomly allocated port.
Rserv.c, line 1781
while ((port = (((int) random()) & 0x7fff)+32768)>65000) {};
So the port that needs to be reachable between the client and Rserve is something between 32768 and 65000. In my case, this didn't work because I was using docker.
EDIT: I built a solution to use Rserve with docker without opening a huge range of ports (which does not work nicely with docker). The snipper below replaces my install.packages("Rserve"):
RUN wget https://www.rforge.net/src/contrib/Rserve_1.8-5.tar.gz
RUN tar -xf Rserve_1.8-5.tar.gz
RUN rm Rserve_1.8-5.tar.gz
RUN sed -i '1763s/.*/\t\tint port = 53000;/' /Rserve/src/Rserv.c
RUN sed -i '1781s/.*//' /Rserve/src/Rserv.c
RUN sed -i '1793s/.*/\t\tif (port>53100) {/' /Rserve/src/Rserv.c
RUN tar -czvf Rserve_1.8-5.tar.gz /Rserve
RUN R CMD INSTALL Rserve_1.8-5.tar.gz
This modifies the Rserve sources so that ports are taken in the range [53000, 53100[ which is sufficient in my case.

How to connect MYSQL database From a Admin system using Eclipse Juno?

I have tried to create a Mysql connection between Server & Client System. My admin system ip address is (192.168.1.5). And I have a database "test". I downloaded the mysql-connector-java-5.1.29 and added with Java Buildpath.
The ScreenShot is shown below:
Whenever i tried to test the connection, it shows the error like this..
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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:358)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2535)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2320)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:214)
at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:73)
at org.eclipse.datatools.enablement.internal.mysql.connection.JDBCMySQLConnectionFactory.createConnection(JDBCMySQLConnectionFactory.java:28)
at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:308)
... 22 more
I have attached the Screenshot too..
Please help me to solve this issue.. Thanks in advace..
Change your url to : jdbc:mysql://192.168.1.5:3306/
And your database name to atrics instead of test.
HTH.
Check out http://itsolutionsforall.com/eclipse_sql.php
Updated Solution:
MySQL Connectivity between Client/Server Network using Eclipse:
•Download the Latest MySQL Software from the http://www.mysql.com/
•Install it to your Administrator System.
•Download the MySQL Connector from the following:
http://dev.mysql.com/downloads/connector/j/
(Only download the .zip or .rar file)
•Extract the file in a specific location. Example,
D:\eclipse\mysql-connector-java-5.1.29
•Now Login to your Admin System and Open “MySQL Command Line Client” from the program list.
•Enter the password. (Which is you provided during the installation of MYSQL. By default “root”
•If you get any kind of error means you need to check the password or reinstall the MySQL.
•If you succeeded means, you will get the mysql command in the next line like this
mysql>
• To view the current databases.
mysql>show databases;
•To use a new database,
MySQL>use database_name;
example:
mysql >use sample;
•To view the tables,
mysql>show tables;
Creating a New User:
• Use the following query to create a new user.
mysql>create user ‘JsAtrics’#’192.168.1.6’ identified by ’root’;
• To give grant privileges,
mysql>grant all privileges on . to ‘JsAtrics’#’192.168.1.6’;
• Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
mysql>flush privileges;
mysql>select user();
mysql>select current_user();
mysql> select host,user,password,Grant_priv,Super_priv FROM mysql.user;
•A list of users will be open. Check your username and check the privileges as “Y”.
•If it is “N”, execute the following,
UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='JsAtrics';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'JsAtrics'#'192.168.1.6';
•Now again check the following:
mysql> select host,user,password,Grant_priv,Super_priv FROM mysql.user;
•If you see “Y” in privileges means that’s all. You finished.
Test Connection in Eclipse:
•Open Eclipse and Click WindowOpen Perspective  other  Database Development
•Now the Data Source Explorer open in the left or in the bottom.
•Right click on the Database Connections New  MySQLNext
•Now a new window will open.
•In the Connection Window Click New Driver (Which was placed in the right of Driver list)
•Select “MySQL JDBC Driver 5.1” and move to the “Jar list” tab and click clear all. And Click “Add Jar” and update the new connector which you downloaded early and extracted in the location D:\eclipse\mysql-connector-java-5.1.29
•After adding the jar file, move to properties tab. In that
Connection url: jdbc:mysql://192.168.1.5:3306/sample
Database Name: sample
Driver Class: com.mysql.jdbc.Driver
Password: root
User ID : JsAtrics
•And Click “OK” Now Click “Save Password”.
•Now we can check the connection. So Click “Test Connection”
•It will displays a message “Ping Succeeded”.
•Click “Ok” and Click “Finish”.
•Now the database connected to your client system..!!!
Note: If you found any errors in this, read the above steps once again and follow the above steps once again.

RMI exception. Cannot connect to the server

I'm trying to connect a client and remote server through RMI and I get this error message:
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.net.MalformedURLException: unknown protocol: c
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at RmiClient.getMessage(RmiClient.java:11)
at RmiClient.main(RmiClient.java:26)
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at sun.rmi.server.LoaderHandler.pathToURLs(Unknown Source)
at sun.rmi.server.LoaderHandler.getDefaultCodebaseURLs(Unknown Source)
at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source)
at java.rmi.server.RMIClassLoader.loadClass(Unknown Source)
at sun.rmi.server.MarshalInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
... 4 more
The following may be the cause of the problem:
The server has this line:
Naming.rebind("rmi://172.31.24.66:1099/RmiServer", obj);
which refers to the private IP address of an EC2 instance. The client on the other hand has this line - which is line 11 from above:
RmiServerIntf obj = (RmiServerIntf)Naming.lookup("rmi://54.229.66.114:1099/RmiServer");
which is the public IP of that instance. Also, on the client side I set the codebase property as:
System.setProperty("java.rmi.server.codebase", "C:\\Users\\spacitron\\Projects\\RMIClient\\bin");
If I try to add the same line on the server I get a "permission denied" error.
A codebase is a list of URLs. What you have specified is a file name, not a URL.
It is also a rather useless filename, unless the client is on the same host as the server, in which case it is difficult to see why you're using the codebase feature at all. Even if you have a shared drive it is just as easy to specify directories in the client's CLASSPATH as it is to use java.rmi.server.codebase. A codebase URL needs to be usable by the recipient. That's why they are almost always HTTP URLs, or maybe FTP URLs.
try down grading your jdk7 below update 21/jdk6 below update 45.
compile and test again of this work problem is java.rmi.server.useCodebaseOnly system property
please check below link for solution for both version of jdk's
http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/relnotes.html

Linux bash: java.net.ConnectException: Connection refused

I have a java program Read which reads file from an URL looks like this:
file://myhost/system.log
On Windows I use this command:
java Read "file://myhost/system.log"
and it works flawlessly.
But on Linux when I try to use the same command it gives me this error:
Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.ftp.impl.FtpClient.doConnect(Unknown Source)
at sun.net.ftp.impl.FtpClient.tryConnect(Unknown Source)
at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
at sun.net.ftp.impl.FtpClient.connect(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at read.readInput(ReadHTML.java:53)
at read.readInput.main(ReadHTML.java:76)
Any ideas?
UPDATE:
I think I found the problem:
myhost is not mounted on the Linux machine so that it cannot connect to it by using file://...
Thanks for all the answers, guys!
This is because your program (by virtue of using a file URL on a Linux OS) is attempting to open an FTP connection to the host specified in the URL. The establishment of the FTP connection fails.
This could be due to a variety of issues. Try connecting with ftp directly:
ftp myhost
And I'm guessing it'd fail as well. Do you have an FTP server installed & running?
One of the possible reason can be that hosts.allow does not have entry of your machine.
I suspect you just need a third /. The correct syntax for file uris is file://[path] but [path] should begin with a /.

Application Not Debugging in Eclipse

I am trying to run my Java application in debug mode in Eclipse, but it's not working. It was fine until yesterday when I could properly debug my application. I am able to run my application on the server ( Oracle OC4J Standalone Server 10.1.3) properly. Only while debugging, it continues for a long time and then shows a server time out error. I tried increasing the Server time out, but was of no use.
I also tried removing the Server and creating it again and restarting Eclipse. Is there something I could do about this?
Edit: Strack trace on trying to Run it as Remote Java Application
Exception Stack tace:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.eclipse.jdi.internal.connect.SocketTransportService$2.run(SocketTransportService.java:136)
at java.lang.Thread.run(Unknown Source)
There is an issue with OC4J's debug mode enabling. Sometimes I've noticed that after enabling it (via opmn.xml) it will work for a while then stop working. What I've found to actually work all the time is editing the /bin/oc4j.cmd file and adding the line:
set JVMARGS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=9009,suspend=y,server=y
then start a Remote debugging session from Eclipse on the port 9009

Categories

Resources