I know, there will be duplicates on this topic, but solutions I found were like 2 years old, and not working for me, so thats why I am asking for topic update.
Question is simple: How to connect my Android device to MySQL database?
I have followed this old topic from Feb 2013 (ofc with a comparsion to other related topics, which were actually almost the same). https://stackoverflow.com/a/14982316/4256313
I have found library from the solution, the mysql-connector-java-3.0.17-ga-bin.jar
After beeing not succesfull, I also tried newer version of this library, mysql-connector-java-5.1.23-bin.jar
Both libraries were added to project following this video-guide: https://www.youtube.com/watch?v=dpuJPoXkFG4
And now my personal solution: I have 4 fields in Main Activity, for Host, port, name and password.
Main activity looks like this (nothing really special here):
TextView txtHost;
TextView txtPort;
TextView txtLoginName;
TextView txtPassword;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtHost = (TextView) findViewById(R.id.txtHost);
txtPort = (TextView) findViewById(R.id.txtPort);
txtLoginName = (TextView) findViewById(R.id.txtLoginName);
txtPassword = (TextView) findViewById(R.id.txtPassword);
login = (Button) findViewById(R.id.btnLogin);
}
//this is function bind to Login Button.
// I need to do a connection in static class to preserve it for another activities
public void klik(View view){
ConnCore.ConnCoreStatic.servername = txtHost.getText().toString();
ConnCore.ConnCoreStatic.port = txtPort.getText().toString();
ConnCore.ConnCoreStatic.name = txtLoginName.getText().toString();
ConnCore.ConnCoreStatic.pass = txtPassword.getText().toString();
if(ConnCore.ConnCoreStatic.DBConn(MainActivity.this)==true){
Intent inent = new Intent(this, TableRows.class);
startActivity(inent);
}
}
And here is my static method handling the connection:
public class ConnCore {
public static class ConnCoreStatic{
static String servername;
static String port;
static String name;
static String pass;
static Connection mysqlConn;
static List<String> DBResponse;
static boolean DBConn(Context c){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e){
AlertDialog alertDialog = new AlertDialog.Builder(c).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Could not initialize driver");
alertDialog.show();
return false;
}
try{ //Toast just for me, checking if data for conn function is correct
Toast.makeText(c, "jdbc:mysql://"+servername+":"+port+name+pass,
Toast.LENGTH_LONG).show();
mysqlConn = DriverManager.getConnection(servername+":"+port,name,pass);
return true;
}
catch(Exception e){
AlertDialog alertDialog = new AlertDialog.Builder(c).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Could not login to database");
alertDialog.show();
return false;
}
}
}
}
I have also added INTERNET permission to android manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
I have also included (hopefully) all needed libraries in my static class.
import java.sql.*;
And where is actually my problem? Android is not connecting. This part always ends in catch block. And I am pretty sure, I have right connection access, for testing purposes, I use db I am frequently connecting to from my PC.
try{
mysqlConn = DriverManager.getConnection(servername+":"+port,name,pass);
return true;
}
catch(Exception e){
AlertDialog alertDialog = new AlertDialog.Builder(c).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Could not login to database");
alertDialog.show();
return false;
}
As an adress, I tried I think all combinations I have found in examples in this problem:
jdbc:mysql://XXX.XXX.XXX.XXX:YYYY
jdbc:mysql://XXX.XXX.XXX.XXX:YYYY/database_name (dont want that, just trying)
XXX.XXX.XXX.XXX:YYYY
XXX.XXX.XXX.XXX:YYYY/database_name (dont want that, just trying)
Also just for eliminating mistake from some conversion of strings from UI elements, I wrote down the connection data in code.
Still not working. Still no access. Nor connected to PC (where phone gets internet through USB from PC), nor disconnected from pc, running on wifi directly or on mobile data directly.
I dont know, if it is just my feeling, but even on PC, it takes 0.5 - 2 sec to get connected. Here, it seems like the alert dialog is shown almost immediately, like it have not even tried to get connected.
What is wrong here? Please, can you help?
Thak you in advance.
//EDITS BASED ON COMMENTS: Try/catch block throws
"java.sqlException: No suitable Driver"
//FROM ONE ERROR TO ANOTHER I have succesfully added a driver. 3.0.17 is outdated, need to use mysql-connector-java-5.1.23-bin.jar In Adress, there has to be "jdbc:mysql://" prefix
Now have this error:
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 recived any packets form the server
Still have the right connection data, and still looks like android does not even try to connect. Same error on wifi, phone data, shared computer internet acces, or even disconnected from internet.
//After more digging: I have found a different way, how to pass connection data to JDBC: jdbc:mysql://XXX.XXX.XXX.XXX:YYYY?user=root&password=test&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
Well, now, it says, it tries to connect to this IP 10 times, now with error:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 10 times. Giving up.
Also the Android studio in "Android monitor" says:
I/System.out: [CDS]connect[/XX.XX.XXX.XX:3306] tm:90
But I checked the log on my MySQL server, and there is no incomming connection.
Any suitable for android?