This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 6 years ago.
I'm developing a Java application that (among other things) queries a remote database. I'm having trouble getting a connection to the database.
My code is:
private static String MYSQL_URL = "jdbc:mysql://db644874220.db.1and1.com";
private static String DB_NAME = "db644874220";
private static Connection CONNECTION;
static {
String dbUrl = MYSQL_URL + "/" + DB_NAME;
try {
Class.forName("com.mysql.jdbc.Driver");
CONNECTION = DriverManager.getConnection(dbUrl, MYSQL_USERNAME, MYSQL_PASSWORD);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
The error I get is:
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:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.joshuaevanslowell.IO.<clinit>(IO.java:41)
at com.joshuaevanslowell.Speculator.<init>(Speculator.java:46)
at com.joshuaevanslowell.Speculator.main(Speculator.java:32)
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 com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 17 more
I've seen some other questions dealing with this, but they generally assume that the database is on localhost, which mine isn't. I'm using 1&1 web hosting, and I can work with the database through my browser through phpMyAdmin. The information it gives about the server is
Server: db644874220.db.1and1.com via TCP/IP
Server type: MySQL
Server version: 5.5.50-0+deb7u2-log - (Debian)
Protocol version: 10
User: dbo644874220#10.72.2.9
Server charset: UTF-8 Unicode (utf8)
I feel like I'm doing something horribly wrong, and I just can't make head or tail of this.
If you have telnet client client installed assuming 3306 is the mysql port, please try
telnet ipaddress 3306.
I fear you may not have direct connectivity to mysql server. To enable this you may need to edit the configuration as answered in similar questions.
Also, please do close the DB connection or rather use a connection pool. Having too many open connections(above the configured limit) may cause similar issues.
my guess is that the db644874220.db.1and1.com host is not known from the location where you run your Java code
try to do ping db644874220.db.1and1.com from the machine where you run the Java code or telnet db644874220.db.1and1.com 3306. If neither return a result it means that you don't have a way to access that machine via its name.
Either use the IP instead if you know it or add a line in your hosts file
[IP address] db644874220.db.1and1.com
Related
I've been trying to establish a very basic connection between two computers in my LAN. I've viewed several code snippets and went from there, but things seem to fail at the very start: creating a socket.
I've tried it with both connecting to the computer I'm executing the code from (using the hostnames "Jeroen-DESKTOP" and "Localhost" and the localhost IP "127.0.0.1"), and connecting to my laptop (using "Jeroen-LAPTOP"). I've tried the ports 6666 and 7598and opened all TCP connections to it in my firewall settings.
The code I'm using is very basic:
socket = new Socket("Jeroen-LAPTOP", 6666);
System.out.println(socket.getPort());
With the error being:
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.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at domain.Client.<init>(Client.java:13)
Without knowing exactly what you are doing -
Assuming you are opening a socket on Machine A and trying to connect to it using Machine B, then the typical scenario in Java would be.
Machine A:
ServerSocket server = new ServerSocket(4444);
Socket connection = server.Accept();
Machine B:
Socket sock = new Socket(MACHINE-A-IP, 4444);
This would require exception handling etc to allow it to work. You can see examples here;
http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
Regarding your comment - yes you can run them both locally.
I'm trying to connect to an external mySQL DB through a Java program. When I attempt to connect, I obtain the following exception:
Exception in thread "main" com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:605)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1078)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:48)
This is only for this external server. I was able to connect to a local server I set up without this issue at all.
I did some research on this and it talked about increasing my client max_allowed_packet size through a /etc/my.cnf file. I attempted to do this without any success. Would somebody be able to walk me through this as I can't even find the .cnf file. Is this even the right approach for a Java based connection?
Thanks in advance.
On the server, you must change your mysql configuration file. You will need to find it based on your operation system. See https://dev.mysql.com/doc/refman/5.6/en/option-files.html
You should not need to do anything on the client side to set this. Just send a large amount of data.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java sockets - java.net.ConnectException: Connection refused: connect
I've created a simple chat program which communicates using sockets. Everything works fine when I'm running it on localhost. However, the problems occur when I try to link the client and server programs using my IP.
http://www.canyouseeme.org/ can connect to my server on port 9999 so I know that the server is fine and the port is open. However, my client can't connect.
The error log...
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 Client.connect(Client.java:129)
at Client.main(Client.java:47)
Does anybody have any idea what might be causing this? Thanks in advance.
Links to the full source code:
http://pastebin.com/2XftHtn9
Are you trying to connect to your own server using its public IP address from inside your LAN? For most SoHo routers, port forwarding only works WAN-to-LAN, not LAN-to-LAN. What you're looking for is called "hairpin NAT", and many SoHo routers just don't do it. To reach your server from inside your LAN, use its inside IP address, not its public IP address.
Testing an unmodified version of JCTerm (terminal emulator, can be used as an applet; I would like to use the applet functionality), everything seems to be working fine in Eclipse's AppletViewer, but testing the resulting jar file in an web page, all options display a message box with a flashing yellow warning symbol and do nothing. Some options display a "Establish the connection before this setting" error.
The option I'm interested in using is 'Open SHELL Session...' from the File menu. When trying 'Open SHELL Session...' option, the Java Console for the applet displays this stuff, which seems directly related. I don't have a clue as to what it means though.
network: Connecting http://xxx.xx.xx.xxx/crossdomain.xml with proxy=DIRECT
network: Connecting http://xxx.xx.xx.xxx:80/ with proxy=DIRECT
java.security.PrivilegedActionException: java.net.ConnectException: Connection refused: connect
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.deploy.net.CrossDomainXML.check(Unknown Source)
at com.sun.deploy.net.CrossDomainXML.check(Unknown Source)
at sun.plugin2.applet.Applet2SecurityManager.checkConnect(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.jcraft.jsch.Util$1.run(Util.java:354)
at java.lang.Thread.run(Unknown Source)
Caused by: 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 sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at com.sun.deploy.net.CrossDomainXML$2.run(Unknown Source)
... 10 more
An example of this program working correctly can be found here, http://wiredx.net/jcterm/
I'm the author of jcterm.
The jar files at http://wiredx.net/jcterm/ have been digitally signed.
So, if you copy and intstall those files into your web server, it will work.
This looks like a security problem.
By default, an unsigned Java applet can only connect to the host if was loaded from. Additionally, if the host you want to connect to allows this with it's crossdomain.xml file, you can also connect to other hosts.
Judging from the stack trace, you want to connect to a host which does not have an HTTP server, and thus can't provide a crossdomain.xml file. For this reason, you get this exception here.
There are these ways out of this:
Put the applet on the same web server which you want to connect to with SSH later. (Every applet can connect to its own host.)
Let the SSH server have a minimal web server with a crossdomain.xml. (The crossdomain.xml must allow content from the applet's server to access this server.)
Sign the applet (and let the user trust it). (Signed and trusted applets are allowed to do everything.)
The official WiredX sample applet you linked uses the last method, this is why it works even when connecting to your server.
After a hard work, I have set up Java RMI tunnelling using apache as http
server. Everything is fine and Works like a charm at my office LAN.But when I installed at client's place, I am getting some exception.The RMI System works only on his server.
When I tried from other clients' pcs I get the following.
Can you guys help me solve this?
java.rmi.ConnectException: Connection refused to host: 172.xx.x.xxx;
nested exception is: java.net.ConnectException: Connection timed
out: connect at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown
Source) at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown
Source) at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown
Source) at sun.rmi.server.UnicastRef.invoke(Unknown Source) at
java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown
Source) at
java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy1.getUserID(Unknown Source) at
rmi.source.ServerImpl$JobScheduler.run(ServerImpl.java:265) at
java.util.TimerThread.mainLoop(Unknown Source) at
java.util.TimerThread.run(Unknown Source) Caused by:
java.net.ConnectException: Connection timed out: 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
sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown
Source) at
sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown
Source) ... 10 more
I am running files from roseindia rmi
there is mistake rmicregistry, one have to use rmiregistry:
I changed ip to localhost, how to run on special port number I don't know this moment
one has to use rmiregistry
not rmicregistry
so:
move to folder where does your class files take seat.
type : rmiregistry
then open 2 new command prompts move to same folder where classes seat and use one to run server and another to client.
RMI servers have the nasty habit to pass new port numbers to clients to handle the client communication to a particular Remote object. If this port number is blocked by the firewall, you would get an exception like that.
Make sure you register all Remote objects with the same port number. RMI will then multiplex the client calls for you.
after my research, the solution is:
System.setProperty("java.rmi.server.hostname", IP);
before you register the service on server side. following is an example, hope it might help you!
public static void main(String[] args) throws MalformedURLException, RemoteException, AlreadyBoundException {
RmiServer server=new RmiServer();
System.setProperty("java.rmi.server.hostname", 指定IP);
LocateRegistry.createRegistry(8808);
Naming.rebind("//10.10.116.74:8808/SAMPLE-SERVER", server);
}