MySQL connectivity from an external network with java - java

Hey guys i'm trying to connect mysql database from my friend's wifi. The database connects and works fine from all my other devices with are all on the same network but i want to connect to the database from an external network(my friends network).
I have replaced localhost/device ip with my public ip address which i got from whatsmyip.com
String connectionUrl = "jdbc:mysql://myPublicIp:3306/trial";
and i ran it from my friend's home:
this is what i got:
com.mysql.jdbc.CommunicationException: Communications link failure due to underlying exception:
BEGIN NESTED EXCEPTION
java.net.Connect.Exception
MESSAGE: Connection timed out: connect
As i said there is no prob with the code, it works fine for locally connected devices. I wanted to know if my string's syntax is wrong.
Assuming my public ip address is 50.50.50.50 and my device running mysql is 192.168.x.x and the port is 3306 how should i format my string.
Thanks.

You should forward your 3306 port for the device which got the database on it, otherwise your router can't decide which device to look for when you request connection through this port and it won't let anything through instead.

Related

Failed to connect to local IP from Android application

The client failed to establish a connection to the local address localhost:3000.
The error encountered was:
dial tcp [::1]:3000: connectex: No connection could be made because the target machine actively refused it.
How can I solve it?
Change your localhost:3000 address to local IP address for example 192.168.1.15:3000 etc.
Your server and client must be in same network
Emulator
On an emulator the localhost is accessible using the IP 10.0.2.2 .
So localhost:3000 will be 10.0.2.2:3000 in your emulator.
Physical device
First of all you must be on the same network to be able to connect.
Get the PC ip using ipconfig (windows) and ip a or ifconfig (linux)
Connect to the PC localhost using it's ip. i.g 192.168.2.121:3000
(your firewall must be OK with this)
If you are trying to connect using code, same things are needed for this

How to connect Android device to a Java program via sockets?

I need to connect an android device to my java program via socket connection. The device is working as server. The problem is that while trying out socket connection I need to give IP address and port number of server.
Is there something like a static IP address for an Android device to connect? If not, is there any alternative way to establish a socket connection between the device and my program?
As the device has to have a connection to the same network as the computer, it has to have an IP address configured. So you have to use that one.
The used port is defined by the server application running on your device.
If you have the problem that anyhow a normal network connection is not possible, but you have an ADB connection, you can forward local pots to the device and let your server listen:
http://developer.android.com/tools/help/adb.html#forwardports
Over a mobile connection (GPRS, EDGE, UMTS, HSDPA) there will be no practicable way to establish a connection from somewhere to your device (instead you have to do it the other way, while canceling the whole client-server directive), in cause of the used IP sharing. In that case you have a problem of which I'm not aware if it's possible in general, not to mention how you could achieve this.
Otherwise you simply have to configure a static IP for your device when connected to your local network, or you have to evaluate the actual IP of your device every most of the time, while using it with this configuration:
Resolve it by the device itself
Resolve InetAdress from DhcpInfo
Documentation for DhcpInfo
Resolve by using ADB command

connection link failure mysql

I am trying to connect to my android device with my mysql database, I can do it in local (from a emulation device) but when I use my IP to connect to with my mobile I get:
Communication Link failure
The last packet send succesfully to the server was 0 millisecond ago. The driver has not recived any packet from the server
my code is:
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://MYIP:3306/BD", "USER", "USER");
with localhost it works:
Connection conn=DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/BD", "USER", "USER");
Thanks for your help.
To connect your Android device to Mysql you should use webs ervices. you can find more information in this tutorial
the Tutorial use PHP to creat web services, however you can use any other server side language to connect with mysql database, this will return JSON data that you can parse it to in your android app
It seems this is a network issue, if the devices are in the same LAN don't use your internet IP. Or even if the devices are in different network your router could be blocking your connection.

Android socket chat connection errors

Hy! I'm adapting a chat using sockets and threads from java client to android client. The server remains the same. I've wrote the internet and ACCESS_NETWORK_STATE permissions in the manifest. The problem is that when I try to connect to server it throws some errors.
The try{ socket = new Socket("localhost", 5000);} line throws:
link to screenshot
What could be the problem ? Would you want to put the whole code here ?
I'm sure that you're trying to contact your local machine and not the device itself. The phone will address itself using localhost or 127.0.0.1. So when your device is not a server and is not listening for that port the connection will fail.
Try to use 10.0.2.2. This should target your machine you're developing on. (source)
Have you tried using the real network ip of your machine (sth. like 192.168.0.1) instead of "localhost"? The error itself looks to me like there's nobody listening on port 5000. So I guess the Droid is trying to connect to itself instead to the server.

java mysql connection across network

I need to connect to a mysql database across a network.
The connection string ive given is
"jdbc:mysql://host/dbname"
i can access the site across the network but the only problem is with the java database connection.
Ive updated the phpmyadmin.conf file giving
# Deny from all
Allow from all
But still the database connection cannot be made.
Please help me..
I don't know much aboud phpmyadmin, but what I did to configure a remote db:
The connection string is: jdbc:mysql://host:port/dbname
The port is 3306 by default
The mysql user is often name#localhost, you need to configure a user for the remote (client) machine (IP address, hostname, wildcard)
HTH
Ok, there are few things you need to check ...
1) If your database got username/password
2) Have you restarted your server after altering phpmyadmin.conf?
3) Make sure you are connecting to the correct port. Ex: mysql://host:port/dbname (if port differs from default.
4) make sure that the PC that have the DB allows incoming connections through the port.
If you still facing problems, try disabling your antivirus/firewall on the PC that have the DM and try.

Categories

Resources